我要转换
list_int=['(2,3)','(3,4)']
到整数坐标点:
[(2, 3), (4, 8)]
我尝试了这段代码
list_int=['(2,3)','(3,4)']
for x in list_int:
con= int(x0 for x in list_int)
print con
我收到此错误
TypeError: int() argument must be a string or a number, not 'generator'
我是 Python 新手。
最佳答案
您可以使用ast.literal_eval :
from ast import literal_eval
list_int=['(2,3)','(3,4)']
converted = [literal_eval(tup) for tup in list_int]
# [(2, 3), (3, 4)]
来自 ast.literal_eval
的文档:
Safely evaluate an expression node or a string containing a Python literal or container display. The string or node provided may only consist of the following Python literal structures: strings, bytes, numbers, tuples, lists, dicts, sets, booleans, and None.
This can be used for safely evaluating strings containing Python values from untrusted sources without the need to parse the values oneself. It is not capable of evaluating arbitrarily complex expressions, for example involving operators or indexing.
关于python - 如何将表示元组的字符串列表转换为整数元组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52571612/