簡體   English   中英

無法散列的類型:“ dict”

[英]unhashable type: 'dict'

我在這里是新手,想問一些有關刪除重復數據輸入的問題,現在我仍在做關於人臉識別的項目,並卡在刪除重復數據輸入中,並將其發送到Google表格,這是我使用的代碼:

if(confidence <100):
    id = names[id]
    confidence = "{0}%".format (round(100-confidence))
    row = (id,datetime.datetime,now().strftime('%Y-%m-%d %H:%M:%S'))
    index = 2
    sheet.insert_row (row,index)
    data = sheet.get_all_records()
    result = list(set(data))
    print (result)

消息錯誤“無法散列的類型:'dict”,我只想輸入一次就將結果發布到Google工作表中

您無法將字典添加到集合中。

可以做的是將字典項添加到集合中。 您可以像這樣將其強制轉換為元組列表:

s = set(tuple(data.items()))

如果之后需要將其轉換回字典,則可以執行以下操作:

for t in s:
    new_dict = dict(t)

根據gspread文檔, gspread get_all_records()返回字典 列表 ,其中dict的頭行為鍵,值為單元格值。 因此,您需要遍歷此列表,比較id來查找和刪除重復項。 樣例代碼:

visited = []
filtered = []
for row in data:
    if row['id'] not in visited:
        visited.append(row['id'])
    else:
        filtered.append(row)

現在,過濾后應包含唯一項。 但是,您應該輸入包含重復值的列名稱,而不是id

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM