I have a list of codes coming from a csv file:
file_path = 'c:\\temp\\list.csv'
csvfile = open(file_path, 'rb')
reader = csv.reader(csvfile, delimiter=';')
rr = []
for sor in reader:
if sor[1][0] == '1':
rr.append(sor)
print type(rr)
<type 'list'>
set (rr)
Traceback (most recent call last):
File "<pyshell#85>", line 1, in <module>
set (rr)
TypeError: unhashable type: 'list'
If I do the very same on an other list coming from a database it works fine:
cur.execute('select code from mytable')
res = cur.fetchall()
res1 = []
res1.append(x[0] for x in res)
print type(res1)
<type 'list'>
set(res1)
set(['13561255', '11120088'])
What is the difference between rr and res1 as both are of list type?
Actually I'm looking for records in the database which doesn't exist in the csv file by doing
result = list(set(res1) - set(rr))
How can I achieve this (maybe in a more optimal/faster way)?
Every sor
is a csv row - a list of row "cell" values, rr
becomes a list of lists. Lists cannot be items of a set since lists are not hashable.
res1
on the other hand is a list of string values. Strings are hashable.
Here is an example that demonstrates the difference:
>>> l1 = [["1", "2", "3"]]
>>> l2 = ["1", "2", "3"]
>>>
>>> set(l1)
Traceback (most recent call last):
File "<input>", line 1, in <module>
set(l1)
TypeError: unhashable type: 'list'
>>> set(l2)
set(['1', '3', '2'])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With