[英]how can i split the elements of one list by the elements of another list but in the order they are presented in the lists
something like this:像这样的东西:
equation=list_1[0]+list_2[0]+list_1[1]+list_2[1]+list_1[2]+list_2[2]+list_1[3]
but how can I make it more general?但我怎样才能让它更普遍? this will only work if i know the number of elements in each list.
这只有在我知道每个列表中的元素数量时才有效。
Loop over the result of zip(list_1, list_2)
:循环
zip(list_1, list_2)
的结果:
equation = ""
for i1, i2 in zip(list_1, list_2):
equation += str(i1) + str(i2)
If ths lists have different lengths, you can append the extra elements afterward:如果列表有不同的长度,您可以 append 之后的额外元素:
if len(list_1) > len(list_2):
equation += sum([str(i) for i in list_1[len(list_2):]])
elif len(list_2) > len(list_1):
equation += sum([str(i) for i in list_2[len(list_1):]])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.