Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #64109 > unrolled thread
| Started by | Nac Temha <naccttemha@gmail.com> |
|---|---|
| First post | 2014-01-17 00:24 +0200 |
| Last post | 2014-01-16 17:36 -0800 |
| Articles | 9 — 7 participants |
Back to article view | Back to comp.lang.python
Python solve problem with string operation Nac Temha <naccttemha@gmail.com> - 2014-01-17 00:24 +0200
Re: Python solve problem with string operation John Gordon <gordon@panix.com> - 2014-01-16 22:30 +0000
Re: Python solve problem with string operation Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-01-16 22:48 +0000
Re: Python solve problem with string operation John Gordon <gordon@panix.com> - 2014-01-16 22:58 +0000
Re: Python solve problem with string operation giacomo boffi <pecore@pascolo.net> - 2014-01-17 01:17 +0100
Re: Python solve problem with string operation giacomo boffi <pecore@pascolo.net> - 2014-01-17 01:38 +0100
Re: Python solve problem with string operation Denis McMahon <denismfmcmahon@gmail.com> - 2014-01-17 00:30 +0000
Re: Python solve problem with string operation "Rhodri James" <rhodri@wildebst.org.uk> - 2014-01-17 01:05 +0000
Re: Python solve problem with string operation Asaf Las <roegltd@gmail.com> - 2014-01-16 17:36 -0800
| From | Nac Temha <naccttemha@gmail.com> |
|---|---|
| Date | 2014-01-17 00:24 +0200 |
| Subject | Python solve problem with string operation |
| Message-ID | <mailman.5607.1389911083.18130.python-list@python.org> |
[Multipart message — attachments visible in raw view] — view raw
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.
[toc] | [next] | [standalone]
| From | John Gordon <gordon@panix.com> |
|---|---|
| Date | 2014-01-16 22:30 +0000 |
| Message-ID | <lb9mi3$le4$1@reader1.panix.com> |
| In reply to | #64109 |
In <mailman.5607.1389911083.18130.python-list@python.org> Nac Temha <naccttemha@gmail.com> writes:
> --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)
--
John Gordon Imagine what it must be like for a real medical doctor to
gordon@panix.com watch 'House', or a real serial killer to watch 'Dexter'.
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2014-01-16 22:48 +0000 |
| Message-ID | <mailman.5609.1389912537.18130.python-list@python.org> |
| In reply to | #64110 |
On 16/01/2014 22:30, John Gordon wrote: > In <mailman.5607.1389911083.18130.python-list@python.org> Nac Temha <naccttemha@gmail.com> writes: > >> --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 :) -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence
[toc] | [prev] | [next] | [standalone]
| From | John Gordon <gordon@panix.com> |
|---|---|
| Date | 2014-01-16 22:58 +0000 |
| Message-ID | <lb9o74$7sb$1@reader1.panix.com> |
| In reply to | #64112 |
In <mailman.5609.1389912537.18130.python-list@python.org> Mark Lawrence <breamoreboy@yahoo.co.uk> writes:
> > 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')
--
John Gordon Imagine what it must be like for a real medical doctor to
gordon@panix.com watch 'House', or a real serial killer to watch 'Dexter'.
[toc] | [prev] | [next] | [standalone]
| From | giacomo boffi <pecore@pascolo.net> |
|---|---|
| Date | 2014-01-17 01:17 +0100 |
| Message-ID | <87eh47a2d3.fsf@pascolo.net> |
| In reply to | #64109 |
Nac Temha <naccttemha@gmail.com> writes:
> 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
%
--
your instructor is a mean person
[toc] | [prev] | [next] | [standalone]
| From | giacomo boffi <pecore@pascolo.net> |
|---|---|
| Date | 2014-01-17 01:38 +0100 |
| Message-ID | <877g9za1ed.fsf@pascolo.net> |
| In reply to | #64117 |
giacomo boffi <pecore@pascolo.net> writes: > % 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 % -- for Nikos
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2014-01-17 00:30 +0000 |
| Message-ID | <lb9tio$7p1$4@dont-email.me> |
| In reply to | #64109 |
On Fri, 17 Jan 2014 00:24:40 +0200, Nac Temha wrote:
> 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[i]!=input[i-1]:
output+=input[i]
--
Denis McMahon, denismfmcmahon@gmail.com
[toc] | [prev] | [next] | [standalone]
| From | "Rhodri James" <rhodri@wildebst.org.uk> |
|---|---|
| Date | 2014-01-17 01:05 +0000 |
| Message-ID | <op.w9s3zrnr5079vu@gnudebeest> |
| In reply to | #64109 |
On Thu, 16 Jan 2014 22:24:40 -0000, Nac Temha <naccttemha@gmail.com> wrote: > 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. I almost convinced myself this was homework, you know. A hint as to why you might want such a thing would look a lot less suspicious :-) The simplest way to do this is probably using groupby: from itertools import groupby input = "344111133311222223377" output = "".join(k for k, _ in groupby(s)) print output -- Rhodri James *-* Wildebeest Herder to the Masses
[toc] | [prev] | [next] | [standalone]
| From | Asaf Las <roegltd@gmail.com> |
|---|---|
| Date | 2014-01-16 17:36 -0800 |
| Message-ID | <46ac774f-02c7-4dc7-993e-9880b22cbb73@googlegroups.com> |
| In reply to | #64109 |
inpu = "344111133311222223377"
tstr = inpu[0]
for k in range(1, len(inpu)):
if inpu[k] != inpu[k-1] :
tstr = tstr + inpu[k]
print(tstr)
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web