简体   繁体   English

python中两个for循环之间的交替执行

[英]alternate execution between two for loop in python

I have two strings that contains values that should be joined together, that means value one in string1 with values two in string2. 我有两个包含应该连接在一起的值的字符串,这意味着string1中的值1和string2中的值2。 Here is the code that I have tried it: 这是我尝试过的代码:

str1 = "John,Gabriel,Isaac,Albert"
str2 = "Cena,Heins,Newton,Einstein"
for x,y in str1.split(','),str2.split(','):
    print (x+y) 

I have got this error: 我有这个错误:

Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
ValueError: too many values to unpack

You could use zip , it returns an iterator (in Python 2 just a list) which spits out tuples with values combined from specified sources: 您可以使用zip ,它返回一个迭代器(在Python 2中只是一个列表),该迭代器吐出具有从指定源组合的值的元组:

str1 = 'John,Gabriel,Isaac,Albert'
str2 = 'Cena,Heins,Newton,Einstein'

for x, y in zip(str1.split(','), str2.split(',')):
    print(x + y)

To address what the actual problem is, this line: 为了解决实际的问题,此行:

for x,y in str1.split(','),str2.split(','):

Does not mean what you intend. 并不代表您的意图。

There are two things going on. 有两件事发生。

for i in s:

Iterates over elements of s , assigning them to i . 遍历s元素,将它们分配给i s in your case is: s在你的情况是:

>>> str1.split(','),str2.split(',')
(['John', 'Gabriel', 'Isaac', 'Albert'], ['Cena', 'Heins', 'Newton', 'Einstein'])

That is, a sequence of 2 elements, each being a list. 也就是说,由2个元素组成的序列,每个元素都是一个列表。 So your loop would iterate twice with i being set to one of those lists each time. 所以,你的循环将有两次迭代i被设置为每一次的名单之一。

On top of that, you're also trying to unpack i , effectively, trying to assign x, y with one of those lists, the same as this: 最重要的是,您还尝试有效地解压缩i ,并尝试为x, y分配其中一个列表,如下所示:

>>> x, y = ['John', 'Gabriel', 'Isaac', 'Albert']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: too many values to unpack

This doesn't work because there are more values on the right, than names on the left. 这不起作用,因为右侧的值比左侧的名称更多。

The solution is to construct a new sequence that has pairs of values from each list, and iterate over that instead, as other have said, using zip . 解决方案是构造一个新序列,该序列具有来自每个列表的成对值,然后像其他人所说的那样使用zip遍历该序列。

In case the number of fields in each string might be different you can use itertools.zip_longest() for Python 3, or itertools.izip_longest() in Python 2: 如果在每个字符串中的字段数可能会有所不同,您可以使用itertools.zip_longest()为Python 3,或itertools.izip_longest()在Python 2:

from itertools import zip_longest

str1 = "John,Gabriel,Isaac,Albert,Aristotle"
str2 = "Cena,Heins,Newton,Einstein"

for firstname, surname in zip_longest(str1.split(','), str2.split(','), fillvalue=''):
    print('{}{}{}'.format(firstname, ' ' if firstname and surname else '', surname))

Output: 输出:

John Cena
Gabriel Heins
Isaac Newton
Albert Einstein
Aristotle

Otherwise, if you know for certain that the names are always paired, you can just use zip() . 否则,如果您确定名称总是成对的,则可以使用zip()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM