类型错误:无法散列的类型:'列表'(使用字典)

时间:2019-10-05 14:51:41

标签: python

我在代码unhashable type: 'list'中遇到此错误

我有一个带有此输入的文本文件:

hokejistov hokej
strelena strela
trener trenuje

我想将单词分为2行。第一行将包含第一个单词。第二行将包含第二个单词。然后,我需要将单词分为符号。这部分工作正常,但是我需要使用字典(mapka = {'h':1,'o':2,'k':3, 'e':4, 'j':5, 'i':6, 's':7, 't':8,'v':9,'r':10, 'l':11, 'n':12, 'a':13, 'u':14})

在输出中,我想得到这个:

first = [[1, 2, 3, 4, 5, 6, 7, 8, 2, 9], [7, 8, 10, 4, 11, 4, 12, 13], [8, 10, 4, 12, 4, 10]]
second = [[1, 2, 3, 4, 5], [7, 8, 10, 4, 11, 13], [8, 10, 4, 12, 14, 5, 4]]

这是我的代码

data = [line.strip() for line in open("mamradhokej.txt",'r')]
mapka = {'h':1,'o':2,'k':3, 'e':4, 'j':5, 'i':6, 's':7, 't':8,'v':9, 
        'r':10, 'l':11, 'n':12, 'a':13, 'u':14}

first = [[word.lower() for word in text.split()[0]]for text in data]
second = [[word.lower() for word in text.split()[1]]for text in data]

print(first)
print(second)

map(mapka.get, first)
[mapka[k] for k in first] #here i am getting Type error: unhashable type: 'list'.

3 个答案:

答案 0 :(得分:3)

为什么会出现此错误?

字典键必须是可哈希对象。

为了使对象可哈希化,它应该是实现function sendEmail(event){ var sheet = event.range.getSheet(); if(sheet.getName() !== 'Sheet1') { // if it's not the correct sheet we end the function here return; } var range = event.range; var row = range.getRow(); // we get the index of the row of the edited cell var dataRange = sheet.getRange("A" + row + ":C" + row); var values = dataRange.getValues(); var rowValues = values[0]; var recipient = rowValues[0]; var email = rowValues[1]; var refillsNumber = rowValues[2]; if (refillsNumber = 2) { // if 'refillsNumber' is not equal to 2 we end the function here var message = 'Dear ' + recipient + ',\n\n'+ 'You have ' + refillsNumber + ' remaining would you like to buy more?' + '\n\n' + 'Kind regards,' + '\n\n' + 'The revol team.'; var subject = 'Refill reminder'; MailApp.sendEmail(email,subject,message); return } if (refillsNumber !== 0) { return} var message = 'Dear ' + recipient + ',\n\n'+ 'You have ' + refillsNumber + ' remaining would you like to buy more? If not then would you like to end your subsription?' + '\n\n' + 'Kind regards,' + '\n\n' + 'The revol team.'; var subject = 'Refill reminder'; MailApp.sendEmail(email,subject,message); return } // in addition to this script which aims to send an email to the customer when they only have 2 refill remaining - /// I also want to set up a function that sends an email when they have 0 refills remaining __eq__方法的类的实例。

__hash__类型的对象不可哈希。

出现此错误的原因是因为list是列表列表。

侧面说明:可哈希表示在python中不可变

解决方案

您需要嵌套的迭代:

first

first_output = [[mapka[letter] for letter in word] for word in first] 也是如此。您还可以循环第一和第二步,以避免代码重复

答案 1 :(得分:0)

由于mapka将应用于字符,因此您应该在字符级别进行迭代。

在python中,迭代 string 会导致遍历字符。

所以这应该起作用:

data = ["hokejistov hokej", "strelena strela", "trener trenuje"] 
second = [[1, 2, 3, 4, 5], [7, 8, 10, 4, 11, 13], [8, 10, 4, 12, 14, 5, 4]]
mapka = {'h':1,'o':2,'k':3, 'e':4, 'j':5, 'i':6, 's':7, 't':8,'v':9, 
    'r':10, 'l':11, 'n':12, 'a':13, 'u':14}

first = [[word.lower() for word in text.split()[0]]for text in data]
second = [[word.lower() for word in text.split()[1]]for text in data]

print([[mapka[letter] for letter in word] for word in first])
print([[mapka[letter] for letter in word] for word in second])

答案 2 :(得分:0)

这是仅包含列表的解决方案:

# pretend to read your file
data = ["hokejistov hokej","strelena strela","trener trenuje"]

# a dictionary is not strictly needed here
mapka = "hokejistvrlnau"

# convert lines to columns
columns = []
for line in data:
    for i,word in enumerate(line.split()):
        try:
            columns[i].append(word)
        except IndexError:
            columns.append([word])

print(columns) # [['hokejistov', 'strelena', 'trener'], ['hokej', 'strela', 'trenuje']]

# look up each letter
for column in columns:
    print( [ [mapka.index(letter)+1  for letter in word] for word in column] )

# [[1, 2, 3, 4, 5, 6, 7, 8, 2, 9], [7, 8, 10, 4, 11, 4, 12, 13], [8, 10, 4, 12, 4, 10]]
# [[1, 2, 3, 4, 5], [7, 8, 10, 4, 11, 13], [8, 10, 4, 12, 14, 5, 4]]
相关问题