如果你想获得列表中的每一个第三项,你可以做myList [:: 3]。
例如,
Python 2.7 (r27:82525, Jul 4 2010, 09:01:59) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> a = []
>>> for i in xrange(1,22):
... a.append(i)
...
>>> a
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]
>>> b = a # start index of b will be the first item
>>> b = b[::3]
>>> b
[1, 4, 7, 10, 13, 16, 19]
>>> c = a[1:] # start index of c will be the second item
>>> c = c[::3]
>>> c
[2, 5, 8, 11, 14, 17, 20]
>>> d = a[2:] # start index of d will be the third item
>>> d = d[::3]
>>> d
[3, 6, 9, 12, 15, 18, 21]
所以,你可以在你的readlines方法()列表分成像这样3个独立的列表,然后将它们添加到您的ListCtrl。
Readlines只是返回一个列表,其中列表中的每个项目都是文件中的一行。所以,如果你有作为F打开该文件,它应该是这样的,
with open("file.txt", 'r') as f:
lines = f.readlines()
list1 = lines
list1 = list1[::3]
list2 = lines[1:]
list2 = list2[::3]
list3 = lines[2:]
list3 = list3[::3]
现在list1
每持有三线(0,3,6,9),list2
持有(1,4,7,10) ,并且list3
成立(2,5,8,11)。
啊,那么我在readlines()函数中究竟引用了什么?我尝试使用列表,然后使用'b = b [:: 3]',然后在'readlines(b)'中使用b,但它给了我一个类型错误。 – 2013-03-06 00:35:21
等一下,我重读它,并意识到你的意思。真棒,就像一个魅力。谢谢。 – 2013-03-06 00:48:39
Readlines只是返回一个列表,其中列表中的每个项目都是来自文件的一行。我用新代码编辑了我原来的帖子。 – 2013-03-06 00:48:47