简体   繁体   English

我如何将一个列表的元素拆分为另一个列表的元素,但按照它们在列表中的显示顺序

[英]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.

相关问题 如何将列表拆分为包含 3 个元素的多个列表? - How can I split a list into multiple lists of 3 elements? 如何保留列表列表中元素的顺序 - How can I perserve the order of elements in a list of lists 如何以相同的顺序将一个列表的元素插入另一个列表? - How can I insert elements of one list into another list in the same order? 如何根据相邻元素之间的差异将列表拆分为列表列表? - How can I split a list into a list of lists based on the difference between adjacent elements? 如何拆分列表的元素? - How can I split elements of a list? 如何将元素列表与 Python 中的另一个列表列表相乘 - How do I multiply a list of elements with another list of lists in Python 您如何在另一个列表中按顺序查找一个列表中的元素? - How do you find the elements in one list, in that order, in another list? 如何以另一个列表中的元素为条件修改一个列表中的元素? - How can I modify elements in one list conditional on the elements in another list? 如何在 Python 中按顺序将列表元素添加到另一个列表? - How do I add list elements to another list with an order in Python? 如何在列表列表中按空格和引号拆分元素? - How can I split elements by space and quotation within a list of list?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM