2012-09-26 27 views
0

所以我遇到的这个新问题就是这个。我有两个列表,每个列表有五个项目。从五个项目列表中除了第二个项目之外的所有项目都打印出两个列表Python

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

我想要做的就是打印字符串中的第一,第二,第三和第五,从每个列表的项目:

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

但是他们需要在一个新行每次打印以便为两个列表中的每个元素运行文本:

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 

我不知道如何做到这一点。我尝试使用列表拆分,但自从它的五分之四我无法弄清楚如何跳过它。此外,我用这个列表的字符串一个新行:

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

但我不知道如何与两个列表使用或者如果它甚至可以用两个列表中。

请帮助:(

编辑:

此外,我不知道是什么脚本的元素,所以我只能用自己的号码列表中的所以我需要摆脱[。 4]在这两个列表。

回答

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

说明

  • zip(listone,listtwo)给你的元组(listone[0],listtwo[0]), (listone[1],listtwo[1])...
  • enumerate(listone)给你的元组(0, listone[0]), (1, listone[1]), ...]

    (你猜对了的名单列表,这是另一种,做zip(range(len(listone)),listone)

  • 更有效的方法通过将两者结合起来,您可以得到您想要的索引元素列表
  • 因为您的第一个元素索引为0和你不想要的第四个元素,只是检查了指数不3
+0

我爱你。哈哈哈,但没有认真的这完美的作品,这是一个巨大的帮助。非常感谢你 – Adilicious

0
for x in zip(list1,list2)[:-1]: 
    print("the number is {0} the element is {0}".format(x)) 
1
for pos in len(listone): 
    if(pos != 3): 
     print("the number is {0} the element is {1}".format(pos,listone[pos])) 
0
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) 
相关问题