在我的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