我目前面临一个问题,要把我的cvs数据变成字典。
我想在文件中使用3列:
userID, placeID, rating
U1000, 12222, 3
U1000, 13333, 2
U1001, 13333, 4我想把结果看上去如下:
{'U1000': {'12222': 3, '13333': 2},
'U1001': {'13333': 4}}也就是说,我想使我的数据结构看起来像:
sample = {}
sample["U1000"] = {}
sample["U1001"] = {}
sample["U1000"]["12222"] = 3
sample["U1000"]["13333"] = 2
sample["U1001"]["13333"] = 4但我有很多数据要处理。我想用循环得到结果,但我已经试了两个小时,但失败了。
-以下代码可能会把你搞糊涂
我的结果现在是这样的:
{'U1000': ['12222', 3],
'U1001': ['13333', 4]}我想我的代码有很多错误。如果你不介意,请看一看:
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))我怎样才能改进代码?我已经通过堆叠溢出,但由于个人能力不足,谁能请您帮助我这一点?
非常感谢!!
发布于 2016-03-02 18:16:35
这应该是你想做的事:
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一起失败),请使用以下命令:
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发布于 2016-03-02 18:22:06
本例中的预期输出是不可能的,因为{'1333': 2}不会与键相关联。不过,您可以使用dict of dicts获得dict:
sample = {}
for row in reader:
userID, placeID, rating = row[:3]
sample.setdefault(userID, {})[placeID] = rating # Possibly int(rating)?或者,使用collections.defaultdict(dict)来避免对setdefault的需求(或涉及try/except KeyError或if userID in sample:的替代方法,这些方法牺牲了setdefault的原子性,而不是不必要地创建空的dict):
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来一次加快和简化所有想要的列的提取:
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)?https://stackoverflow.com/questions/35754394
复制相似问题