python - Flask TypeError:字符串索引必须是整数
问题描述
我的 Flask 项目中的 delete 方法出现此错误。 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'))
推荐阅读
- python - 使用 qt 后端识别 matplolib 中的鼠标单击事件
- javascript - 如何使用 vue-multiselect 进行动态多个下拉菜单
- javascript - 当我在 webpackV5 中使用 webpack 服务时,__webpack_exports__ 不正确
- node.js - 如何生成“非重复 7 位字母数字”字符串,如 git commit
- iphone - 有没有办法可以在 iOS 15 Beta 2 上使用我的 iPhone 来模拟我必须打开社区大门的 RFID 钥匙?
- python - 在 python 中使用 .format() 替换字符串的一部分
- swift - 可以在协议中动态更改 UILabel 值吗?
- php - laravel-imap,仅获取正文的文本内容的问题
- c++ - C++ 原子变量可见性
- echarts - Y 轴标签溢出图表?