Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Question on Python Split
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  4 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
subhabangal...@gmail.com  
View profile  
 More options Oct 7 2012, 3:30 pm
Newsgroups: comp.lang.python
From: subhabangal...@gmail.com
Date: Sun, 7 Oct 2012 12:30:52 -0700 (PDT)
Local: Sun, Oct 7 2012 3:30 pm
Subject: Question on Python Split
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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
MRAB  
View profile  
 More options Oct 7 2012, 4:01 pm
Newsgroups: comp.lang.python
From: MRAB <pyt...@mrabarnett.plus.com>
Date: Sun, 07 Oct 2012 21:01:07 +0100
Local: Sun, Oct 7 2012 4:01 pm
Subject: Re: Question on Python Split
On 2012-10-07 20:30, subhabangal...@gmail.com wrote:

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.']

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.', '')]


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Terry Reedy  
View profile  
 More options Oct 7 2012, 4:09 pm
Newsgroups: comp.lang.python
From: Terry Reedy <tjre...@udel.edu>
Date: Sun, 07 Oct 2012 16:08:47 -0400
Local: Sun, Oct 7 2012 4:08 pm
Subject: Re: Question on Python Split
On 10/7/2012 3:30 PM, subhabangal...@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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
subhabangal...@gmail.com  
View profile  
 More options Oct 8 2012, 10:45 am
Newsgroups: comp.lang.python
From: subhabangal...@gmail.com
Date: Mon, 8 Oct 2012 07:45:12 -0700 (PDT)
Local: Mon, Oct 8 2012 10:45 am
Subject: Re: Question on Python Split

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

Regards,
Subhabrata.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »