Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #30930 > unrolled thread

Question on Python Split

Started bysubhabangalore@gmail.com
First post2012-10-07 12:30 -0700
Last post2012-10-08 07:45 -0700
Articles 5 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  Question on Python Split subhabangalore@gmail.com - 2012-10-07 12:30 -0700
    Re: Question on Python Split MRAB <python@mrabarnett.plus.com> - 2012-10-07 21:01 +0100
    Re: Question on Python Split Terry Reedy <tjreedy@udel.edu> - 2012-10-07 16:08 -0400
    Re: Question on Python Split Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-10-07 20:34 -0400
    Re: Question on Python Split subhabangalore@gmail.com - 2012-10-08 07:45 -0700

#30930 — Question on Python Split

Fromsubhabangalore@gmail.com
Date2012-10-07 12:30 -0700
SubjectQuestion on Python Split
Message-ID<68fc8fcb-b356-4fce-8541-e2abf371fecf@googlegroups.com>
Dear Group,

Suppose I have a string as, 

"Project Gutenberg has 36000 free ebooks for Kindle Android iPad iPhone."

I am terming it as,

str1= "Project Gutenberg has 36000 free ebooks for Kindle Android iPad iPhone." 

I am working now with a split function,

str_words=str1.split()
so, I would get the result as,
['Project', 'Gutenberg', 'has', '36000', 'free', 'ebooks', 'for', 'Kindle', 'Android', 'iPad', 'iPhone.']

But I am looking for,

['Project Gutenberg', 'has 36000', 'free ebooks', 'for Kindle', 'Android iPad', 'iPhone']

This can be done if we assign the string as,

str1= "Project Gutenberg, has 36000, free ebooks, for Kindle, Android iPad, iPhone,"

and then assign the split statement as,

str1_word=str1.split(",")

would produce,

['Project Gutenberg', ' has 36000', ' free ebooks', ' for Kindle', ' Android iPad', ' iPhone', '']

My objective generally is achieved, but I want to convert each group here in tuple so that it can be embedded, like,

[(Project Gutenberg), (has 36000), (free ebooks), (for Kindle), ( Android iPad), (iPhone), '']

as I see if I assign it as

for i in str1_word:
       print i
       ti=tuple(i)
       print ti

I am not getting the desired result.

If I work again from tuple point, I get it as,
>>> tup1=('Project Gutenberg')
>>> tup2=('has 36000')
>>> tup3=('free ebooks')
>>> tup4=('for Kindle')
>>> tup5=('Android iPad')
>>> tup6=tup1+tup2+tup3+tup4+tup5
>>> print tup6
Project Gutenberghas 36000free ebooksfor KindleAndroid iPad

Then how may I achieve it? If any one of the learned members can kindly guide me.
Thanks in Advance,
Regards,
Subhabrata.

NB: Apology for some minor errors. 






[toc] | [next] | [standalone]


#30934

FromMRAB <python@mrabarnett.plus.com>
Date2012-10-07 21:01 +0100
Message-ID<mailman.1933.1349640075.27098.python-list@python.org>
In reply to#30930
On 2012-10-07 20:30, subhabangalore@gmail.com wrote:
> Dear Group,
>
> Suppose I have a string as,
>
> "Project Gutenberg has 36000 free ebooks for Kindle Android iPad iPhone."
>
> I am terming it as,
>
> str1= "Project Gutenberg has 36000 free ebooks for Kindle Android iPad iPhone."
>
> I am working now with a split function,
>
> str_words=str1.split()
> so, I would get the result as,
> ['Project', 'Gutenberg', 'has', '36000', 'free', 'ebooks', 'for', 'Kindle', 'Android', 'iPad', 'iPhone.']
>
> But I am looking for,
>
> ['Project Gutenberg', 'has 36000', 'free ebooks', 'for Kindle', 'Android iPad', 'iPhone']
>
> This can be done if we assign the string as,
>
> str1= "Project Gutenberg, has 36000, free ebooks, for Kindle, Android iPad, iPhone,"
>
> and then assign the split statement as,
>
> str1_word=str1.split(",")
>
> would produce,
>
> ['Project Gutenberg', ' has 36000', ' free ebooks', ' for Kindle', ' Android iPad', ' iPhone', '']
>
It can also be done like this:

 >>> str1 = "Project Gutenberg has 36000 free ebooks for Kindle Android 
