[英]How do you create a two variable list?
I know the usual way in Python of creating a 2d list is by making a list of lists and accessing an element with foo[3][2]
.我知道在 Python 中创建二维列表的常用方法是制作列表列表并使用
foo[3][2]
访问元素。 However, when performing some operations needed for AES, it would be helpful to use foo[-1][2]
, which would be a column of the data ( -1
is every value).但是,在执行 AES 所需的一些操作时,使用
foo[-1][2]
会很有帮助,这将是一列数据( -1
是每个值)。
Would it be possible to set up a 2d list using the format foo[3, 2]
free from object-oriented classes, or not?是否可以使用格式
foo[3, 2]
建立一个没有面向对象类的二维列表,或者不? Side note: the only time I've ever seen this is in the PILLOW Image library for Python, however it was used in a class, and I don't know how to isolate just what I want.旁注:我见过的唯一一次是在 Python 的 PILLOW 图像库中,但是它用于 class,我不知道如何隔离我想要的。
There is no magic 'all values' built-in for lists... but:列表没有内置的神奇“所有值”......但是:
You can write things like bar(foo[*])
as map(bar, foo)
or [bar(x) for x in foo]
.您可以将
bar(foo[*])
类的东西写成map(bar, foo)
或[bar(x) for x in foo]
。
For example, foo[*][2]
can be [x[2] for x in foo]
.例如,
foo[*][2]
可以是[x[2] for x in foo]
。
Another option when trying to get columns is to transpose the results (though consider using numpy et al. at this point).尝试获取列时的另一个选择是转置结果(尽管此时考虑使用 numpy 等人)。 You can do this with
list(zip(*foo))
, as mentioned by @gelonida.正如@gelonida 所提到的,您可以使用
list(zip(*foo))
来做到这一点。
If you are willing to use classes or helpers then you have many other options to get the syntactic sugar you desire.如果您愿意使用类或助手,那么您还有许多其他选择来获得您想要的语法糖。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.