Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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 如何使用列表作为每个嵌套dict的值来编写嵌套dict_Python_List_Dataframe_Dictionary_Nested - Fatal编程技术网

Python 如何使用列表作为每个嵌套dict的值来编写嵌套dict

Python 如何使用列表作为每个嵌套dict的值来编写嵌套dict,python,list,dataframe,dictionary,nested,Python,List,Dataframe,Dictionary,Nested,我想我知道我想要什么。我的输出需要有“团队”作为dict键,每个dict键都有一个嵌套的dict,在每个嵌套的dict中,键将是一个球员的名字,每个嵌套的dict键的值将是每场比赛的目标列表。我希望最终能够找出谁在每支球队中进球最多 我不知道数据帧是否更适合这个 编写嵌套dict的代码-我不知道怎么做 mydict = {"team" : {"players" : "goals_each_game"}} team = list(ran

我想我知道我想要什么。我的输出需要有“团队”作为dict键,每个dict键都有一个嵌套的dict,在每个嵌套的dict中,键将是一个球员的名字,每个嵌套的dict键的值将是每场比赛的目标列表。我希望最终能够找出谁在每支球队中进球最多

我不知道数据帧是否更适合这个

编写嵌套dict的代码-我不知道怎么做

mydict = {"team" : {"players" : "goals_each_game"}}

team = list(range(1, 7))
print("team : ", team)
players = ["gk", "lb", "dl", "dr", "rb", "rw", "rm", "lm", "lw", "ls", "rs" ]
goals_each_game = list(range(1,7))

for d in team:
    for k, v in mydict.items():
        mydict["team"] = d
        


        #mydict[v] = a nested dict of each player and their list of goals
        for p in players:

            teamlist = []

            new_dict = {}

            for k,v in new_dict.items():

                new_dict[k] = p

                new_dict[v] = goals_each_game

                teamlist.append(new_dict)


        mydict[v] = teamlist


            

for k,v in mydict.items():
    print(k,v)
预期产出

mydict = {"team" : {"players" : "goals_each_game"}}

team = list(range(1, 7))
print("team : ", team)
players = ["gk", "lb", "dl", "dr", "rb", "rw", "rm", "lm", "lw", "ls", "rs" ]
goals_each_game = list(range(1,7))

for d in team:
    for k, v in mydict.items():
        mydict["team"] = d
        


        #mydict[v] = a nested dict of each player and their list of goals
        for p in players:

            teamlist = []

            new_dict = {}

            for k,v in new_dict.items():

                new_dict[k] = p

                new_dict[v] = goals_each_game

                teamlist.append(new_dict)


        mydict[v] = teamlist


            

for k,v in mydict.items():
    print(k,v)
我想知道如何做到这一点,并将任何值列表放入嵌套的dict中,而不是[1,2,3]

mydict = {"1": [{"gk":[1,2,3]}, {"lb":[1,2,3]}] ,"2": [{"gk":[1,2,3]}, {"lb":[1,2,3]}], "3": [{"gk":[1,2,3]}, {"lb":[1,2,3]}] ,"4": [{"gk":[1,2,3]}, {"lb":[1,2,3]}] }


for k,v in mydict.items():
    print(k,v)

team :  [1, 2, 3, 4, 5, 6]
1 [{'gk': [1, 2, 3]}, {'lb': [1, 2, 3]}]
2 [{'gk': [1, 2, 3]}, {'lb': [1, 2, 3]}]
3 [{'gk': [1, 2, 3]}, {'lb': [1, 2, 3]}]
4 [{'gk': [1, 2, 3]}, {'lb': [1, 2, 3]}]

最外层的字典是一个字典,它的键是团队索引,值是列表

如果你的目标是让每个值都成为得分球员姓名首字母的字典,那么你希望
teamlist
成为
{}
(并且调用
teamlist.append
替换为赋值)

你可以用更地道的方式来理解:

goals_per_game = list(range(1, 7))

mapping = {
    d: {
        p: goals_per_game
        for p in players
    }
    for d in teams
}
这里有一些警告。第一,所有球队都有相同的球员;您可能需要维护一个单独的字典,将团队映射到玩家姓名首字母

第二点是,正如所写-
每场比赛的目标在所有玩家中都是相同的:

Python中的赋值语句不复制对象,而是在目标和对象之间创建绑定

这意味着在上述实现中,对一个玩家的列表进行变异将对所有玩家的列表进行变异。考虑:

mapping[1]['gk'].append(100)

print(mapping[2]['lb'])  # Has 100!

为了解决这个问题,您可以为每个玩家创建一个新的列表(即分别调用
list o range
),或者为每个玩家创建一个副本。

谢谢,goas_每个游戏的列表对于每个玩家都是不同的。谢谢你。