简体   繁体   English

如何创建两个变量列表?

[英]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.

相关问题 在 Python 中,如果您有两个共享多个元素的列表,您如何创建具有匹配项的新列表? - In Python, if you have two lists that share a number of elements, how do you create a new list with the matches? 在python中,如何在运行时创建一个名称未指定的新变量(编辑:如何将setattr()与列表理解一起使用)? - In python, how do you create a new variable with a name unspecified until runtime (Edit: How do you use setattr() with a list compehension)? 你如何在 Python 中创建变量代码? - How do you create variable code in Python? 如何在 Python 中创建一个浮动变量? - How do you create a floating variable in Python? 如何在Python中将列表重命名为变量 - How do you rename a list to a variable in Python 如果一个变量有两种可能的结果,如何分别从列表中添加值 - how do you add values from a list separately if one variable has two possible outcomes 你如何创建一个空的元组列表? - How do you create an empty list of tuples? 如何从两个列表创建元组,以便重复一个列表元素 - How do you create Tuples from two lists so that one list elements repeat 如何使用%i从变量创建变量? - How do you create a variable from a variable with %i? 如何在Python的两个进程之间共享变量? - How do you share a variable between two processes in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM