在我的OP后,其他网络搜索发现这个:How do I use Python's itertools.groupby()?
这是我目前的方法。请告知我是否可以使它更加Pythonic。
loadfile1.txt(无分组变量 - 相同的输出loadfile4.txt):
pgm1
pgm2
pgm3
pgm4
pgm5
pgm6
pgm7
pgm8
/a/path/with spaces/pgm9
loadfile2.txt(随机分组变量):
10, pgm1
10, pgm2
10, pgm3
ZZ, pgm4
ZZ, pgm5
-5, pgm6
-5, pgm7
-5, pgm8
-5, /a/path/with spaces/pgm9
loadfile3.txt(同一分组变量 - 不依赖关系 - 多线程):
,pgm1
,pgm2
,pgm3
,pgm4
,pgm5
,pgm6
,pgm7
,pgm8
,/a/path/with spaces/pgm9
loadfile4.txt(不同的分组变量 - dep endencies - 单线程):
1, pgm1
2, pgm2
3, pgm3
4, pgm4
5, pgm5
6, pgm6
7, pgm7
8, pgm8
9, /a/path/with spaces/pgm9
我的Python脚本:
#!/usr/bin/python
# See https://stackoverflow.com/questions/4842057/python-easiest-way-to-ignore-blank-lines-when-reading-a-file
# convert file to list of lines, ignoring any blank lines
filename = 'loadfile2.txt'
with open(filename) as f_in:
lines = filter(None, (line.rstrip() for line in f_in))
print(lines)
# convert list to a list of lists split on comma
lines = [i.split(',') for i in lines]
print(lines)
# create list of lists based on the key value (first item in sub-lists)
listofpgms = []
for key, group in groupby(lines, lambda x: x[0]):
pgms = []
for pgm in group:
try:
pgms.append(pgm[1].strip())
except IndexError:
pgms.append(pgm[0].strip())
listofpgms.append(pgms)
print(listofpgms)
输出使用loadfile2.txt时:
['10, pgm1', '10, pgm2', '10, pgm3', 'ZZ, pgm4', 'ZZ, pgm5', '-5, pgm6', '-5, pgm7', '-5, pgm8', '-5, /a/path/with spaces/pgm9']
[['10', ' pgm1'], ['10', ' pgm2'], ['10', ' pgm3'], ['ZZ', ' pgm4'], ['ZZ', ' pgm5'], ['-5', ' pgm6'], ['-5', ' pgm7'], ['-5', ' pgm8'], ['-5', ' /a/path/with spaces/pgm9']]
[['pgm1', 'pgm2', 'pgm3'], ['pgm4', 'pgm5'], ['pgm6', 'pgm7', 'pgm8', '/a/path/with spaces/pgm9']]
请您能不能告诉你有什么到目前为止已经试过? – styvane
我进行了网络搜索并搜索了超过一个小时之前发布的“列表的Python文件列表”。难倒我的是如何检测团队何时改变。话虽如此,未来我会尽我所能提供我已经尝试的示例代码,作为所有SO帖子的一部分。 – Scott