iPad iPhone."
 >>> # Splitting into words:
 >>> s = str1.split()
 >>> s
['Project', 'Gutenberg', 'has', '36000', 'free', 'ebooks', 'for', 
'Kindle', 'Android', 'iPad', 'iPhone.']
 >>> # Using slicing with a stride of 2 gives:
 >>> s[0 : : 2]
['Project', 'has', 'free', 'for', 'Android', 'iPhone.']
 >>> # Similarly for the other words gives:
 >>> s[1 : : 2]
['Gutenberg', '36000', 'ebooks', 'Kindle', 'iPad']
 >>> # Combining them in pairs, and adding an extra empty string in case 
there's an odd number of words:
 >>> [(x + ' ' + y).rstrip() for x, y in zip(s[0 : : 2], s[1 : : 2] + [''])]
['Project Gutenberg', 'has 36000', 'free ebooks', 'for Kindle', 'Android 
iPad', 'iPhone.']

> My objective generally is achieved, but I want to convert each group here in tuple so that it can be embedded, like,
>
> [(Project Gutenberg), (has 36000), (free ebooks), (for Kindle), ( Android iPad), (iPhone), '']
>
> as I see if I assign it as
>
> for i in str1_word:
>         print i
>         ti=tuple(i)
>         print ti
>
> I am not getting the desired result.
>
> If I work again from tuple point, I get it as,
>>>> tup1=('Project Gutenberg')
>>>> tup2=('has 36000')
>>>> tup3=('free ebooks')
>>>> tup4=('for Kindle')
>>>> tup5=('Android iPad')
>>>> tup6=tup1+tup2+tup3+tup4+tup5
>>>> print tup6
> Project Gutenberghas 36000free ebooksfor KindleAndroid iPad
>
It's the comma that makes the tuple, not the parentheses, except for the 
empty tuple which is just empty parentheses, i.e. ().

> Then how may I achieve it? If any one of the learned members can kindly guide me.

 >>> [((x + ' ' + y).rstrip(), ) for x, y in zip(s[0 : : 2], s[1 : : 2] 
+ [''])]
[('Project Gutenberg',), ('has 36000',), ('free ebooks',), ('for 
Kindle',), ('Android iPad',), ('iPhone.',)]

Is this what you want?

If you want it to be a list of pairs of words, then:

 >>> [(x, y) for x, y in zip(s[0 : : 2], s[1 : : 2] + [''])]
[('Project', 'Gutenberg'), ('has', '36000'), ('free', 'ebooks'), ('for', 
'Kindle'), ('Android', 'iPad'), ('iPhone.', '')]

[toc] | [prev] | [next] | [standalone]


#30937

FromTerry Reedy <tjreedy@udel.edu>
Date2012-10-07 16:08 -0400
Message-ID<mailman.1936.1349640557.27098.python-list@python.org>
In reply to#30930
On 10/7/2012 3:30 PM, subhabangalore@gmail.com wrote:

> If I work again from tuple point, I get it as,
>>>> tup1=('Project Gutenberg')
>>>> tup2=('has 36000')
>>>> tup3=('free ebooks')
>>>> tup4=('for Kindle')
>>>> tup5=('Android iPad')

These are strings, not tuples. Numbered names like this are a bad idea.

>>>> tup6=tup1+tup2+tup3+tup4+tup5
>>>> print tup6
> Project Gutenberghas 36000free ebooksfor KindleAndroid iPad

tup1=('Project Gutenberg')
tup2=('has 36000')
tup3=('free ebooks')
tup4=('for Kindle')
tup5=('Android iPad')
print(' '.join((tup1,tup2,tup3,tup4,tup5)))

 >>>
Project Gutenberg has 36000 free ebooks for Kindle Android iPad

-- 
Terry Jan Reedy

[toc] | [prev] | [next] | [standalone]


#30941

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2012-10-07 20:34 -0400
Message-ID<mailman.1940.1349656470.27098.python-list@python.org>
In reply to#30930
On Sun, 7 Oct 2012 12:30:52 -0700 (PDT), subhabangalore@gmail.com
declaimed the following in gmane.comp.python.general:

> 
> But I am looking for,
> 
> ['Project Gutenberg', 'has 36000', 'free ebooks', 'for Kindle', 'Android iPad', 'iPhone']
> 

	Is splitting a sentence at every other word really what you want? Or
are you intending, at some point, to have the splitting take place on
syntactic/semantic features (subject, verb, object...).

	If the latter, you may be in need of some Natural Language
Processing (NLP) libraries/algorithms. (First google hit:
http://nltk.org/ )
-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
        wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

[toc] | [prev] | [next] | [standalone]


#30959

Fromsubhabangalore@gmail.com
Date2012-10-08 07:45 -0700
Message-ID<8c16a71f-0c2f-4253-8cb9-d9a9312137ed@googlegroups.com>
In reply to#30930
On Monday, October 8, 2012 1:00:52 AM UTC+5:30, subhaba...@gmail.com wrote:
> Dear Group,
> 
> 
> 
> Suppose I have a string as, 
> 
> 
> 
> "Project Gutenberg has 36000 free ebooks for Kindle Android iPad iPhone."
> 
> 
> 
> I am terming it as,
> 
> 
> 
> str1= "Project Gutenberg has 36000 free ebooks for Kindle Android iPad iPhone." 
> 
> 
> 
> I am working now with a split function,
> 
> 
> 
> str_words=str1.split()
> 
> so, I would get the result as,
> 
> ['Project', 'Gutenberg', 'has', '36000', 'free', 'ebooks', 'for', 'Kindle', 'Android', 'iPad', 'iPhone.']
> 
> 
> 
> But I am looking for,
> 
> 
> 
> ['Project Gutenberg', 'has 36000', 'free ebooks', 'for Kindle', 'Android iPad', 'iPhone']
> 
> 
> 
> This can be done if we assign the string as,
> 
> 
> 
> str1= "Project Gutenberg, has 36000, free ebooks, for Kindle, Android iPad, iPhone,"
> 
> 
> 
> and then assign the split statement as,
> 
> 
> 
> str1_word=str1.split(",")
> 
> 
> 
> would produce,
> 
> 
> 
> ['Project Gutenberg', ' has 36000', ' free ebooks', ' for Kindle', ' Android iPad', ' iPhone', '']
> 
> 
> 
> My objective generally is achieved, but I want to convert each group here in tuple so that it can be embedded, like,
> 
> 
> 
> [(Project Gutenberg), (has 36000), (free ebooks), (for Kindle), ( Android iPad), (iPhone), '']
> 
> 
> 
> as I see if I assign it as
> 
> 
> 
> for i in str1_word:
> 
>        print i
> 
>        ti=tuple(i)
> 
>        print ti
> 
> 
> 
> I am not getting the desired result.
> 
> 
> 
> If I work again from tuple point, I get it as,
> 
> >>> tup1=('Project Gutenberg')
> 
> >>> tup2=('has 36000')
> 
> >>> tup3=('free ebooks')
> 
> >>> tup4=('for Kindle')
> 
> >>> tup5=('Android iPad')
> 
> >>> tup6=tup1+tup2+tup3+tup4+tup5
> 
> >>> print tup6
> 
> Project Gutenberghas 36000free ebooksfor KindleAndroid iPad
> 
> 
> 
> Then how may I achieve it? If any one of the learned members can kindly guide me.
> 
> Thanks in Advance,
> 
> Regards,
> 
> Subhabrata.
> 
> 
> 
> NB: Apology for some minor errors.

Thank you for nice answer. Your codes and discussions always inspire me.

Regards,
Subhabrata. 

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web