如何将字符串转换为列表或元组 [英] how to convert string to list or tuple
问题描述
a ="(1,2,3)"
我想将a转换为元组:(1,2,3),但是元组(a)返回(''(') ',''''','','',
''2'','','',''3'','')'')not(1, 2,3)
5月26日,flyaflya< fl ****** @ gmail.com>写道:a ="(1,2,3)"
我想将a转换为元组:(1,2,3),但是元组(a)返回(' '('',''1','','',
''2'','','',''3'','')'')不是(1,2 ,3)
简答 - 使用eval()。
长答案 - *不要*使用eval除非你控制了你正在评估的字符串来源
。
-
干杯,
Simon B,
si***@brunningonline.net ,
http://www.brunningonline.net/simon/blog/
" flyaflya" < FL ****** @ gmail.com>写道:a ="(1,2,3)"
我想将a转换为元组:(1,2,3),但是元组(a)返回(' '('',''1','','',
''2'','','',''3'','')'')不是(1,2 ,3)
如果您信任来源,请使用
eval(a)
,如果你不信任它,你可以使用,比如说
元组(int(x)代表re.findall中的x(" \d +") ;,a))
或者,或许
元组([1:-1] .split中的x的int(x) (","))
或其中的一些变体。
(如果您使用的是2.4以上的版本,在里面添加括号
元组()调用:
元组([int(x)for x in a [1:-1] .split(") ;,),)
等
< / F>
2005年5月26日星期四19:53:38 +0800,flyaflya写道:
a ="( 1,2,3)
我希望将a转换为元组:(1,2,3),但是元组(a)返回(''('',''1'','',' ',
''''','','',''3'','')'')不是(1,2,3)
其他人已经提出了一些建议。这是其他一些。
你没有说输入字符串来自哪里。你控制了吗?b $ b吗?而不是使用:
String_Tuple_To_Real_Tuple("(1,2,3)")
你能创建元组吗?第一名?
a =(1,2,3)
第二个建议:如果你知道输入字符串总是在
表格(1,2,3)然后你可以这样做:
a ="(1,2,3)"
a = a [1:-1]#删除领先和尾随括号
a = a.split(",")#创建一个列表[" 1"," 2"" 3"](项目为字符串)
a = [int(x)for x in a]#创建一个列表[1,2,3](项目是整数)
a = tuple(a)#cverts to a tuple
或作为单行代码:
a ="(1,2,3)"
a = tuple([int(x)for x in a [1:-1] .split(",")])
最重要的是,将逻辑包装在一个函数定义与一些
错误检查:
def String_Tuple_To_Real_Tuple(s):
"""返回a来自一个看起来像元组的字符串的整数元组。""
如果不是s:
return()
if (s [0] =="(")and s [-1] ==")"):
s = s [1:-1]
else:
引发ValueError(缺少括号(s)in string。")
return tuple([int(x)for x in s.split(",")])
希望这个帮助,
-
史蒂文。
a = "(1,2,3)"
I want convert a to tuple:(1,2,3),but tuple(a) return (''('', ''1'', '','',
''2'', '','', ''3'', '')'') not (1,2,3)
On 5/26/05, flyaflya <fl******@gmail.com> 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 evaluating.
--
Cheers,
Simon B,
si***@brunningonline.net,
http://www.brunningonline.net/simon/blog/
"flyaflya" <fl******@gmail.com> 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
tuple(int(x) for x in a[1:-1].split(","))
or some variation thereof.
(if you''re using a version older than 2.4, add brackets inside
the tuple() call:
tuple([int(x) for x in a[1:-1].split(",")])
etc.
</F>
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 control
it? Instead of using:
String_Tuple_To_Real_Tuple("(1,2,3)")
can you just create the tuple in the first place?
a = (1, 2, 3)
Second suggestion: if you know that the input string will ALWAYS be in the
form "(1,2,3)" then you can do this:
a = "(1,2,3)"
a = a[1:-1] # deletes leading and trailing parentheses
a = a.split(",") # creates a list ["1", "2", "3"] (items are strings)
a = [int(x) for x in a] # creates a list [1, 2, 3] (items are integers)
a = tuple(a) # coverts to a tuple
or as a one-liner:
a = "(1,2,3)"
a = tuple([int(x) for x in a[1:-1].split(",")])
Best of all, wrap your logic in a function definition with some
error-checking:
def String_Tuple_To_Real_Tuple(s):
"""Return a tuple of ints from a string that looks like a tuple."""
if not s:
return ()
if (s[0] == "(") and s[-1] == ")"):
s = s[1:-1]
else:
raise ValueError("Missing bracket(s) in string.")
return tuple([int(x) for x in s.split(",")])
Hope this helps,
--
Steven.
这篇关于如何将字符串转换为列表或元组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!