Re: [Tutor] string to list

2010-08-05 Thread Huy Ton That
Speaking of which, is there some place you or anyone can recommend on picking up all the constructs for list comprehension & generator comprehension. I've perused the python.org documents and have just been building them according to example. How can I pick up more advanced usage? I mostly catch

Re: [Tutor] string to list

2010-08-05 Thread Walter Prins
Just a minor related/tangential suggestion -- O'Reilly had a stand at the recent EuroPython conference I attended and I was paging through "Bioinformatics Programming using Python", Book reference: http://oreilly.com/catalog/9780596154516 Just thought I'd mention it as it seems relevant to the cur

Re: [Tutor] string to list

2010-08-05 Thread Alan Gauld
"Vikram K" wrote Suppose i have this string: z = 'AT/CG' How do i get this list: zlist = ['A','T/C','G'] There are lots of ways and it really needs a tighter specification of yhow you split. What would "AT/C/DGH" give for example? But in general there are several approaches, I suspect r

Re: [Tutor] string to list

2010-08-05 Thread Thomas C. Hicks
On Thu, 5 Aug 2010 03:40:55 -0400 Sander Sweers wrote: > On 5 August 2010 06:38, Vikram K wrote: > > Suppose i have this string: > > z = 'AT/CG' > > > > How do i get this list: > > > > zlist = ['A','T/C','G'] > > If you know the format of the string is always the same you can do > something lik

Re: [Tutor] string to list

2010-08-05 Thread Sander Sweers
On 5 August 2010 06:38, Vikram K wrote: > Suppose i have this string: > z = 'AT/CG' > > How do i get this list: > > zlist = ['A','T/C','G'] If you know the format of the string is always the same you can do something like this. This fails when you have strings that do not have the '/' in the midd

Re: [Tutor] string to list

2010-08-05 Thread James Mills
On Thu, Aug 5, 2010 at 2:38 PM, Vikram K wrote: > Suppose i have this string: > z = 'AT/CG' > > How do i get this list: > > zlist = ['A','T/C','G'] >>> import re >>> z = 'AT/CG' >>> [x for x in re.split("([A-Z]\/[A-Z])|([A-Z])", z) if x] ['A', 'T/C', 'G'] >>> cheers James -- -- James Mills --

[Tutor] string to list

2010-08-04 Thread Vikram K
Suppose i have this string: z = 'AT/CG' How do i get this list: zlist = ['A','T/C','G'] ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor

Re: [Tutor] string to list

2010-02-11 Thread Kent Johnson
On Thu, Feb 11, 2010 at 8:37 AM, Stefan Behnel wrote: > Kent Johnson, 11.02.2010 14:16: >> On Thu, Feb 11, 2010 at 4:44 AM, Stefan Behnel wrote: >> >>> 2) given that you have lists as items in the 'data' list, it's enough to >>> call sort() once, as the comparison of lists is defined as the compar

Re: [Tutor] string to list

2010-02-11 Thread Stefan Behnel
Kent Johnson, 11.02.2010 14:16: > On Thu, Feb 11, 2010 at 4:44 AM, Stefan Behnel wrote: > >> 2) given that you have lists as items in the 'data' list, it's enough to >> call sort() once, as the comparison of lists is defined as the comparison >> of each item to the corresponding item of the other

Re: [Tutor] string to list

2010-02-11 Thread Kent Johnson
On Thu, Feb 11, 2010 at 4:44 AM, Stefan Behnel wrote: > 2) given that you have lists as items in the 'data' list, it's enough to > call sort() once, as the comparison of lists is defined as the comparison > of each item to the corresponding item of the other list. If you want to > sort based on t

Re: [Tutor] string to list

2010-02-11 Thread Stefan Behnel
Owain Clarke, 10.02.2010 17:57: > data.sort(key=lambda x:x[0]) > data.sort(key=lambda x:x[1]) Two things to note: 1) you can use the operator module, specifically operator.itemgetter 2) given that you have lists as items in the 'data' list, it's enough to call sort() once, as the comparison of l

Re: [Tutor] string to list

2010-02-11 Thread Alan Plum
On Mi, 2010-02-10 at 16:57 +, Owain Clarke wrote: > I would love a really clear explanation of lambda! Generally, lambdas are anonymous functions. In Python specifically, however, they are limited to simple expressions (i.e. only what you can put on the right-hand side of an assignment). my

