简体   繁体   English

将str元组列表转换为int元组列表?

[英]Convert list of str tuples into list of int tuples?

I have a list of tuples which I would like to import into a dictionary. 我有一个元组列表,我想将其导入字典中。 Those tuples consist of two strings. 这些元组由两个字符串组成。 I would like to convert those tuples into integers. 我想将这些元组转换为整数。 ie

myList = ['0,1', '0,2', '0,3']

The resulting list should be: 结果列表应为:

resList = [(0,1), (0,2), (0,3)]

I have tried using: 我试过使用:

list(map(int, myList))

But that doesn't seem to work. 但这似乎不起作用。

You could use eval function to get the tuples, like this. 您可以使用eval函数来获取元组,如下所示。 Works in both python2, and python3. 在python2和python3中均可使用。

>>> myList = ['0,1', '0,2', '0,3']
>>> list(map(eval, myList))
[(0, 1), (0, 2), (0, 3)]

You also need to split your strings by ',' . 您还需要用','分割字符串。

>>> [tuple(map(int, s.split(','))) for s in myList]                                                                               
[(0, 1), (0, 2), (0, 3)]

Another option is to use the csv module. 另一种选择是使用csv模块。

>>> import csv                                                                                                                    
>>> list(csv.reader(myList))                                                                                                      
[['0', '1'], ['0', '2'], ['0', '3']]
>>> [tuple(map(int, row)) for row in csv.reader(myList)]                                                                          
[(0, 1), (0, 2), (0, 3)]

If it's not terribly important the the inner elements of the result are tuples and that they are of type int , we can simplify the csv solution to 如果不是很重要,则结果的内部元素是元组并且它们是int类型的,我们可以将csv解决方案简化为

>>> list(csv.reader(myList, quoting=csv.QUOTE_NONNUMERIC))                                                                        
[[0.0, 1.0], [0.0, 2.0], [0.0, 3.0]]

~edit~ 〜编辑〜

thelogicalkoan's answer reminded me of ast.literal_eval which provides the most elegant (and safe) solution here. theologickoan的回答使我想起了ast.literal_eval ,它在这里提供了最优雅(和最安全)的解决方案。

>>> from ast import literal_eval                                                                                                  
>>> [literal_eval(x) for x in myList]                                                                                             
[(0, 1), (0, 2), (0, 3)]

An alternative solution is to use ast.literal_eval (instead of eval which can be considered unsafe): 另一种解决方案是使用ast.literal_eval (而不是可以认为不安全的eval ):

>>> import ast
>>> ast.literal_eval('0,1')
(0, 1)

So, you can do: 因此,您可以执行以下操作:

>>> my_list = ['0,1', '0,2', '0,3']
>>> result = [ast.literal_eval(x) for x in my_list]
>>> result
[(0, 1), (0, 2), (0, 3)]

str.partition can be used to do this. str.partition可用于执行此操作。

>>> [(int(x), int(y)) for x, _, y in (z.partition(',') for z in myList)]
[(0, 1), (0, 2), (0, 3)]

The generator expression (z.partition(',') for z in myList) produces the output of str.partition on each element of the list, and then we call int on the parts that we require. 发电机表达(z.partition(',') for z in myList)产生的输出str.partition列表中的每个元素,然后我们称之为int上,我们所需要的部分。

This is a different option: 这是一个不同的选项:

myList = ['0,1', '0,2', '0,3']

def tSet(list): 
    return tuple([int(i) for i in list])

resList  = [tSet(i.split(",")) for i in myList]

print(resList) #Output: [(0, 1), (0, 2), (0, 3)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM