top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration
Print Preview

split in python without using comma [CLOSED]

+1 vote
25 views

How can I use the '.split()' method (am I right in calling it a method?) without instead of writing each comma between words in the pie list in the following code? Also, is there a way to use .split instead of typing the apostrophes?

import random
pie=['keylime', 'peach', 'apple', 'cherry', 'pecan']
print(random.choice(pie))
closed with the note: None
posted Aug 14, 2013 by Seema Siddique

Share this question
Facebook Share Button Twitter Share Button Google+ Share Button LinkedIn Share Button Multiple Social Share Button

1 Answer

+1 vote

I think you are referring to this:

 pie = 'keylime peach apple cherry pecan'.split()

While it's easier to type, and does save a few characters, I think the original list is clearer to a reader of your program.

answer Aug 14, 2013 by Abhay Kulkarni
Similar Questions
0 votes

I have a list, songs, which I want to divide into two groups.
Essentially, I want:

new_songs = [s for s in songs if s.is_new()]
old_songs = [s for s in songs if not s.is_new()]

but I don't want to make two passes over the list. I could do:

new_songs = []
old_songs = []
for s in songs:
 if s.is_new():
 new_songs.append(s)
 else:
 old_songs.append(s)

Which works, but is klunky compared to the two-liner above. This seems like a common enough thing that I was expecting to find something in itertools which did this. I'm thinking something along the lines of:

matches, non_matches = isplit(lambda s: s.is_new, songs)

Does such a thing exist?

0 votes

I want to use python for creating dynamic database driven websites. and I don't want to use existing web frameworks for my work. I am learning things so I wont feel lazy to write all the code myself because I want to learn.

could anyone suggest me any books/site from where I can start. I want to follow MVC architecture. so please suggest me some links/book or anything

+1 vote

The source code:

for i in range(8):
 n = input()

When we run it, consider the numbers below is the user input,

1
2
3
(and so forth)

my question, can i make it in just a single line like,

1 2 3 (and so forth)
+2 votes

Is it possible to say to a BufferedReader stream "give me all the bytes you have available in the buffer, or do one OS call and give me everything you get back"? The problem is that the "number of bytes" argument to read1() isn't optional, so I can't do available_bytes = fd.read1().

I need this because I want to decode the returned bytes from UTF-8, and I *might* get a character split across the boundary of any arbitrary block size I choose. (I'm happy to ignore the possibility that the *source* did a flush part-way through a character).

I don't really want to have to do incremental encoding if I can avoid it - it looks hard...


Useful Links with Similar Problem
Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#470/147, 3rd Floor, 5th Main,
HSR Layout Sector 7,
Bangalore - 560102,
Karnataka INDIA.
QUERY HOME
...