Re: [Tutor] string to list

2010-02-11 Thread Alan Gauld
"Owain Clarke" wrote I would love a really clear explanation of lambda! lambda returns an anonymous function - a function without a name. When we define a function normally we use something like: def square(x): return x*x That creates a function that has the name square We can do the

Re: [Tutor] string to list

2010-02-10 Thread Owain Clarke
With thanks to all who made suggestions, this was what I settled on f = open('testfile') #(testfile consisting of 2 columns of data, as per Kent's suggestion) data = [] for line in f: line_data = [int(x) for x in line.split()] data.append(line_data) data.sort(key=lambda x:x[0]) print "sort by f

Re: [Tutor] string to list

2010-02-10 Thread Owain Clarke
If you mean the external shell (say, "bash" under Linux or the DOS command line in Windows, or similar) then you can only input strings -- everything is a string in such shells. Yes, I meant capturing data from bash There are two ways to conv

Re: [Tutor] string to list

2010-02-10 Thread Stefan Behnel
Owain Clarke, 10.02.2010 14:32: >> You may want to add a little bit about your use case. Is that really >> the input you >> have to deal with? Where does it come from? Can you control the >> format? What do >> you want to do with the list you extract from the string? >> >> All of that may have an i

Re: [Tutor] string to list

2010-02-10 Thread Eike Welk
On Wednesday February 10 2010 14:32:52 Owain Clarke wrote: > My son was doing a statistics project in which he had to sort some data by > either one of two sets of numbers, representing armspan and height of a > group of children - a boring and painstaking job. I came across this > piece of cod

Re: [Tutor] string to list

2010-02-10 Thread Kent Johnson
On Wed, Feb 10, 2010 at 8:32 AM, Owain Clarke wrote: > My son was doing a statistics project in which he had to sort some data by > either one of two sets of numbers, representing armspan and height of a > group of children - a boring and painstaking job.  I came across this piece > of code:- > >

Re: [Tutor] string to list

2010-02-10 Thread Steven D'Aprano
On Thu, 11 Feb 2010 12:32:52 am Owain Clarke wrote: > My son was doing a statistics project in which he had to sort some > data by either one of two sets of numbers, representing armspan and > height of a group of children - a boring and painstaking job. I came > across this piece of code:- > > l

Re: [Tutor] string to list

2010-02-10 Thread Owain Clarke
Owain Clarke wrote: Please excuse the obviousness of my question (if it is), but I have searched the documentation for how to generate a list e.g. [(1,2), (3,4)] from a string "[(1,2), (3,4)]". I wonder if someone could point me in the right direction. Many thanks Owain Clarke Th

Re: [Tutor] string to list

2010-02-10 Thread Kent Johnson
On Wed, Feb 10, 2010 at 6:26 AM, Owain Clarke wrote: > Please excuse the obviousness of my question (if it is), but I have searched > the documentation for how to generate a list e.g. [(1,2), (3,4)] from a > string "[(1,2), (3,4)]". I wonder if someone could point me in the right > direction. Pyt

Re: [Tutor] string to list

2010-02-10 Thread Stefan Behnel
Owain Clarke, 10.02.2010 13:34: > I have solved it myself - must search more before posting! > > If anyone at my kind of level is interested:- > mystring = "[(1,2), (3,4)]" mylist = eval(mystring) mylist > [(1,2), (3,4)] type(mylist) > As others have pointed out, this may or

Re: [Tutor] string to list

2010-02-10 Thread Owain Clarke
I have solved it myself - must search more before posting! If anyone at my kind of level is interested:- >>> mystring = "[(1,2), (3,4)]" >>> mylist = eval(mystring) >>> mylist [(1,2), (3,4)] >>> type(mylist) Thanks Owain Clarke wrote: Please excuse the obviousness of my question (if it is), b

Re: [Tutor] string to list

2010-02-10 Thread Stefan Behnel
Owain Clarke, 10.02.2010 12:26: > Please excuse the obviousness of my question (if it is), but I have > searched the documentation for how to generate a list e.g. [(1,2), > (3,4)] from a string "[(1,2), (3,4)]". I wonder if someone could point > me in the right direction. You may want to add a lit

