Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.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 - Fatal编程技术网

Python 每次按每个子列表项打印

Python 每次按每个子列表项打印,python,Python,到目前为止,几个小时后,我仍然无法理解这一点。任何人能提供的任何帮助或任何东西都将不胜感激。先谢谢你 stats.append({ 'rounds' : round_playing, 'round' : [{'name' : str(list_players[i]['name']), 'score' : list_players[i]['score']

到目前为止,几个小时后,我仍然无法理解这一点。任何人能提供的任何帮助或任何东西都将不胜感激。先谢谢你

 stats.append({
                'rounds' : round_playing,
                'round' : [{'name' : str(list_players[i]['name']),
                            'score' : list_players[i]['score']
                           }]
                })
列出玩家:

list_players.append({'name': '',
             'score': 0})
打印出来:

for s in stats:
            print("*************************************")
            print("Round: " + str(s['rounds']))
            print("*************************************")
            for p in s['round']:
                print("@" + str(p['name'])
                    + "\nScore: " + str(p['score'])+ "\n")

        print("*********************") 
============================
Round 1
============================
Player1
Score: 11

============================
Round 1
============================
Player2
Score: 23
============================
Round 1
============================
Player1
Score: 11

Player2
Score: 23

============================
Round 2
============================
Player1
Score: 55

Player2
Score: 7
当前:

for s in stats:
            print("*************************************")
            print("Round: " + str(s['rounds']))
            print("*************************************")
            for p in s['round']:
                print("@" + str(p['name'])
                    + "\nScore: " + str(p['score'])+ "\n")

        print("*********************") 
============================
Round 1
============================
Player1
Score: 11

============================
Round 1
============================
Player2
Score: 23
============================
Round 1
============================
Player1
Score: 11

Player2
Score: 23

============================
Round 2
============================
Player1
Score: 55

Player2
Score: 7
所需结果:

for s in stats:
            print("*************************************")
            print("Round: " + str(s['rounds']))
            print("*************************************")
            for p in s['round']:
                print("@" + str(p['name'])
                    + "\nScore: " + str(p['score'])+ "\n")

        print("*********************") 
============================
Round 1
============================
Player1
Score: 11

============================
Round 1
============================
Player2
Score: 23
============================
Round 1
============================
Player1
Score: 11

Player2
Score: 23

============================
Round 2
============================
Player1
Score: 55

Player2
Score: 7
有人提出这样的建议:

def buildList(p):
    for i in range(len(p)): 
        list_players.append({'name': '', 'score': 0})

不确定

TL;DR-不要将单人游戏条目附加到
stats
。相反,为每轮添加完整的数据(所有玩家)


代码中的问题是所使用的数据结构,这使得很难以正确的方式打印数据。如注释中所述,
stats
的每个元素中的
轮数
值的长度始终为
1
,因此您得到这样的输出

虽然可以使用这样的数据结构来处理
stats
,但考虑到您希望打印数据的格式,这可能不是最好的主意。最好收集一轮对应的所有数据。因此,我在回答中的想法是将与一轮收集的数据相对应的所有数据放入
stats
的单个元素中

假设-我假设在您构建
统计数据时,
列表玩家
会动态更新

构建
列出玩家
-保持原样,因为你打算浏览整个列表

构建
统计数据
-如果您的轮数总是从
1
开始并按顺序增长,您只需创建一个列表,其中索引
i
表示轮数
i+1
。如果您有更复杂的轮名,您可以使用字典,键为轮号,因为这样可以轻松访问特定轮号的统计信息

此外,在构建相同的数据结构时,您可以直接复制
list_players
,而不是在其上运行循环

有一份清单

for round in range(0, total_rounds):
    # Modify scores in `list_players` correctly
    # You have to copy the whole list, since `list_players` will
    # change over the course loop
    stats.append(list_players[:])
用字典

stats = {}
for round in round_numbers:
    # Modify scores of `list_players` correctly
    # Again, make sure you copy the list as it's dynamic
    stats[round] = list_players[:]
for key, value in stats.iteritems():
    # Always use 4 space indentation
    print("*************************************")
    print("Round: " + str(key)
    print("*************************************")
    for p in value:
        print("@" + str(p['name']) +
              "\nScore: " + str(p['score'])+ "\n")
        print("*********************") 
打印结果-

如果
stats
作为一个列表保存,则我已经使用了该函数

for index, round in enumerate(stats):
    # Always use 4 space indentation
    print("*************************************")
    print("Round: " + str(index+1)
    print("*************************************")
    for p in round:
        print("@" + str(p['name']) +
              "\nScore: " + str(p['score'])+ "\n")
        print("*********************") 
用字典

stats = {}
for round in round_numbers:
    # Modify scores of `list_players` correctly
    # Again, make sure you copy the list as it's dynamic
    stats[round] = list_players[:]
for key, value in stats.iteritems():
    # Always use 4 space indentation
    print("*************************************")
    print("Round: " + str(key)
    print("*************************************")
    for p in value:
        print("@" + str(p['name']) +
              "\nScore: " + str(p['score'])+ "\n")
        print("*********************") 

round
指向的数据始终是一个单项目列表。您基本上是将播放器分派到每个dict条目中,而不是收集它们。您发布的代码没有按原样运行,并且您问题中的数据不足以生成示例输出。为了获得体面帮助的最佳机会,请创建并发布体面的帮助。请阅读此重要链接以了解详细信息。您需要更改
list_players
的结构,以便它是一个字典列表,每个子列表对应于一轮中的玩家及其分数,在您必须执行的某一点上
s['round']。append()
,否则您的列表将有1个大小。您的数据结构似乎非常复杂。@JohnSmith您实际上只需要一个数据结构。返回到您构建的代码
列出玩家
,并对其进行更改,使其包含您需要的所有信息