gpt4 book ai didi

Python添加列表作为字典中的键

转载 作者:太空宇宙 更新时间:2023-11-03 17:44:40 25 4
gpt4 key购买 nike

我正在尝试创建一个字典,其中键是 date ,
就我而言,每个日期都是这样的列表:[2012, 5, 25]

这是我从 csv 文件读取到字典中的代码。
此代码已给出,我无法更改它:

def read_profiles(r_file_name):
"""Reads the file profiles-full.csv into a dictionary where the key is the users id
and the value is a dictionary with users fields "public", "compl_percent", "gender",
"region", "last_login", "age", "body". In addition to this dictionary, the function
returns the list of all users that have 0 completion percentage."""
users = {}
noinfo_users = []
with open(r_file_name, 'rb') as csvfile:
linereader = csv.reader(csvfile)
for line in linereader:
profile = {}
data_fields = ("public", "compl_percent", "gender", "region",
"last_login", "age", "body")
for field, raw_data in zip(data_fields, line[1:]):
if field == "compl_percent" and int(raw_data) == 0:
noinfo_users.append(int(line[0]))

if raw_data == "null":
profile[field] = None
elif field == "body":
profile[field] = raw_data
elif field == "last_login": #<=== reading last_login
profile[field] = map(int, raw_data.split("-"))
elif field == "region":
profile[field] = map(int, raw_data.split(";"))
else:
profile[field] = int(raw_data)

users[int(line[0])] = profile
return users, noinfo_users

这是 csv 文件的内容,对应于这个模式:

"public", "compl_percent", "gender", "region",                  "last_login", "age", "body"**
231702, 1, 60, 0, 1;1;21, 2012-05-15, 0, 171 cm;58 kg

这是配置文件字典中的元素的样子:

1492433: {'body': '160 cm;54 kg', 'compl_percent': 78, 'gender': 0, 'region': [1, 10, 6], 'age': 36, 'last_login': [2012, 5, 25], 'public': 1}

这是我的功能:

def getStrongConnectedForAttr(user,edges,profiles,attr):
result = dict()
if user in edges:
userFriends = edges.get(user)
for friend in userFriends:
if isBidirectional(edges,user,friend) == 2:
if friend in profiles:
friendAttr = (profiles.get(friend))[str(attr)]
if attr == "last_login":
#friendAttr = '-'.join(map(str, friendAttr))
friendAttr = tuple(friendAttr)
if friendAttr in result: #<===== error
result[friendAttr] = result.get(friendAttr) + 1
else:
result[friendAttr] = 1
return sorted(result.items(), key=operator.itemgetter(1), reverse=True)
else:
return result

它将配置文件作为参数之一,并构建一个空字典。
iffriendAttr in result: 行中,我收到错误:

TypeError: unhashable type: 'list'

我尝试在网络上搜索解决方案,发现了很多,尤其是在 Stack Overflow 上,正如你所看到的,我尝试了很多解决方案,其中一个将列表转换为元组,或者将列表连接为字符串。
但它们都不起作用。

最佳答案

您只是将 login_attr 值转换为元组,但忘记了 region 属性。 那些值仍然会引发异常。

您仅在此处测试 login_attr:

if attr == "last_login":
friendAttr = tuple(friendAttr)

无需将值转换为元组,只需在读取文件时存储元组即可。

替换

elif field == "last_login": #<=== reading last_login
profile[field] = map(int, raw_data.split("-"))
elif field == "region":
profile[field] = map(int, raw_data.split(";"))

elif field == "last_login":
profile[field] = tuple(map(int, raw_data.split("-")))
elif field == "region":
profile[field] = tuple(map(int, raw_data.split(";")))

关于Python添加列表作为字典中的键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29990075/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com