Python:对二维数组(列表的列表)中的元素执行操作的单行?

发布于 2024-11-15 21:09:35 字数 232 浏览 2 评论 0原文

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

其中“values”是列表的列表。它一直说 x 是一个列表,因此不能作为 int() 的参数。显然我在这里做了一些愚蠢的事情,那是什么?对于这种事情有一个公认的习语吗?

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]]

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

回眸一遍 2024-11-22 21:09:35

这使得整数嵌套起来。

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

如果你想让它们扁平化,这对于 Python3 map() 返回一个迭代器来说也不难

。您可以使用

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

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

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

This leaves the ints nested

[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

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

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

[[int(y) for y in x] for x in values]
鸢与 2024-11-22 21:09:35

怎么样:

>>> 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]]

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]]
薔薇婲 2024-11-22 21:09:35

另一种解决方法

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

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
无人问我粥可暖 2024-11-22 21:09:35

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

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

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

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

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)
吃颗糖壮壮胆 2024-11-22 21:09:35

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

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

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

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

an ugly way is to use 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]
怎樣才叫好 2024-11-22 21:09:35

为了映射具有任意数量维度的列表,您可以使用 numpy.apply_over_axes

import numpy as np
np.apply_over_axes(lambda x,_:x*2, np.array([[1,2,3],[5,2,1]]),[0])
--------------------
array([[ 2,  4,  6],
       [10,  4,  2]])

不幸的是,如果您还需要更改变量类型,则这不起作用。没有找到任何库解决方案,所以这里是执行此操作的代码:

def map_multi_dimensional_list(l, transform):
    if type(l) == list and len(l) > 0:
        if type(l[0]) != list:
            return [transform(v) for v in l]
        else:
            return [map_multi_dimensional_list(v, transform) for v in l]
    else:
        return []
            
map_multi_dimensional_list([[[1,2,3],[5,2,1]],[[10,20,30],[50,20,10]]], lambda x:x*2)
------------------
[[[2, 4, 6], [10, 4, 2]], [[20, 40, 60], [100, 40, 20]]]

In order to map list with any number of dimensions you could use numpy.apply_over_axes

import numpy as np
np.apply_over_axes(lambda x,_:x*2, np.array([[1,2,3],[5,2,1]]),[0])
--------------------
array([[ 2,  4,  6],
       [10,  4,  2]])

Unfortunately that doesn't work if you also need to change variable type. Didn't find any library solution for this, so here is the code to do that:

def map_multi_dimensional_list(l, transform):
    if type(l) == list and len(l) > 0:
        if type(l[0]) != list:
            return [transform(v) for v in l]
        else:
            return [map_multi_dimensional_list(v, transform) for v in l]
    else:
        return []
            
map_multi_dimensional_list([[[1,2,3],[5,2,1]],[[10,20,30],[50,20,10]]], lambda x:x*2)
------------------
[[[2, 4, 6], [10, 4, 2]], [[20, 40, 60], [100, 40, 20]]]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文