Modify list with python by index[0] value -
i have browsed similar threads lot, maybe due lack of knowledge in python, haven't found working solution in problem.
this part of code:
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) the output of "both" looks atm:
['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'] i reserve lines / cells same index[0] value. in case:
['fam_c828_1', '12-0799nk', '100'] ['fam_c828_1', '12-0800tk', '100'] and rest removed list.
thanks in advance
you use itertools.groupby:
>>> itertools import groupby >>> groups = groupby(both, lambda x: x[0]) # group `both` zeroth index of members >>> group = next(groups) # first group in groups >>> group ('fam_c828_1', <itertools._grouper object @ 0x10f065d10>) >>> list(group[1]) # cast group iterable list display purposes [['fam_c828_1', '12-0799nk', '100'], ['fam_c828_1', '12-0800tk', '100']]
Comments
Post a Comment