使用python循环遍历两个列表,使用一个作为列表列表的索引,另一个作为要追加的值 [英] Use python to loop over two lists, using one as an index to a list of lists, and the other as a value to append

查看:36
本文介绍了使用python循环遍历两个列表,使用一个作为列表列表的索引,另一个作为要追加的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在从 zip 命令中获取预期结果时遇到了一些问题.情况 1-3 有意义,但在情况 4 和 5(我认为它们是等效的?)我希望结果是 [['a'],['b'],['c'],['d']],而是将整个第二个列表附加到我初始化的列表列表的每个子列表中.

案例 1:

<预><代码>>>>对于 zip([1,2,3,4],['a','b','c','d']):...打印一个(1, '一')(2, 'b')(3, 'c')(4, 'd')

情况 2:

<预><代码>>>>对于 zip([1,2,3,4],['a','b','c','d']) 中的 (a,b):... 打印 a,b...1个2 乙3 c4天

情况 3:

<预><代码>>>>温度 = [[]] * 4>>>对于 zip([0,1,2,3],['a','b','c','d']) 中的 (a,b):... temp[a] = b...>>>温度['A B C D']

案例 4:

<预><代码>>>>温度 = [[]] * 4>>>对于 zip([0,1,2,3],['a','b','c','d']) 中的 (a,b):... temp[a].append(b)...>>>温度[['a', 'b', 'c', 'd'], ['a', 'b', 'c', 'd'], ['a', 'b', 'c','d'], ['a', 'b', 'c', 'd']]

案例 5:

<预><代码>>>>温度 = [[]] * 4>>>对于 zip([0,1,2,3],['a','b','c','d']) 中的 a,b:... temp[a].append(b)...>>>温度[['a', 'b', 'c', 'd'], ['a', 'b', 'c', 'd'], ['a', 'b', 'c','d'], ['a', 'b', 'c', 'd']]

解决方案

您已经遇到了 Python 处理变量名和引用的方式.如果您以前使用过 C 风格的语言,这可能会令人困惑,但如果您不这么想,那就很有意义了!

Python 有事物"和事物名称".例如,x 是一个名称,[] 是一个事物,而 x=[] 分配名称 x> 到事物 [].

列表乘法语法在名称和事物方面有点混乱.让我们展开它看看它是如何工作的:

<块引用>

name = [x]*4

相同

_temp = x名称 = [_temp, _temp, _temp, _temp]

考虑到这一点,以下代码应该是有意义的:

<预><代码>>>>x = []>>>y = [x]*4>>>y[0].append(1)>>>是[[1], [1], [1], [1]]

为什么?好吧,当您编写 y=[x]*4] 时,您会列出四个名称.但这些都是相同事物的名称!如果你 .append(1) 到以名字命名的对象,你改变了所有名称的对象,好吧,名称.

现在,就像以前一样,

<块引用>

name = [[]]*4

相同

_temp = []名称 = [_temp, _temp, _temp, _temp]

您尚未明确创建新名称.但是你已经暗中这样做了!你只调用 [] 一次,所以你只能创建一个新列表.然后你列出了四个名字,每个都是你刚刚创建的新的空列表.因此,所有名称都命名相同的事物也就不足为奇了!

<小时>

你说如何解决这个问题?好吧,您需要为 每个 所需的新列表调用一次 [].为此,请使用列表推导式:

[[] for _ in range(4)]

请注意,_ 只是另一个名称,尽管它通常用于您不关心的事情.

I am having some trouble getting expected results out of the zip command. Cases 1-3 make sense, but in cases 4 and 5 (which I assume are equivalent?) I expect the results to be [['a'],['b'],['c'],['d']], but instead the entirety of the second list is appended to each sublist of the list of lists I initialize.

Case 1:

>>> for a in zip([1,2,3,4],['a','b','c','d']):
...     print a

(1, 'a')
(2, 'b')
(3, 'c')
(4, 'd')

Case 2:

>>> for (a,b) in zip([1,2,3,4],['a','b','c','d']):
...     print a,b
... 
1 a
2 b
3 c
4 d

Case 3:

>>> temp = [[]] * 4
>>> for (a,b) in zip([0,1,2,3],['a','b','c','d']):
...     temp[a] = b
... 
>>> temp
['a', 'b', 'c', 'd']

Case 4:

>>> temp = [[]] * 4
>>> for (a,b) in zip([0,1,2,3],['a','b','c','d']):
...     temp[a].append(b)
... 
>>> temp
[['a', 'b', 'c', 'd'], ['a', 'b', 'c', 'd'], ['a', 'b', 'c', 'd'], ['a', 'b', 'c', 'd']]

Case 5:

>>> temp = [[]] * 4
>>> for a,b in zip([0,1,2,3],['a','b','c','d']):
...     temp[a].append(b)
... 
>>> temp
[['a', 'b', 'c', 'd'], ['a', 'b', 'c', 'd'], ['a', 'b', 'c', 'd'], ['a', 'b', 'c', 'd']]

解决方案

You've run into the way Python deals with variable names and references. It can be confusing if you have worked with C-style languages before, but makes a lot of sense if you don't think that way!

Python has "things" and "names for things". For example, x is a name, [] is a thing, and x=[] assigns the name x to the thing [].

The list multiplication syntax is a bit confusing with regards to names and things. Let's expand it out to see how it works:

name = [x]*4

is the same as

_temp = x
name = [_temp, _temp, _temp, _temp]

With that in mind, the following code should make sense:

>>> x = []
>>> y = [x]*4
>>> y[0].append(1)
>>> y
[[1], [1], [1], [1]]

Why? Well, when you write y=[x]*4] you make a list of four names. But those are all names for the same thing! If you .append(1) to the object named by the first name, you change the object that all the names, well, name.

Now, just as before,

name = [[]]*4

is the same as

_temp = []
name = [_temp, _temp, _temp, _temp]

You haven't explicitly created a new name. But you have done so implicitly! You only call [] once, so you only make one new list. You then make a list of four names, each of the new empty list you just made. So it should come as no surprise that all the names name the same thing!


How to fix this, you say? Well, you need to call [] once for each new list that you want. To do that, use a list comprehension:

[[] for _ in range(4)]

Note that _ is just another name, although it is conventionally used for things you don't care about.

这篇关于使用python循环遍历两个列表,使用一个作为列表列表的索引,另一个作为要追加的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