簡體   English   中英

Flask 類型錯誤:字符串索引必須是整數

[英]Flask TypeError: string indices must be integers

對於刪除方法,我在 Flask 項目中遇到此錯誤。 TypeError:字符串索引必須是整數

我該如何解決?

def delete(self,player_name):
        global nba_players_json
        nba_players_json = list(filter(lambda x : x['name'] != player_name, nba_players_json))
        return {'message' : 'Item deleted'}

這是我的字典的樣子:

"AJ Price": {
        "Name": "AJ Price",
        "Games Played": "26",
        "MIN": "324",
        "PTS": "133",
        "FGM": "51",
        "FGA": "137",
        "FG%": "37.2",
        "3PM": "15",
        "3PA": "57",
        "3P%": "26.3",
        "FTM": "16",
        "FTA": "24",
        "FT%": "66.7",
        "OREB": "6",
        "DREB": "26",
        "REB": "32",
        "AST": "46",
        "STL": "7",
        "BLK": "0",
        "TOV": "14",
        "PF": "15",
        "EFF": "110",
        "AST/TOV": "3.29",
        "STL/TOV": "0.5",
        "Age": "29",
        "Birth_Place": "us",
        "Birthdate": "October 7, 1986",
        "Collage": "University of Connecticut",
        "Experience": "5",
        "Height": "185",
        "Pos": "PG",
        "Team": "PHO",
        "Weight": "81.45",
        "BMI": "23.79839299"}

見下文

nba_players_json = {"AJ Price": {
        "Name": "AJ Price",
        "Games Played": "26",
        "MIN": "324",
        "PTS": "133",
        "FGM": "51",
        "FGA": "137",
        "FG%": "37.2",
        "3PM": "15",
        "3PA": "57",
        "3P%": "26.3",
        "FTM": "16",
        "FTA": "24",
        "FT%": "66.7",
        "OREB": "6",
        "DREB": "26",
        "REB": "32",
        "AST": "46",
        "STL": "7",
        "BLK": "0",
        "TOV": "14",
        "PF": "15",
        "EFF": "110",
        "AST/TOV": "3.29",
        "STL/TOV": "0.5",
        "Age": "29",
        "Birth_Place": "us",
        "Birthdate": "October 7, 1986",
        "Collage": "University of Connecticut",
        "Experience": "5",
        "Height": "185",
        "Pos": "PG",
        "Team": "PHO",
        "Weight": "81.45",
        "BMI": "23.79839299"}}

def delete(player_name):
        global nba_players_json
        if player_name in nba_players_json:
          del nba_players_json[player_name]
          return {'message' : 'Item deleted'}
        else:
          return {'message' : 'Item not deleted'}

print(delete('AJ Price'))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM