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
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
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,
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
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
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.
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
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
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
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
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
[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:
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
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
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
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
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.
[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
[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.
[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
[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:
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
--
[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
[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?
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
[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
[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
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
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
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
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:
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
[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
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
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
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
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
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
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
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
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
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
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
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))
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,
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
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
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
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
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
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
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
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
201 - 253 of 253 matches
Mail list logo