固定非类型

2024-09-28 19:20:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写一个代码来跟踪作为字符串列表返回的国家获得的奖牌。格式示例:

"COUNTRY G S B"

以下是我目前的代码:

def generate(results):
    """
    return list of strings
    based on data in results, a list of strings
    """

    #  [country_code, gold_count, silver_count, bronze_count ]
    allCountries = []
    for i in results:
        x = i.split()
        for country in x:
            allCountries.append(country)
    allCountries = set(allCountries)
    medalTracker = []
    medalTracker = [medalTracker.append(i) for i in allCountries]
    # medalTracker = list(medalTracker)
    for i in medalTracker:
        goldCount, silverCount, bronzeCount = 0, 0, 0
        Idx = medalTracker.index(i)
        medalTracker[Idx].append(goldCount, silverCount, bronzeCount)
        for rank in results:
            if rank [1] == i:
                goldCount += 1
            elif rank [2] == i:
                silverCount += 1
            elif rank[3] == i:
                bronzeCount += 1

print(generate(["ITA JPN AUS", "KOR TPE UKR", "KOR KOR GBR", "KOR CHN TPE"]))

应返回:

[ "KOR 3 1 0",  "ITA 1 0 0",  "TPE 0 1 1",  "CHN 0 1 0",  "JPN 0 1 0",  
  "AUS 0 0 1",  "GBR 0 0 1",  "UKR 0 0 1" ]

我在第20行遇到一个非类型错误:

medalTracker[Idx].append(goldCount, silverCount, bronzeCount)

我不知道为什么。 有人能解释一下我怎么能解决这个问题吗? 提前谢谢!你知道吗


Tags: inforcountcountryresultslistrankappend
3条回答

您的问题存在于以下行:

medalTracker = [medalTracker.append(i) for i in allCountries]

当您append一个list时,它会修改list,但返回None。你可以更清楚地看到这里发生了什么:

allCountries = ['ITA', 'JPN', 'AUS', 'KOR', 'TPE']   # This is essentially the shorter equivalent of your list.

medalTracker = []

def show_medalTracker_detail():
    print(f'medalTracker id: {id(medalTracker)}, values: {medalTracker}')

def peek(x):
    result = medalTracker.append(x)
    show_medalTracker_detail()
    return result

运行时:

>>> show_medalTracker_detail()

medalTracker id: 52731616, values: []

>>> medalTracker = [peek(i) for i in allCountries]

medalTracker id: 52731616, values: ['ITA']
medalTracker id: 52731616, values: ['ITA', 'JPN']
medalTracker id: 52731616, values: ['ITA', 'JPN', 'AUS']
medalTracker id: 52731616, values: ['ITA', 'JPN', 'AUS', 'KOR']
medalTracker id: 52731616, values: ['ITA', 'JPN', 'AUS', 'KOR', 'TPE']

>>> show_medalTracker_detail()

medalTracker id: 52736448, values: [None, None, None, None, None]

注意到medalTracker对象id和值在理解列表后是如何变化的吗?这是因为在列表理解完成循环和追加之后,它还创建了一个None列表,并且正在重新分配该列表(一个新对象)以替换现有的medalTracker。你知道吗

解决方案是只使用medalTracker = list(allCountries),或者如果要追加,则使用for循环:

for i in allCountries:
    medalTracker.append(i)

首先,一般不赞成用大写字母来表示除类以外的任何事物。继续。。。你知道吗

medalTracker = [medalTracker.append(i) for i in allCountries]

问题源于这一行,它返回一个None的列表

<class 'list'>: [None, None, None, None, None, None, None, None]

还有一些其他问题,即尝试将一些数据附加到字符串中。请看一下这个代码,我想它会给你的结果,你正在寻找

def generate(results):
"""
return list of strings
based on data in results, a list of strings
"""

#  [country_code, gold_count, silver_count, bronze_count ]
allCountries = []
for i in results:
    x = i.split()
    for country in x:
        allCountries.append(country)
allCountries = list(set(allCountries))
medalTracker = []
# medalTracker = list(medalTracker)
for i in allCountries:
    goldCount, silverCount, bronzeCount = 0, 0, 0
    index = allCountries.index(i)
    for rank in results:
        rank = rank.split(' ')
        if rank[0] == i:
            goldCount += 1
        elif rank[1] == i:
            silverCount += 1
        elif rank[2] == i:
            bronzeCount += 1
    medalTracker.append({i: {'goldCount': goldCount, 'silverCount': silverCount, 'bronzeCount': bronzeCount}})

print(generate(["ITA JPN AUS", "KOR TPE UKR", "KOR KOR GBR", "KOR CHN TPE"]))

你的代码有几个问题:

  • 你的medalTracker列表没有值。您可以使用list(allCountries)生成列表
  • 您将附加到medalTracker值,这些值是字符串。你知道吗
  • 你没有在medalTracker的等级计算后更新奖牌数量
  • 你把国家的第一个字母和全名进行比较,所以奖牌的价值总是为0

这是一个更新的工作版本

def generate(results):
    """
    return list of strings
    based on data in results, a list of strings
    """

    #  [country_code, gold_count, silver_count, bronze_count ]
    allCountries = []
    for i in results:
        x = i.split()
        for country in x:
            allCountries.append(country)

    medalTracker = list(set(allCountries))
    for i in medalTracker:
        goldCount, silverCount, bronzeCount = 0, 0, 0
        Idx = medalTracker.index(i)
        for rank in results:
            ranks = rank.split()
            if ranks[0] == i:
                goldCount += 1
            elif ranks[1] == i:
                silverCount += 1
            elif ranks[2] == i:
                bronzeCount += 1

        medalTracker[Idx] = medalTracker[Idx] + " " + str(goldCount) + " " + str(silverCount) + " " + str(bronzeCount)
    return medalTracker

print(generate(["ITA JPN AUS", "KOR TPE UKR", "KOR KOR GBR", "KOR CHN TPE"]))

输出

['TPE 0 1 1', 'JPN 0 1 0', 'UKR 0 0 1', 'CHN 0 1 0', 'ITA 1 0 0', 'AUS 0 0 1', 'GBR 0 0 1', 'KOR 3 0 0']

相关问题 更多 >