Re: [Tutor] string to list

2010-02-10 Thread Jose Amoreira
On Wednesday 10 February 2010 11:26:25 am Owain Clarke wrote: > Please excuse the obviousness of my question (if it is), but I have > searched the documentation for how to generate a list e.g. [(1,2), > (3,4)] from a string "[(1,2), (3,4)]". I wonder if someone could point > me in the right directi

Re: [Tutor] string to list

2010-02-10 Thread Christian Witts
Owain Clarke wrote: Please excuse the obviousness of my question (if it is), but I have searched the documentation for how to generate a list e.g. [(1,2), (3,4)] from a string "[(1,2), (3,4)]". I wonder if someone could point me in the right direction. Many thanks Owain Clarke _

Re: [Tutor] string to list

2010-02-10 Thread spir
On Wed, 10 Feb 2010 11:26:25 + Owain Clarke wrote: > Please excuse the obviousness of my question (if it is), but I have > searched the documentation for how to generate a list e.g. [(1,2), > (3,4)] from a string "[(1,2), (3,4)]". I wonder if someone could point > me in the right direction

[Tutor] string to list

2010-02-10 Thread Owain Clarke
Please excuse the obviousness of my question (if it is), but I have searched the documentation for how to generate a list e.g. [(1,2), (3,4)] from a string "[(1,2), (3,4)]". I wonder if someone could point me in the right direction. Many thanks Owain Clarke __

Re: [Tutor] String to list conversion

2009-02-24 Thread Lie Ryan
> name like "foo" to be changed Nitpick: "foo" is a string, not a name... ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor

Re: [Tutor] String to list conversion

2009-02-24 Thread Robert Berman
Or, you could do: In [1]: print list(raw_input('name please...')) name please...John ['J', 'o', 'h', 'n'] Robert Berman Kent Johnson wrote: On Tue, Feb 24, 2009 at 10:16 AM, Taylan Karaman wrote: Hello, I am a beginner. And I am trying to get a user input converted to a lis

Re: [Tutor] String to list conversion

2009-02-24 Thread Kent Johnson
On Tue, Feb 24, 2009 at 10:16 AM, Taylan Karaman wrote: > Hello, > > I am a beginner. And I am trying to get a user input converted to a list. > > print 'Enter your first name :' > firstname = raw_input() > > So if the user input is > > firstname = 'foo'    --->should become> > fir

Re: [Tutor] String to list conversion

2009-02-24 Thread عماد نوفل
On Tue, Feb 24, 2009 at 10:16 AM, Taylan Karaman wrote: > Hello, > > I am a beginner. And I am trying to get a user input converted to a list. > > print 'Enter your first name :' > firstname = raw_input() > > So if the user input is > > firstname = 'foo'--->should become> > fir

[Tutor] String to list conversion

2009-02-24 Thread Taylan Karaman
Hello, I am a beginner. And I am trying to get a user input converted to a list. print 'Enter your first name :' firstname = raw_input() So if the user input is firstname = 'foo'--->should become> firstlist['f','o','o'] thanks in advance ___

Re: [Tutor] String to List and back?

2005-07-02 Thread Chinook
Just the thing Light and Chuck - I thought there might be something simple (and I've used the join method before duh). Thanks, Lee C ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor

Re: [Tutor] String to List and back?

2005-07-02 Thread Chuck Allison
Hello Chinook, How about join(), as in ''.join(strlist) ? Saturday, July 2, 2005, 9:45:28 PM, you wrote: C> I'm missing something simple again. The simplified issue is: C> Python 2.4.1 (#2, Mar 31 2005, 00:05:10) C> >>> mystr = 'abc' C> # I can create a list of the string characters C>

Re: [Tutor] String to List and back?

2005-07-02 Thread Light
Hi, Lee. You could using: >>> bts = ''.join(strlist) Then you would get: >>> bts'abc'   Light   FOX.GIF Description: GIF image ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor

[Tutor] String to List and back?

2005-07-02 Thread Chinook
I'm missing something simple again. The simplified issue is: Python 2.4.1 (#2, Mar 31 2005, 00:05:10) >>> mystr = 'abc' # I can create a list of the string characters # with list comprehension >>> [c for c in mystr] ['a', 'b', 'c'] # Or just a simple builtin conversion function >>> l