这是一个简单的代码,用于使用英文名和姓氏列表生成全名列表:
names = """
Walter
Dave
Albert""".split()
fullnames = [(first + last) for first, last in names]
print(fullnames)为了这篇文章,我把names变小了,但我收录了100个名字。
输出:
Traceback (most recent call last):
File "/home/pussyslayer42069/Desktop/py/names.py", line 105, in <module>
fullnames = [(first + last) for first, last in names]
File "/home/pussyslayer42069/Desktop/py/names.py", line 105, in <listcomp>
fullnames = [(first + last) for first, last in names]
ValueError: too many values to unpack (expected 2)发布于 2021-03-28 10:42:53
使用zip遍历列表的两个片段
[(f, l) for f, l in zip(names[:-1], names[1:]]
发布于 2021-03-28 10:47:04
如果我找到你了,这就是解决方案;
names = """
Walter
Dave
Albert""".split()
fullnames = [(names[i] + ' ' + names[i + 1]) for i in range(len(names) - 1)]
print(fullnames)https://stackoverflow.com/questions/66837890
复制相似问题