Re: String To List

2008-03-17 Thread Jeff Schwab
Girish wrote: I have a string a = ['xyz', 'abc'].. I would like to convert it to a list with elements 'xyz' and 'abc'. Is there any simple solution for this?? Do you want: (1) Specifically to vivify lists formatted as in your example? If so, why? (2) To save and restore arbitrary python

Re: String To List

2008-03-17 Thread castironpi
On Mar 17, 10:26 am, Jeff Schwab [EMAIL PROTECTED] wrote: Girish wrote: I have a string a = ['xyz', 'abc'].. I would like to convert it to a list with elements 'xyz' and 'abc'. Is there any simple solution for this?? Do you want: (1) Specifically to vivify lists formatted as in your

Re: difference between string and list

2007-02-28 Thread Gabriel Genellina
En Tue, 27 Feb 2007 18:00:25 -0300, lincoln rutledge [EMAIL PROTECTED] escribió: Okay, I actually have those pages up in my browser. I found the string methods: http://docs.python.org/lib/string-methods.html But I am having trouble finding the same information for lists... On that page,

difference between string and list

2007-02-27 Thread lincoln rutledge
I'm having trouble figuring out the difference between a string and a list. I know that: string = foo bar is a list of characters, foo bar, and string[0] is f. while: list = [foo, bar] and list[0] is foo. strings have methods like string.count(f) returns 1. What methods do lists have

Re: difference between string and list

2007-02-27 Thread Paul Rubin
lincoln rutledge [EMAIL PROTECTED] writes: strings have methods like string.count(f) returns 1. What methods do lists have? Is it a similar class to string? Strings and lists are similar but not the same. dir(string) will show you the methods available for strings. dir(list) will show you

Re: difference between string and list

2007-02-27 Thread skip
lincoln strings have methods like string.count(f) returns 1. What lincoln methods do lists have? Is it a similar class to string? Similar in some ways, different in others. Some things to play with: 1. At an interpreter prompt, execute: help() help([]) 2.

Re: difference between string and list

2007-02-27 Thread Bruno Desthuilliers
lincoln rutledge a écrit : I'm having trouble figuring out the difference between a string and a list. ['R', 'e', 'a', 'l', 'l', 'y', ' ', '?', ' ', 'S', 'e', 'e', 'm', 's', ' ', 'q', 'u', 'i', 't', 'e', ' ', 'o', 'b', 'v', 'i', 'o', 'u', 's', ' ', 't', 'o', ' ', 'm', 'e', '.'] I know

Re: difference between string and list

2007-02-27 Thread lincoln rutledge
Hi Skip, Okay, I actually have those pages up in my browser. I found the string methods: http://docs.python.org/lib/string-methods.html But I am having trouble finding the same information for lists... Thanks I will look into it some more, Lincoln [EMAIL PROTECTED] 02/27/07 3:28 PM

Re: Convert String to list of chars

2007-01-27 Thread Vlad Dogaru
On Jan 27, 9:18 am, Neil Cerutti [EMAIL PROTECTED] wrote: On 2007-01-27, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: How can I convert a string to a char list? for example hello -- ['h','e','l','l','o'] I have been searching but I can't find my answers list(hello) Wow, I've been using

Convert String to list of chars

2007-01-26 Thread juanefren
How can I convert a string to a char list? for example hello -- ['h','e','l','l','o'] I have been searching but I can't find my answers thanks -- Juan Efrén Castillo Encinas -- http://mail.python.org/mailman/listinfo/python-list

Re: Convert String to list of chars

2007-01-26 Thread Neil Cerutti
On 2007-01-27, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: How can I convert a string to a char list? for example hello -- ['h','e','l','l','o'] I have been searching but I can't find my answers list(hello) -- http://mail.python.org/mailman/listinfo/python-list

Re: sending string or list to a function

2006-12-05 Thread Peter Otten
[EMAIL PROTECTED] wrote: Or, just always send the function a list. If you have one string, send it a list containing that one string. Or, if a single string is more common and the lists are short or generated only for the function call, have the function accept a variable number of arguments:

sending string or list to a function

2006-12-04 Thread manstey
Hi, Is there a neat way to write a function that can receive either a string or a list of strings, and then if it receives a string it manipulates that, otherwise it manipulates each string in the list? That is, rather than having to send a list of one member MyFunction(['var1']), I can send

Re: sending string or list to a function

2006-12-04 Thread Farshid Lashkari
manstey wrote: Is there a neat way to write a function that can receive either a string or a list of strings, and then if it receives a string it manipulates that, otherwise it manipulates each string in the list? The following code shows one way you can accomplish this. I don't consider

Re: sending string or list to a function

2006-12-04 Thread Gabriel Genellina
At Monday 4/12/2006 21:20, manstey wrote: Is there a neat way to write a function that can receive either a string or a list of strings, and then if it receives a string it manipulates that, otherwise it manipulates each string in the list? That is, rather than having to send a list of one

Re: sending string or list to a function

2006-12-04 Thread [EMAIL PROTECTED]
Or, just always send the function a list. If you have one string, send it a list containing that one string. Cheers, -T -- http://mail.python.org/mailman/listinfo/python-list

Re: string to list of numbers conversion

2006-11-10 Thread Tim Williams
On 5 Nov 2006 04:34:32 -0800, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: Hi, I have a string '((1,2), (3,4))' and I want to convert this into a python tuple of numbers. But I do not want to use eval() because I do not want to execute any code in that string and limit it to list of numbers.

Re: string to list of numbers conversion

2006-11-08 Thread henning
[EMAIL PROTECTED] skrev: Hi, I have a string '((1,2), (3,4))' and I want to convert this into a python tuple of numbers. I think your question is deeper and more natural than is clear from the many recepies given so far in this thread, so I'll take on another point of view, From a language

Re: string to list of numbers conversion

2006-11-06 Thread Frederic Rentsch
[EMAIL PROTECTED] wrote: Hi, I have a string '((1,2), (3,4))' and I want to convert this into a python tuple of numbers. But I do not want to use eval() because I do not want to execute any code in that string and limit it to list of numbers. Is there any alternative way? Thanks.

Re: string to list of numbers conversion

2006-11-06 Thread Paul McGuire
[EMAIL PROTECTED] [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] Hi, I have a string '((1,2), (3,4))' and I want to convert this into a python tuple of numbers. But I do not want to use eval() because I do not want to execute any code in that string and limit it to list of

Re: string to list of numbers conversion

2006-11-06 Thread Fredrik Lundh
[EMAIL PROTECTED] wrote: I have a string '((1,2), (3,4))' and I want to convert this into a python tuple of numbers. But I do not want to use eval() because I do not want to execute any code in that string and limit it to list of numbers. here's yet another approach:

string to list of numbers conversion

2006-11-05 Thread [EMAIL PROTECTED]
Hi, I have a string '((1,2), (3,4))' and I want to convert this into a python tuple of numbers. But I do not want to use eval() because I do not want to execute any code in that string and limit it to list of numbers. Is there any alternative way? Thanks. Suresh --

Re: string to list of numbers conversion

2006-11-05 Thread Gerard Flanagan
[EMAIL PROTECTED] wrote: Hi, I have a string '((1,2), (3,4))' and I want to convert this into a python tuple of numbers. But I do not want to use eval() because I do not want to execute any code in that string and limit it to list of numbers. Is there any alternative way? Python 2.5

Re: string to list of numbers conversion

2006-11-05 Thread Peter Otten
[EMAIL PROTECTED] wrote: I have a string '((1,2), (3,4))' and I want to convert this into a python tuple of numbers. But I do not want to use eval() because I do not want to execute any code in that string and limit it to list of numbers. Is there any alternative way?

Re: string to list of numbers conversion

2006-11-05 Thread [EMAIL PROTECTED]
Peter, Thanks. This recipe fails when negative numbers are used. safe_eval('(12, -12)') *** Unsafe_Source_Error: Line 1. Unsupported source construct: compiler.ast.UnarySub But, I think it could be easily fixed for somebody who understands the script. Can somebody help. Thanks. Suresh Peter

Re: string to list of numbers conversion

2006-11-05 Thread bearophileHUGS
[EMAIL PROTECTED] wrote: Hi, I have a string '((1,2), (3,4))' and I want to convert this into a python tuple of numbers. But I do not want to use eval() because I do not want to execute any code in that string and limit it to list of numbers. Is there any alternative way? This is a

Re: string to list of numbers conversion

2006-11-05 Thread Peter Otten
[EMAIL PROTECTED] wrote: This recipe fails when negative numbers are used. safe_eval('(12, -12)') *** Unsafe_Source_Error: Line 1. Unsupported source construct: compiler.ast.UnarySub But, I think it could be easily fixed for somebody who understands the script. I think that somebody

Re: [ python-Bugs-1215887 ] String and list methods deeply hidden

2005-12-24 Thread Chuck Rhode
SourceForge.net wrote this on Mon, Jun 06, 2005 at 11:16:19AM -0700. My reply is below. The way the docs are written makes perfect sense to me ~ now ~ but I too had difficulty navigating them at first, particularly with finding the methods of sequence types from the TOC. Eventually, I must

Re: Pattern matching with string and list

2005-12-13 Thread BartlebyScrivener
Taking you literally, I'm not sure you need regex. If you know or can find position n, then can't you just: sentence = the color is $red patterns = [blue,red,yellow] pos = sentence.find($) for x in patterns: if x==sentence[pos+1:]: print x, pos+1 But maybe I'm oversimplifying. rpd

Re: Pattern matching with string and list

2005-12-13 Thread BartlebyScrivener
Even without the marker, can't you do: sentence = the fabric is red colors = [red, white, blue] for color in colors: if (sentence.find(color) 0): print color, sentence.find(color) -- http://mail.python.org/mailman/listinfo/python-list

Re: Pattern matching with string and list

2005-12-13 Thread Brett g Porter
BartlebyScrivener wrote: Even without the marker, can't you do: sentence = the fabric is red colors = [red, white, blue] for color in colors: if (sentence.find(color) 0): print color, sentence.find(color) That depends on whether you're only looking for whole words:

Pattern matching with string and list

2005-12-12 Thread olaufr
Hi, I'd need to perform simple pattern matching within a string using a list of possible patterns. For example, I want to know if the substring starting at position n matches any of the string I have a list, as below: sentence = the color is $red patterns = [blue,red,yellow] pos = sentence.find

Re: Pattern matching with string and list

2005-12-12 Thread Michael Spencer
[EMAIL PROTECTED] wrote: Hi, I'd need to perform simple pattern matching within a string using a list of possible patterns. For example, I want to know if the substring starting at position n matches any of the string I have a list, as below: sentence = the color is $red patterns

Re: Pattern matching with string and list

2005-12-12 Thread Tom Anderson
On Mon, 12 Dec 2005 [EMAIL PROTECTED] wrote: I'd need to perform simple pattern matching within a string using a list of possible patterns. For example, I want to know if the substring starting at position n matches any of the string I have a list, as below: sentence = the color is $red

[ python-Bugs-1215887 ] String and list methods documentation deeply hidden

2005-06-14 Thread SourceForge.net
thread, including the initial issue submission, for this request, not just the latest update. Category: Documentation Group: Python 2.4 Status: Closed Resolution: Fixed Priority: 5 Submitted By: Reinhold Birkenfeld (birkenfeld) Assigned to: Nobody/Anonymous (nobody) Summary: String and list methods

[ python-Bugs-1215887 ] String and list methods deeply hidden

2005-06-06 Thread SourceForge.net
of the comment thread, including the initial issue submission, for this request, not just the latest update. Category: Documentation Group: Python 2.4 Status: Open Resolution: None Priority: 5 Submitted By: Reinhold Birkenfeld (birkenfeld) Assigned to: Nobody/Anonymous (nobody) Summary: String and list

Re: how to convert string to list or tuple

2005-06-02 Thread Duncan Booth
Ruud de Jong wrote: Steven Bethard schreef: But unless the person eval-ing your code *only* writes immaculate code I can see that you can probably screw them. ;) I wonder why __subclasses__ isn't a restricted attribute... Is it ever used for something that isn't evil? ;) STeVe

Re: how to convert string to list or tuple

2005-06-01 Thread Duncan Booth
Steven Bethard wrote: Interestingly, I don't seem to be able to create a file object as a class attribute in restricted mode: py class C(object): ... def __init__(self): ... self.f = file('temp.txt', 'w') ... py eval('''[ cls for cls in

Re: how to convert string to list or tuple

2005-06-01 Thread Fuzzyman
flyaflya wrote: a = (1,2,3) I want convert a to tuple:(1,2,3),but tuple(a) return ('(', '1', ',', '2', ',', '3', ')') not (1,2,3) Probably a bit late... but there's always listquote - It's part of the pythonutils module. http://www.voidspace.org.uk/python/pythonutils.html It will turn

Re: how to convert string to list or tuple

2005-06-01 Thread Steven Bethard
Duncan Booth wrote: Steven Bethard wrote: Interestingly, I don't seem to be able to create a file object as a class attribute in restricted mode: py class C(object): ... def __init__(self): ... self.f = file('temp.txt', 'w') ... py eval('''[ cls for cls in

Re: how to convert string to list or tuple

2005-06-01 Thread Ruud de Jong
Steven Bethard schreef: But unless the person eval-ing your code *only* writes immaculate code I can see that you can probably screw them. ;) I wonder why __subclasses__ isn't a restricted attribute... Is it ever used for something that isn't evil? ;) STeVe Completely off topic, but I

Re: how to convert string to list or tuple

2005-05-31 Thread Duncan Booth
Steven Bethard wrote: Duncan Booth wrote: any new style class you have defined and call any of its methods with whatever arguments I wish. Any new style class that I've defined? Or just any one I pass in as part of dict(__builtins__=None, ...)? If the former, could you elaborate? If

Re: how to convert string to list or tuple

2005-05-31 Thread Steven Bethard
Duncan Booth wrote: e.g. Assuming that the MyDatabase class does something nasty to a file: class MyDatabase(object): def __init__(self, filename): self.filename = filename def initialise(self): print Splat %s % self.filename eval('''[ cls for cls in

Re: how to convert string to list or tuple

2005-05-30 Thread Duncan Booth
Steven Bethard wrote: Have you tried giving it the string '__import__(os).system(rm -rf *)'? [Don't try that at home children!] But you can try it at home if you set __builtins__ to something other than the default: py eval(__import__(os).system('echo hello'), dict(__builtins__=None))

Re: how to convert string to list or tuple

2005-05-30 Thread Steven Bethard
Duncan Booth wrote: Steven Bethard wrote: But you can try it at home if you set __builtins__ to something other than the default: py eval(__import__(os).system('echo hello'), dict(__builtins__=None)) Traceback (most recent call last): File interactive input, line 1, in ? File string,

Re: how to convert string to list or tuple

2005-05-29 Thread Steven D'Aprano
On Thu, 26 May 2005 19:53:38 +0800, flyaflya wrote: a = (1,2,3) I want convert a to tuple:(1,2,3),but tuple(a) return ('(', '1', ',', '2', ',', '3', ')') not (1,2,3) Others have already given some suggestions. Here are some others. You didn't say where the input string a came from. Do you

Re: how to convert string to list or tuple

2005-05-29 Thread Dan Bishop
Simon Brunning wrote: On 5/26/05, flyaflya [EMAIL PROTECTED] wrote: a = (1,2,3) I want convert a to tuple:(1,2,3),but tuple(a) return ('(', '1', ',', '2', ',', '3', ')') not (1,2,3) Short answer - use eval(). Long answer - *don't* use eval unless you are in control of the source of the

Re: how to convert string to list or tuple

2005-05-29 Thread Duncan Booth
Dan Bishop wrote: Simon Brunning wrote: [...] Or if you do use eval, don't give it access to any names. [...] os.system(rm -rf *) Traceback (most recent call last): File stdin, line 1, in ? File string, line 0, in ? NameError: name 'os' is not defined Have you tried giving it

Re: how to convert string to list or tuple

2005-05-29 Thread John Roth
Duncan Booth [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] Dan Bishop wrote: Simon Brunning wrote: [...] Or if you do use eval, don't give it access to any names. [...] os.system(rm -rf *) Traceback (most recent call last): File stdin, line 1, in ? File string, line

Re: how to convert string to list or tuple

2005-05-29 Thread Steven Bethard
Duncan Booth wrote: Dan Bishop wrote: Or if you do use eval, don't give it access to any names. [snip] os.system(rm -rf *) Traceback (most recent call last): File stdin, line 1, in ? File string, line 0, in ? NameError: name 'os' is not defined Have you tried giving it the string

how to convert string to list or tuple

2005-05-26 Thread flyaflya
a = (1,2,3) I want convert a to tuple:(1,2,3),but tuple(a) return ('(', '1', ',', '2', ',', '3', ')') not (1,2,3) -- http://mail.python.org/mailman/listinfo/python-list

Re: how to convert string to list or tuple

2005-05-26 Thread Simon Brunning
On 5/26/05, flyaflya [EMAIL PROTECTED] wrote: a = (1,2,3) I want convert a to tuple:(1,2,3),but tuple(a) return ('(', '1', ',', '2', ',', '3', ')') not (1,2,3) Short answer - use eval(). Long answer - *don't* use eval unless you are in control of the source of the string that you are

Re: how to convert string to list or tuple

2005-05-26 Thread Fredrik Lundh
flyaflya [EMAIL PROTECTED] wrote: a = (1,2,3) I want convert a to tuple:(1,2,3),but tuple(a) return ('(', '1', ',', '2', ',', '3', ')') not (1,2,3) if you trust the source, use eval(a) if you don't trust it, you can use, say tuple(int(x) for x in re.findall(\d+, a)) or, perhaps

<    1   2   3