简体   繁体   中英

How to append only the unique item on the list?

I am hoping to find way to append only the unique item numlookup and wholetoken. Is there a good way to do this?

numlookup = defaultdict(list) 
wholetoken = defaultdict(list)

#mydata is file containing mutation description
mydata = open('/mutation_summary.txt')
for line in csv.reader(mydata, delimiter='\t'):
    code = re.match('[a-z](\d+)[a-z]', line[-1], re.I)
    if code: 
        numlookup[line[-2]].append(code.group(1))
        wholetoken[line[-2]].append(code.group(0))

When i try to use set i got this as error when i call lookup(id) and wholelookup(id): TypeError: 'set' object is not callable

lookup =set()
wholelookup =set()

with open('mutation_summary.txt') as mydata:
    for line in csv.reader(mydata, delimiter='\t'):
        code = re.match('[a-z](\d+)[a-z]', line[-1], re.I)
        if code: 
            lookup.add(code.group(1))
            wholelookup.add(code.group(0))

Why not turn it into a defaultdict of set s? It only keeps the uniques.

If that is not an option, then you could try:

if code:
    if code.group(1) not in numlookup[line[-2]]:
        numlookup[line[-2]].append(code.group(1))
    if code.group(0) not in wholetoken[line[-2]]:
        wholetoken[line[-2]].append(code.group(0))

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM