Question on Python Split

subhabangalore at gmail.com subhabangalore at gmail.com
Mon Oct 8 10:45:12 EDT 2012


On Monday, October 8, 2012 1:00:52 AM UTC+5:30, subhaba... at 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. 



More information about the Python-list mailing list