Python:对2d数组(列表列表)中的元素执行操作的单行程序?

时间:2022-10-29 18:10:40

I have a list of lists, each containing a different number of strings. I'd like to (efficiently) convert these all to ints, but am feeling kind of dense, since I can't get it to work out for the life of me. I've been trying: newVals = [int(x) for x in [row for rows in values]]

我有一个列表列表,每个列表包含不同数量的字符串。我想(有效地)将这些全部转换为整数,但我感觉有点密集,因为我无法让它为我的生活而努力。我一直在尝试:newVals = [int(x)for x in [row for values in values]]

Where 'values' is the list of lists. It keeps saying that x is a list and can therefore not be the argument if int(). Obviously I'm doing something stupid here, what is it? Is there an accepted idiom for this sort of thing?

“值”是列表的列表。它一直说x是一个列表,因此如果是int()则不能成为参数。显然我在这里做了一些蠢事,这是什么?对于这种事情,有没有被接受的习语?

As usual, thanks a ton in advance.

像往常一样,提前感谢。

5 个解决方案

#1


9  

This leaves the ints nested

这使得int嵌套

[map(int, x) for x in values]

If you want them flattened, that's not hard either

如果你想让它们变平,那也不难

for Python3 map() returns an iterator. You could use

for Python3 map()返回一个迭代器。你可以用

[list(map(int, x)) for x in values]

but you may prefer to use the nested LC's in that case

但在这种情况下你可能更喜欢使用嵌套的LC

[[int(y) for y in x] for x in values]

#2


9  

How about:

怎么样:

>>> a = [['1','2','3'],['4','5','6'],['7','8','9']]
>>> [[int(j) for j in i] for i in a]
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

#3


1  

You simply use incorrect order and parenthesis - should be:

你只是使用不正确的顺序和括号 - 应该是:

inputVals = [['1','2','3'], ['3','3','2','2']]
[int(x) for row in inputVals for x in row]

Or if you need list of list at the output then:

或者如果您需要输出列表,那么:

map(lambda row: map(int, row), inputVals)

#4


0  

an ugly way is to use evalf:

一种丑陋的方式是使用evalf:

>>> eval(str(a).replace("'",""))
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

if you don't mind all your numbers in one array you could go:

如果你不介意一个数组中的所有数字,你可以去:

>>> a = [['1','2','3'],['4','5','6'],['7','8','9']]
>>> map(int,sum(a,[]))
[1, 2, 3, 4, 5, 6, 7, 8, 9]

#5


0  

Another workaround

另一种解决方法

a = [[1, 2, 3], [7, 8, 6]]
list(map(lambda i: list(map(lambda j: j - 1, i)), a))
[[0, 1, 2], [6, 7, 5]] #output

#1


9  

This leaves the ints nested

这使得int嵌套

[map(int, x) for x in values]

If you want them flattened, that's not hard either

如果你想让它们变平,那也不难

for Python3 map() returns an iterator. You could use

for Python3 map()返回一个迭代器。你可以用

[list(map(int, x)) for x in values]

but you may prefer to use the nested LC's in that case

但在这种情况下你可能更喜欢使用嵌套的LC

[[int(y) for y in x] for x in values]

#2


9  

How about:

怎么样:

>>> a = [['1','2','3'],['4','5','6'],['7','8','9']]
>>> [[int(j) for j in i] for i in a]
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

#3


1  

You simply use incorrect order and parenthesis - should be:

你只是使用不正确的顺序和括号 - 应该是:

inputVals = [['1','2','3'], ['3','3','2','2']]
[int(x) for row in inputVals for x in row]

Or if you need list of list at the output then:

或者如果您需要输出列表,那么:

map(lambda row: map(int, row), inputVals)

#4


0  

an ugly way is to use evalf:

一种丑陋的方式是使用evalf:

>>> eval(str(a).replace("'",""))
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

if you don't mind all your numbers in one array you could go:

如果你不介意一个数组中的所有数字,你可以去:

>>> a = [['1','2','3'],['4','5','6'],['7','8','9']]
>>> map(int,sum(a,[]))
[1, 2, 3, 4, 5, 6, 7, 8, 9]

#5


0  

Another workaround

另一种解决方法

a = [[1, 2, 3], [7, 8, 6]]
list(map(lambda i: list(map(lambda j: j - 1, i)), a))
[[0, 1, 2], [6, 7, 5]] #output