Python solve problem with string operation

N

Nac Temha

Hi everyone,

I want to do operation with chars in the given string. Actually I want to
grouping the same chars.

For example;

input : "344111133311222223377"
operation-> (3)(44)(1111)(333)(11)(22222)(33)(77)
output: "34131237"



How can I do without list, regular expression. just using string
operations. Using an effective methods of python for this problem.


Thanks,
Best regards.
 
J

John Gordon

In said:
--047d7b6d95d0367a3d04f01de490
Content-Type: text/plain; charset=ISO-8859-1
Hi everyone,
I want to do operation with chars in the given string. Actually I want to
grouping the same chars.
For example;
input : "344111133311222223377"
operation-> (3)(44)(1111)(333)(11)(22222)(33)(77)
output: "34131237"

input = "344111133311222223377"
output = []
previous_ch = None
for ch in input:
if ch != previous_ch:
output.append(ch)
previous_ch = ch
print ''.join(output)
 
M

Mark Lawrence

In said:
--047d7b6d95d0367a3d04f01de490
Content-Type: text/plain; charset=ISO-8859-1
Hi everyone,
I want to do operation with chars in the given string. Actually I want to
grouping the same chars.
For example;
input : "344111133311222223377"
operation-> (3)(44)(1111)(333)(11)(22222)(33)(77)
output: "34131237"

input = "344111133311222223377"
output = []
previous_ch = None
for ch in input:
if ch != previous_ch:
output.append(ch)
previous_ch = ch
print ''.join(output)

Cheat, you've used a list :)
 
J

John Gordon

In said:
input = "344111133311222223377"
output = []
previous_ch = None
for ch in input:
if ch != previous_ch:
output.append(ch)
previous_ch = ch
print ''.join(output)
Cheat, you've used a list :)

Ack! I missed that the OP doesn't want to use lists.

Well, let's try this instead:

import sys

input = "344111133311222223377"
previous_ch = None
for ch in input:
if ch != previous_ch:
sys.stdout.write(ch)
previous_ch = ch
sys.stdout.write('\n')
 
G

giacomo boffi

Nac Temha said:
Hi everyone,

I want to do operation with chars in the given string. Actually I want to
grouping the same chars.

For example;

input : "344111133311222223377"
operation-> (3)(44)(1111)(333)(11)(22222)(33)(77)
output: "34131237"



How can I do without list, regular expression. just using string operations.
Using an effective methods of python for this problem.


% cat a.py
def f(s,n):
if s[n+1] == s[n]:
return s[:n]+s[n+1:], n
return s, n+1

i = "344111133311222223377"
n = 0

while n+1 != len(i):
i, n = f(i, n)

print i
% python a.py
34131237
%
 
D

Denis McMahon

Hi everyone,

I want to do operation with chars in the given string. Actually I want
to grouping the same chars.

For example;

input : "344111133311222223377"
operation-> (3)(44)(1111)(333)(11)(22222)(33)(77)
output: "34131237"
How can I do without list, regular expression. just using string
operations. Using an effective methods of python for this problem.

You can do it on one line, but it looks really messy:

output = ''.join([{x:input[x]for x in range(len(input))}[x]for x in range
(len({x:input[x]for x in range(len(input))}))if(x==0 or {x:input[x]for x
in range(len(input))}[x-1]!={x:input[x]for x in range(len(input))}[x])])

It looks much better if you do it in steps:

a = {x:input[x]for x in range(len(input))}
b = [a[n]for n in range(len(a))if(n==0 or a[n-1]!=a[n])])
output = ''.join(b)

If you really want to do it using just 'string' ops:

for i in range(len(input)):
if (i==0):
output=input[0]
elif input!=input[i-1]:
output+=input
 
G

giacomo boffi

giacomo boffi said:
% python a.py
34131237

% cat a.py
i="344111133311222223377";n=0
while n+1!=len(i):i,n=(i[:n]+i[n+1:],n) if i[n+1]==i[n] else (i,n+1)
print i
% python a.py
34131237
%
 
A

Asaf Las

inpu = "344111133311222223377"
tstr = inpu[0]
for k in range(1, len(inpu)):
if inpu[k] != inpu[k-1] :
tstr = tstr + inpu[k]

print(tstr)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,244
Members
46,838
Latest member
KandiceChi

Latest Threads

Top