如何从列表中打印每一个4项?
list = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm']发布于 2013-10-24 02:31:21
用一片:
fourth_items = list[3::4] # ['d', 'h', 'l']
for i in fourth_items: print i发布于 2013-10-24 02:35:50
谢谢Nirks的评论,我更正了我的答案。列表是python中内置的函数,它不是作为变量名使用的建议,我将其更改为list_test。
list_test = list('abcdefghijklm')
tmp = list_test[3::4]这是不同的部分
按正常顺序打印:
for i in tmp: print i或
按反序打印:
while tmp: print tmp.pop()发布于 2013-10-24 02:16:55
为什么不循环通过,每次迭代增加4。您可以使用len(list)获取python中数组的长度。
index = 0 #or start at 3, pending on where you want to start
while index < len(list) :
print list[index]
index +=4https://stackoverflow.com/questions/19555542
复制相似问题