Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用python上的循环将字典作为值插入字典_Python_Loops_Dictionary - Fatal编程技术网

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

如何使用python上的循环将字典作为值插入字典,python,loops,dictionary,Python,Loops,Dictionary,我目前面临的一个问题是如何将我的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"] = {} sam

我目前面临的一个问题是如何将我的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
但是我有很多数据要处理。 我想通过循环得到结果,但我已经尝试了2个小时,但失败了

---以下代码可能会使您感到困惑---

现在我的结果是这样的:

{'U1000': ['12222', 3],  
'U1001': ['13333', 4]}
  • dict的值是一个列表,而不是一个字典
  • 用户“U1000”出现多次,但在我的结果中只有一次
  • 我认为我的代码有很多错误。。如果您不介意,请看一下:

    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))
    
    如何改进代码? 我已经看过了,但由于个人能力不足, 谁能帮我一下吗


    非常感谢

    这应该满足您的要求:

    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}}
    
    是一个方便实用程序,每当您尝试访问字典中不存在的密钥时,它都会提供默认值。如果您不喜欢它(例如,因为您希望
    sample['non-existence-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
    

    这应该满足您的要求:

    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}}
    
    是一个方便实用程序,每当您尝试访问字典中不存在的密钥时,它都会提供默认值。如果您不喜欢它(例如,因为您希望
    sample['non-existence-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
    

    示例中的预期输出是不可能的,因为
    {'1333':2}
    不会与键相关联。您可以使用
    dict的
    dict
    获得
    {'U1000':{'12222':3,'1333':2},'U1001':{'13333':4}

    sample = {}
    for row in reader:
        userID, placeID, rating = row[:3]
        sample.setdefault(userID, {})[placeID] = rating  # Possibly int(rating)?
    
    或者,使用以避免需要(或涉及
    try
    /
    的替代方法,除了KeyError
    如果示例中的userID:
    牺牲
    setdefault
    的原子性以换取不必要地创建空的
    dict
    ):

    将其转换回普通
    dict
    可确保将来的查找不会自动激活键,将
    KeyError
    提升为正常,并且如果打印
    dict
    ,它看起来像正常的
    dict

    如果包含的
    列很重要(因为名称或列索引可能会更改),可以使用
    操作符.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)?
    

    示例中的预期输出是不可能的,因为
    {'1333':2}
    不会与键相关联。您可以使用
    dict的
    dict
    获得
    {'U1000':{'12222':3,'1333':2},'U1001':{'13333':4}

    sample = {}
    for row in reader:
        userID, placeID, rating = row[:3]
        sample.setdefault(userID, {})[placeID] = rating  # Possibly int(rating)?
    
    或者,使用以避免需要(或涉及
    try
    /
    的替代方法,除了KeyError
    如果示例中的userID:
    牺牲
    setdefault
    的原子性以换取不必要地创建空的
    dict
    ):

    将其转换回普通
    dict
    可确保将来的查找不会自动激活键,将
    KeyError
    提升为正常,并且如果打印
    dict
    ,它看起来像正常的
    dict

    如果包含的
    列很重要(因为名称或列索引可能会更改),可以使用
    操作符.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)?
    

    这似乎是希望字典作为值,而不是键。也许可以更正标题以匹配?谢谢您的提醒。已经更正了标题和内容!另外,您的示例有
    {'U1000':{'12222':3},{'1333':2},'U1001':{'13333':4}
    ,但这是映射
    U1000
    U1001
    ,但没有与
    {'1333':2}
    关联的键(或没有值)。你可以有
    {'U1000':{'12222':3,'1333':2},'U1001':{'13333':4}
    ,或者
    {'U1000':[{'12222':3},{'1333':2},'U1001':[{'13333':4}
    ,但不是你提供的。我明白你的意思。谢谢你的澄清。我已经提前纠正了身体。谢谢这似乎是希望字典作为值,而不是键。也许可以更正标题以匹配?谢谢您的提醒。已经更正了标题和内容!另外,您的示例有
    {'U1000':{'12222':3},{'1333':2},'U1001':{'13333':4}
    ,但这是映射
    U1000
    U1001
    ,但没有与
    {'1333':2}
    关联的键(或没有值)。你可以有
    {'U1000':{'12222':3,'1333':2},'U1001':{'13333':4}
    ,或者
    {'U1000':[{'12222':3},{'1333':2},'U1001':[{'13333':4}
    ,但不是你提供的。我明白你的意思。谢谢你的澄清。我已经提前纠正了身体。谢谢谢谢你的澄清,这真的很有帮助!谢谢你的澄清,这真的很有帮助!谢谢你的回答,这真的很有帮助!谢谢你的回答,这真的很有帮助!