我正试图从我的3d列表中打印出所有只为"Okc"队效力的球员的信息行,但我不知道如何做到。
列表的格式如下:[[Player,Position,Team,etc.],[Player,Position,Team,etc.]],[Playerer,Position,Team.,etc.]]
import csv
f = open('nba-2019-20.csv', 'r')
reader = csv.reader(f)
players = []
for row in reader:
del row[6:10]
del row[21:24]
row.pop(7)
row.pop(8)
del row[9:11]
row.pop(11)
row.pop(12)
players.append(row)
x = "Okc"
for dat in players:
if dat[1] == x:
print(dat)
正如你所做的(如果我理解你想做什么的话(-与@wjandrea注释相同:
for dat in players:
if dat[2] == x:
print(dat)
列表理解:
print([dat for dat in players if dat[2]==x])
另一种解决方案是在Pandas Dataframe中进行(我假设列在csv中有名称(,并在"团队"列上进行过滤:
import pandas as pd
df = pd.read_csv('nba-2019-20.csv')
print(df[df['Team'] == x]]) #returns a numpy series