为简单起见进行了编辑,因为我已将问题指向“参数拆包”。
我正在尝试编写一个将任意数量的列表作为参数交错的函数。所有列表的长度相同。该函数应返回一个列表,其中包含交错的输入列表中的所有元素。
def interleave(*args):
for i, j, k in zip(*args):
print(f"On {i} it was {j} and the temperature was {k} degrees celsius.")
interleave(["Monday Tuesday Wednesday Thursday Friday Saturday Sunday".split()],["rainy rainy sunny cloudy rainy sunny sunny".split()],[10,12,12,9,9,11,11])
输出:
On ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'] it was ['rainy', 'rainy', 'sunny', 'cloudy', 'rainy', 'sunny', 'sunny'] and the temperature was 10 degrees celsius.
所需的输出:
On Monday it was rainy and the temperature was 10 degrees celsius.
On Tuesday it was rainy and the temperature was 12 degrees celsius.
On Wednesday it was sunny and the temperature was 12 degrees celsius.
On Thursday it was cloudy and the temperature was 9 degrees celsius.
On Friday it was rainy and the temperature was 9 degrees celsius.
On Saturday it was sunny and the temperature was 11 degrees celsius.
On Sunday it was sunny and the temperature was 11 degrees celsius.