如果您想获取列表中的每三个项目,您可以执行 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)。