list - 在 Python 中运行多个循环

标签 list python-2.7 for-loop

假设我有两个列表:

>>> passwordList = ['lee', 'venter', 'rusty']
>>> wrong_num = [5, 5, 0]

我将两个列表循环在一起,以使用以下方法组合两个列表的相应索引:

>>>for p, w in zip(passwordList,wrong_num):
>>>      newFile.write("The password " + p + " was wrong by " + str(w) + "characters")

结果如下所示:

>>> The password lee was wrong by 5 characters
>>> The password venter was wrong by 5 characters
>>> The password rusty was wrong by 0 characters

当我想包含一个索引变量以使结果看起来像这样时,我的问题就出现了:

>>>"The password entry 1: lee, was wrong by 5 characters"
>>>"The password entry 2: lee, was wrong by 5 characters"
>>>"The password entry 3: lee, was wrong by 5 characters

我想对第一个列表进行索引,以便可以为列表中的每个项目显示 1、2 和 3。因此,为此我添加了另一个循环,使代码如下所示:

>>>for ind, item in enumerate(passwordList):
>>>        for p, w in zip(passwordList,wrong_num):
>>>             newFile.write("The password entry " + str(ind) + ": " + p + "is wrong by " + str(w) + " characters")

文件 newFile 中的结果如下所示:

>>> Incorrect password 1: lee, wrong by 5 characters
>>> Incorrect password 1: lee, wrong by 5 characters
>>> Incorrect password 1: lee, wrong by 0 characters
>>> Incorrect password 2: venter, wrong by 5 characters
>>> Incorrect password 2: venter, wrong by 5 characters
>>> Incorrect password 2: venter, wrong by 0 characters
>>> Incorrect password 3: rusty, wrong by 5 characters
>>> Incorrect password 3: rusty, wrong by 5 characters
>>> Incorrect password 3: rusty, wrong by 0 characters

我怎样才能阻止这种情况发生,而只打印:

>>> Incorrect password 1 : lee, wrong by 5 characters
>>> Incorrect password 2 : venter, wrong by 5 characters
>>> Incorrect password 3: rusty, wrong by 0 characters

这仅用于理论,因此请忽略“rusty 错误 0 个字符”在刺痛开始时仍然显示错误的密码。

最佳答案

改为枚举 zip() 的结果,只留下一个 for 循环:

>>> passwordList = ['lee', 'venter', 'rusty']
>>> wrong_num = [5, 5, 0]
>>> for i, (p, w) in enumerate(zip(passwordList,wrong_num)):
...   print "The password entry " + str(i) + ": " + p + "is wrong by " + str(w) + " characters"
...
The password entry 0: leeis wrong by 5 characters
The password entry 1: venteris wrong by 5 characters
The password entry 2: rustyis wrong by 0 characters

另一个选择是使用itertools

>>> from itertools import izip, count
>>> passwordList = ['lee', 'venter', 'rusty']
>>> wrong_num = [5, 5, 0]
>>> for i, p, w in izip(count(), passwordList, wrong_num):
...   print "The password entry " + str(i) + ": " + p + "is wrong by " + str(w) + " characters"
...
The password entry 0: leeis wrong by 5 characters
The password entry 1: venteris wrong by 5 characters
The password entry 2: rustyis wrong by 0 characters

如果您需要索引为 1 2 3 而不是 0 1 2,您可以将 str(i) 替换为 str(i+1)

关于list - 在 Python 中运行多个循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32351017/

相关文章:

c - gtk-get 在列表存储 TreeView 中单击了哪个项目

list - Lisp 格式程序

python - 创造众多独特的事件

python - 仅包含 ASCII 字符的 UNICODE 字符串是否总是等于 ASCII 字符串?

有符号和无符号整数表达式之间的比较 [-Wsign-compare] 警告

c++ - 剥离项目的最佳 C++ 容器?

css - 向背景图像添加类?

python - 尝试使用 python 从 azure 函数触发 url,错误提示 "without a $return binding returned a non-None value"

java - 长度无法解析或不是字段

for-loop - 使用 for 循环打印给定的字符模式?