Basically I'm not sure if there is any possible way to convert this code into list comprehension. list1 and list3 are lists of lists that have been extracted from information about genes. So basically x[2] is a gene_id, and y[0] is also a gene_id. y[1] is gene names. basically I want to end up with list 5, which is a lists of lists that I can loop through to write in a tsv.
list2 = []
list4 = []
list5 = []
for x in list1:
for y in list3:
if x[2] == y[0]:
list2.append(y[1])
list4.append(x[2])
list4.append(",".join(list2))
list5.append(list4)
list2 = []
list4 = []
For a list in list1, x is [9606,HEX1M1,9606.ENSP00000328773].
Then for all y that has y[0] == x[2] in list3, so for instance, [9606.ENSP00000395733,ZNF737-001,Ensembl_HGNC_trans_name Ensembl_Vega_transcript] and [9606.ENSP00000395733,ZNF737-002,Ensembl_HGNC_trans_name Ensembl_Vega_transcript]
Then we want to get [["9606.ENSP00000395733", "ZNF737-001, ZNF737-002,HEX1M1"]] for list 5 after that iteration.
You can create a mapping dict from list3
that maps keys in the first items to lists of values in the second items, so that you can use a list comprehension to iterate over list1
to output lists of pairs of keys and associated lists from the aforementioned mapping dict for keys that are found in the dict:
mapping = {}
for k, v, *_ in list3:
mapping.setdefault(k, []).append(v)
list5 = [[k, ','.join(mapping[k])] for *_, k in list1 if k in mapping]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.