Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/335.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

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 如何将许多大小未知的列表放入字典中_Python_List_Dictionary - Fatal编程技术网

Python 如何将许多大小未知的列表放入字典中

Python 如何将许多大小未知的列表放入字典中,python,list,dictionary,Python,List,Dictionary,我正在处理许多表,这些表的行数不一样。使用for循环,我可以遍历这些表的内容并将其放入两个列表中。对于For循环的每次迭代,这些列表的内容将根据这些表的内容进行更改。之后,我希望将每次迭代的这些列表的内容放在一个列表中 例如: 我可以手动将下面两个列表的内容放入字典中,但如果我有两个维度已知的列表(行数已知),这应该可以,但是如果我有两个行数未知的列表,这是行不通的 表:1 player1 = ['1','2','3'] player2 = ['4','5','6'] d ={} d['

我正在处理许多表,这些表的行数不一样。使用for循环,我可以遍历这些表的内容并将其放入两个列表中。对于For循环的每次迭代,这些列表的内容将根据这些表的内容进行更改。之后,我希望将每次迭代的这些列表的内容放在一个列表中

例如:

我可以手动将下面两个列表的内容放入字典中,但如果我有两个维度已知的列表(行数已知),这应该可以,但是如果我有两个行数未知的列表,这是行不通的

表:1

player1 = ['1','2','3']

player2 = ['4','5','6']


d ={}

d['players'] = {}

d['players']['player1'] = []

d['players']['player1'].append(T1[0])

d['players']['player1'].append(T2[0])

d['players']['player2'] = []

d['players']['player2'].append(T1[1])

d['players']['player2'].append(T2[1])

d['players']['player3'] = []

d['players']['player3'].append(T1[2])

d['players']['player3'].append(T2[2])

print(d)

### {'players': {'player1': ['1', '4'], 'player2': ['2', '5'], 'player3': ['3', '6']}}
player1 = ['7','8','9',.....]

player2 = ['1','4','7',.....]
如果我有来自另一个表的另外两个列表,其中的行数未知,并且希望添加到另一个字典中,该字典的值(元素)将随着for循环的每次迭代而更改,我该怎么办

例如:

表格:2

player1 = ['1','2','3']

player2 = ['4','5','6']


d ={}

d['players'] = {}

d['players']['player1'] = []

d['players']['player1'].append(T1[0])

d['players']['player1'].append(T2[0])

d['players']['player2'] = []

d['players']['player2'].append(T1[1])

d['players']['player2'].append(T2[1])

d['players']['player3'] = []

d['players']['player3'].append(T1[2])

d['players']['player3'].append(T2[2])

print(d)

### {'players': {'player1': ['1', '4'], 'player2': ['2', '5'], 'player3': ['3', '6']}}
player1 = ['7','8','9',.....]

player2 = ['1','4','7',.....]
词典应为:

d = {'players': {'player1': ['7', '1'], 'player2': ['8', '4'], 'player3': ['9', '7'], .......}}
最后,我想把上面这两本字典添加到一个列表中

List = [{'players': {'player1': ['1', '4'], 'player2': ['2', '5'], 'player3': ['3', '6']}}, {'players': {'player1': ['7', '1'], 'player2': ['8', '4'], 'player3': ['9', '7'], .......}}]

可以使用类似于以下代码中的for循环来完成此操作:

d = {'players':{}}

for i in range(len(player1)): # Assuming length if player1 is same as player2
   player = 'player'+str(i+1) #String Concat 
   d['players'][player] = [player1[i],player2[I]]
要将它们存储在列表中,可以对第一个表使用
d1
替换
d
,对第二个表使用
d2
替换,如下所示

final_list = [d1,d2]

可以使用类似于以下代码中的for循环来完成此操作:

d = {'players':{}}

for i in range(len(player1)): # Assuming length if player1 is same as player2
   player = 'player'+str(i+1) #String Concat 
   d['players'][player] = [player1[i],player2[I]]
要将它们存储在列表中,可以对第一个表使用
d1
替换
d
,对第二个表使用
d2
替换,如下所示

final_list = [d1,d2]

通过压缩列表并列举以下内容,您可以通过dict理解获得所需的结果:

player1 = ['1','2','3']
player2 = ['4','5','6']

d ={}
d['players'] = {'player'+str(i+1): list(tup) for i, tup in enumerate(zip(player1, player2))}
d
将是:

{'players': {
  'player1': ['1', '4'],
  'player2': ['2', '5'],
  'player3': ['3', '6']}
}

而不是在DICT中使用基于数字的键,您可以考虑只使用列表:

player1 = ['1','2','3']
player2 = ['4','5','6']


d ={}
d['players'] = [list(tup) for i, tup in enumerate(zip(player1, player2))]

# access with index instead of player_index
d['players'][0]
# ['1', '4']

通过压缩列表并列举以下内容,您可以通过dict理解获得所需的结果:

player1 = ['1','2','3']
player2 = ['4','5','6']

d ={}
d['players'] = {'player'+str(i+1): list(tup) for i, tup in enumerate(zip(player1, player2))}
d
将是:

{'players': {
  'player1': ['1', '4'],
  'player2': ['2', '5'],
  'player3': ['3', '6']}
}

而不是在DICT中使用基于数字的键,您可以考虑只使用列表:

player1 = ['1','2','3']
player2 = ['4','5','6']


d ={}
d['players'] = [list(tup) for i, tup in enumerate(zip(player1, player2))]

# access with index instead of player_index
d['players'][0]
# ['1', '4']

我想我对你的问题得到了一个非常简单的答案。 只要试试这个代码。。。它应该是你正在寻找的

player1 = ["7", "8", "9"]

player2 = ["1", "4", "7"]

d = {"players": [{str(player1), str(player2)}]}

d2 = {"players": [{str(player1), str(player2)}]}

dList = []

for player in d:
    dList.append(d)
    dList.append(d2)

我想我对你的问题得到了一个非常简单的答案。 只要试试这个代码。。。它应该是你正在寻找的

player1 = ["7", "8", "9"]

player2 = ["1", "4", "7"]

d = {"players": [{str(player1), str(player2)}]}

d2 = {"players": [{str(player1), str(player2)}]}

dList = []

for player in d:
    dList.append(d)
    dList.append(d2)

如果您有
d1
d2
,您的列表就是
[d1,d2]
。查看defaultdict(列表)非常感谢如果您有
d1
d2
,您的列表就是
[d1,d2]
。查看defaultdict(列表)非常感谢您