简体   繁体   English

将字符串列表转换为整数元组

[英]Convert list of strings into tuple of integers

I need to convert a string of 2 items (separated by a comma) to integers.我需要将 2 个项目的字符串(用逗号分隔)转换为整数。 Also need to switch the list of lists into a tuple of tuples.还需要将列表列表切换为元组的元组。

from:从:

[['0,0'], ['0,1'], ['1,-1'], ['1,0']]

to:至:

((0,0), (0,1), (1,-1), (1,0))

How can I do that?我怎样才能做到这一点?

Thanks.谢谢。

You can use mix of map and tuple with list comprehension as:您可以将 map 和具有列表理解的元组混合使用:

x = [['0,0'], ['0,1'], ['1,-1'], ['1,0']]
x = tuple([tuple(map(int, elt[0].split(','))) for elt in x])
print(x)

Output: Output:

((0, 0), (0, 1), (1, -1), (1, 0))

Explanation: map is used to convert the elements to int on the output of split which is the elements of the inner list.说明: map用于将元素转换为intsplit是内部列表的元素。 The tuple is used to convert the types to tuple as required. tuple用于根据需要将类型转换为tuple

You can use the standard "accumulator pattern":您可以使用标准的“累加器模式”:

# Initialize an accumulator
outer_tuple = tuple()

for inner_list in outer_list:
    a, b = inner_list[0].split(",")
    a, b = int(a), int(b)
    outer_tuple += ((a, b),)

First, inner_list[0] gives the string inside the inner list, eg: "0,1" .首先, inner_list[0]给出内部列表中的字符串,例如: "0,1" Next, .split(",") splits that string on the comma character, which produces a list of strings: ["0", "1"] .接下来, .split(",")将该字符串拆分为逗号字符,从而生成一个字符串列表: ["0", "1"] Then, a, b =... "unpacks" the list into the variables a and b .然后, a, b =...将列表“解包”到变量ab中。 Finally, we cast a and b to int and add it as a tuple of tuples to the outer_tuple (our accumulator).最后,我们将ab转换为int并将其作为元组的元组添加到outer_tuple (我们的累加器)。

The reason we need to do ((a, b),) is because when you add two tuples, you're really just doing a union, or in other words, you're taking the elements from both tuples and creating a new tuple with all the elements:我们需要做((a, b),)的原因是因为当你添加两个元组时,你实际上只是在做一个联合,或者换句话说,你从两个元组中获取元素并创建一个新元组包含所有元素:

>>> x = (1, 2)
>>> y = (3, 4)
>>> x + y
(1, 2, 3, 4)

But that's not what we want.但这不是我们想要的。 We want a tuple of tuples.我们想要一个元组的元组。 Watch what happens:观察会发生什么:

>>> x += y
>>> x
(1, 2, 3, 4)
>>> x += ((5, 6),)
>>> x
(1, 2, 3, 4, (5, 6))

Hope this helps:希望这可以帮助:

First, we will make a list for the output, then we will iterate through every element in the input list and then make a tupled version of it, the tupled version would be made by spliting the string by the comma and then use the map function to get a tupled version which would have int type rather then string. First, we will make a list for the output, then we will iterate through every element in the input list and then make a tupled version of it, the tupled version would be made by spliting the string by the comma and then use the map function获得一个具有 int 类型而不是字符串的元组版本。 Then append that to the output List.然后 append 到 output 列表。 At last convert the output list into a tuple.最后将 output 列表转换为元组。

inputGiven = [['0,0'], ['0,1'], ['1,-1'], ['1,0']]
outputGivenInList = []
for i in inputGiven:
    tupledVersion = tuple(map(int, i[0].split(",")))
    outputGivenInList.append(tupledVersion)
finalOutput = tuple(outputGivenInList)
print(finalOutput)

A simple way:一个简单的方法:

>>> tmp = tuple(eval(str(li).replace('\'', '')))
>>> ans = (tuple(tmp[i]) for i in range(4))
>>> ans
<generator object <genexpr> at 0x000001754697D8C8>
>>> tuple(ans)
((0, 0), (0, 1), (1, -1), (1, 0))

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

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