首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用python上的循环将字典作为值插入字典

如何使用python上的循环将字典作为值插入字典
EN

Stack Overflow用户
提问于 2016-03-02 17:41:00
回答 2查看 179关注 0票数 0

我目前面临一个问题,要把我的cvs数据变成字典。

我想在文件中使用3列:

代码语言:javascript
复制
userID, placeID, rating
U1000,  12222,   3
U1000,  13333,   2
U1001,  13333,   4

我想把结果看上去如下:

代码语言:javascript
复制
{'U1000': {'12222': 3, '13333': 2}, 
'U1001': {'13333': 4}}

也就是说,我想使我的数据结构看起来像:

代码语言:javascript
复制
sample = {}
sample["U1000"] = {}
sample["U1001"] = {}
sample["U1000"]["12222"] = 3
sample["U1000"]["13333"] = 2
sample["U1001"]["13333"] = 4

但我有很多数据要处理。我想用循环得到结果,但我已经试了两个小时,但失败了。

-以下代码可能会把你搞糊涂

我的结果现在是这样的:

代码语言:javascript
复制
{'U1000': ['12222', 3],  
'U1001': ['13333', 4]}
  1. dict的值是一个列表,而不是一个字典。
  2. 用户"U1000“多次出现,但在我的结果中只有一次

我想我的代码有很多错误。如果你不介意,请看一看:

代码语言:javascript
复制
reader = np.array(pd.read_csv("rating_final.csv"))
included_cols = [0, 1, 2]

sample= {}
target=[]
target1 =[]
for row in reader:
        content = list(row[i] for i in included_cols)
        target.append(content[0])
        target1.append(content[1:3])

sample = dict(zip(target, target1))

我怎样才能改进代码?我已经通过堆叠溢出,但由于个人能力不足,谁能请您帮助我这一点?

非常感谢!!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-03-02 18:16:35

这应该是你想做的事:

代码语言:javascript
复制
import collections

reader = ...
sample = collections.defaultdict(dict)

for user_id, place_id, rating in reader:
    rating = int(rating)
    sample[user_id][place_id] = rating

print(sample)
# -> {'U1000': {'12222': 3, '1333': 2}, 'U1001': {'13333': 4}}

defaultdict是一种方便的实用工具,每当您尝试访问字典中没有的键时,它就会提供默认值。如果您不喜欢它(例如,因为您希望sample['non-existent-user-id]KeyError一起失败),请使用以下命令:

代码语言:javascript
复制
reader = ...
sample = {}

for user_id, place_id, rating in reader:
    rating = int(rating)
    if user_id not in sample:
        sample[user_id] = {}
    sample[user_id][place_id] = rating
票数 2
EN

Stack Overflow用户

发布于 2016-03-02 18:22:06

本例中的预期输出是不可能的,因为{'1333': 2}不会与键相关联。不过,您可以使用dict of dicts获得dict

代码语言:javascript
复制
sample = {}
for row in reader:
    userID, placeID, rating = row[:3]
    sample.setdefault(userID, {})[placeID] = rating  # Possibly int(rating)?

或者,使用collections.defaultdict(dict)来避免对setdefault的需求(或涉及try/except KeyErrorif userID in sample:的替代方法,这些方法牺牲了setdefault的原子性,而不是不必要地创建空的dict):

代码语言:javascript
复制
import collections

sample = collections.defaultdict(dict)
for row in reader:
    userID, placeID, rating = row[:3]
    sample[userID][placeID] = rating

# Optional conversion back to plain dict
sample = dict(sample)

返回到普通dict的转换确保了以后的查找不会自动显示键,从而提高KeyError的正常值,如果您将其转换成普通的dict,那么它看起来就像普通的dict

如果included_cols很重要(因为名称或列索引可能会发生变化),您可以使用operator.itemgetter来一次加快和简化所有想要的列的提取:

代码语言:javascript
复制
from collections import defaultdict
from operator import itemgetter

included_cols = (0, 1, 2)
# If columns in data were actually:
# rating, foo, bar, userID, placeID
# we'd do this instead, itemgetter will handle all the rest:
# included_cols = (3, 4, 0)
get_cols = itemgetter(*included_cols)  # Create function to get needed indices at once

sample = defaultdict(dict)
# map(get_cols, ...) efficiently converts each row to a tuple of just 
# the three desired values as it goes, which also lets us unpack directly
# in the for loop, simplifying code even more by naming all variables directly
for userID, placeID, rating in map(get_cols, reader):
    sample[userID][placeID] = rating  # Possibly int(rating)?
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35754394

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档