简体   繁体   English

从五个项目列表中打印除第四项以外的所有内容,以便两个列表彼此相邻

[英]Print all except the fourth item from a five item list for two lists next to each other Python

So this new problem I have come to is this. 所以我遇到的这个新问题就是这个。 I have a two lists with five items each. 我有两个清单,每个清单有五个项目。

listone = ['water', 'wind', 'earth', 'fire', 'ice']
listtwo = ['one', 'two', 'three', 'four', 'five']

What I want to do is print the first, second, third and fifth items from each of these lists in a string: 我想做的是将这些列表中的每个列表的第一,第二,第三和第五项打印成字符串:

print("the number is %s the element is %s" % (listtwo, listone)

But they need to print in a new row each time so that the text is run for each of the elements in the two lists: 但是它们每次都需要在新行中打印,以便针对两个列表中的每个元素运行文本:

the number is one the element is water
the number is two the element is wind
the number is three the element is earth
the number is five the element is five

I have no idea how to do this. 我不知道该怎么做。 I tried using list split but since its the fourth item out of five I can't figure out how to skip it. 我尝试使用列表拆分,但由于它是五分之四中的第四项,所以我不知道如何跳过它。 Also I use this to list the string in a new row: 我也用它在新行中列出字符串:

for x in listone and listtwo:
print("the number is {0} the element is {0}".format(x)  

But I don't know how to use this with two lists or if it even can be used with two lists. 但是我不知道如何将它与两个列表一起使用,甚至不可以与两个列表一起使用。

Please help :( 请帮忙 :(

EDIT: 编辑:

Also I don't know what the elements of the scripts are so I can only use their number in the list. 而且我也不知道脚本的元素是什么,所以我只能在列表中使用它们的编号。 So I need to get rid of [4] in both lists. 因此,我需要在两个列表中都删除[4]。

for (i, (x1, x2)) in enumerate(zip(listone,listtwo)):
    if i != 3:
        print "The number is {0} the element is {1}".format(x1, x2)

Explanation 说明

  • the zip(listone,listtwo) gives you a list of tuples (listone[0],listtwo[0]), (listone[1],listtwo[1])... zip(listone,listtwo)给您一个元组列表(listone[0],listtwo[0]), (listone[1],listtwo[1])...
  • the enumerate(listone) gives you a list of tuples (0, listone[0]), (1, listone[1]), ...] enumerate(listone)为您提供元组列表(0, listone[0]), (1, listone[1]), ...]

    (you guessed it, it's another, more efficient way to do zip(range(len(listone)),listone) (您猜对了,这是另一种更有效的zip(range(len(listone)),listone)

  • By combining the two, you get a list of the elements you want along their indices 通过将两者结合起来,您可以获得沿其索引想要的元素的列表
  • Because your first element has index 0 and that you don't want the fourth element, just check that the index is not 3 因为您的第一个元素的索引为0 ,并且您不希望第四个元素,所以只需检查索引是否为3
for pos in len(listone):
    if(pos != 3):
        print("the number is {0} the element is {1}".format(pos,listone[pos]))
for x in zip(list1,list2)[:-1]:
    print("the number is {0} the element is {0}".format(x))
listone = ['water', 'wind', 'earth', 'fire', 'ice']
listtwo = ['one', 'two', 'three', 'four', 'five']
z = zip(listone, listtwo)
z1 = z[:3]
z1.append(z[4])
for i, j in z1:
    print "the number is {} the element is {}".format(j, i)

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

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