简体   繁体   中英

TypeError: unhashable type: 'list' Python

I'm trying to extract the column number of a column using csv dict and give that as an input to countt. However, it gives me an error saying TypeError: unhashable type: 'list' Python . Here's the part of my code that's been causing me troubles.

indexes = [i for i,x in enumerate(all_fieldnames) if x == barcode]
countt = [rec[indexes] for rec in tsv_data]

I'd really appreciate if you tell me how to fix it.

I've seen many similar questions on stackoverflow before posting here, but none of them helped.

You are passing in a list of indices ( indexes is that list). Moreover, rec is a dictionary (presumably you are using DictReader ) so you should use keys, not indexes.

If barcode contains the field name you are looking for you can just use:

countt = [rec[barcode] for rec in tsv_data]

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