0

我已经浏览了很多类似的线程,但可能由于我对 python 缺乏了解,我还没有在我的问题中找到可行的解决方案。

这是代码的一部分:

for line in splitline:
    if("Fam" in line):
        if("NK" in line or "V" in line):
            normaali = line.split()
            normaalilista.append(normaali)
            both.append(normaali)
        if("TK" in line):
            tumor = line.split()
            tuumorilista.append(tumor)
            both.append(tumor)

“both”的输出看起来像这样:

['Fam_c828_1', '12-0799NK', '100']
['Fam_c828_1', '12-0800TK', '100']
['Fam_s56_1', '12-0801TK', '100']
['Fam_s134_1', '12-0802NK', '100']
['Fam_s146_1', '12-0803TK', '100']

我想保留具有相同 index[0] 值的行/单元格。就像在这种情况下:

['Fam_c828_1', '12-0799NK', '100']
['Fam_c828_1', '12-0800TK', '100']

其余的将被删除到另一个列表中。

提前致谢

4

2 回答 2

1

要根据第一个空格分隔列的值对行进行分组:

from collections import defaultdict

d = defauldict(list) # index[0] -> line
for line in splitline:
    columns = line.split()
    d[columns[0]].append(columns)
于 2013-05-03T11:52:38.690 回答
1

你可以使用itertools.groupby

>>> from itertools import groupby
>>> groups = groupby(both, lambda x: x[0]) # Group `both` by the zeroth index of its members
>>> group = next(groups) # Get the first group in groups
>>> group
('Fam_c828_1', <itertools._grouper object at 0x10f065d10>)
>>> list(group[1]) # Cast the group iterable into a list for display purposes
[['Fam_c828_1', '12-0799NK', '100'], ['Fam_c828_1', '12-0800TK', '100']]
于 2013-05-03T12:24:38.200 回答