Python: sorting/grouping lists of lists alphabetically -
i feel has been asked, answers questions have not worked code.
i trying sort list of lists alphabetically based on first list[2] within matching entries sorted list[3] , list[4] , on. real data bigger , has more entries in each list example:
list = [ ['x_campestris_vesicatoria', 'bacteria', 'proteobacteria', 'gammaproteobacteria', 'xanthomonadales', 'xanthomonadaceae', 'xanthomonas'], ['pantoea', 'bacteria', 'proteobacteria', 'gammaproteobacteria', 'enterobacteriales', 'enterobacteriaceae', 'pantoea'], ['acidobacterium', 'bacteria', 'acidobacteria', 'acidobacteriales', 'acidobacteriaceae', 'granulicella'], ['s_boydii', 'bacteria', 'proteobacteria', 'gammaproteobacteria', 'enterobacteriales', 'enterobacteriaceae', 'shigella']] i have tried of things similar questions have answers:
taxlist.sort(key = lambda row: (row[2],row[3],row[4],row[5])) print taxlist but no sorting happening.
same when try:
sorted(taxlist, key=lambda x: (x[2],x[3],x[4],x[5])) print taxlist and once list sorted can still use or list no longer iterable?
import csv writer = csv.writer(sys.stdout, delimiter="\t") writer.writerows(taxlist) thanks help.
sorted gives new list, while l.sort sorts l in place. in both cases, original list still accessible , can iterated over.
in use sorted, old list still accessible , can iterated over. list returned accessible , can iterated over.
hope helps
Comments
Post a Comment