计算元素[0]中的项数



我正在计算一个元素中有多少个团队。

下面是我的代码:

import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/avinashjairam/avinashjairam.github.io/master/question2_data%20.csv', header=None)
for index, rows in df.iterrows():

team_info = rows.to_list()

team_id = team_info[0]

wins = team_info[1]

losses = team_info[2]

ties = team_info[3]

print(team_id, wins, losses, ties)
print(f'Team {team_id} has {wins} wins, {losses} losses, and {ties} ties.')

total_games_played = wins + losses + ties

print(f'Total Games Played: {total_games_played}')

games_remaining = 16 - total_games_played

if games_remaining == 0:
print('The season is finished.')
else:
pass

print(f'Total Games Remaining: {games_remaining}')

winning_average = (wins/total_games_played) if total_games_played != 0 else 0

print(f'The Winning Average is: {winning_average:.4f}')

if (ties >= wins):
print('The number of tied games are greater than or equal to the number of wins.')
else:
print('The number of tied games are not greater than or equal to the number of wins.')


if ties >= losses:
print('The number of tied games are greater than the number of losses.')
else: 
print('The number of tied games are not greater than the number of losses.')


wip_total = (wins + ties) - (3 * losses) 


if wip_total < 0:
print(f'The Wip Total is: 0.')
else:
print(f'The Wip Total is: {wip_total}')
Total_Number_of_Teams = len(int(team_id))
print(f'The Total Number of Teams are {Total_Number_of_Teams}.')

但是,我收到这个错误消息:

TypeError                                 Traceback (most recent call last)
<ipython-input-101-f8b0a4f5d307> in <module>
----> 1 Total_Number_of_Teams = len(int(team_id))
2 print(f'The Total Number of Teams are {Total_Number_of_Teams}.')
TypeError: object of type 'int' has no len()

我尝试使用len()函数和count函数()来解决这个部分,但我收到相同的错误消息。如何计算team_id中的团队数量?

收到此错误的原因是len()函数需要一个对象,而不是一个"int"

我的建议是,当你填写列表时,将其转换为int:

team_id = int(team_info[0])

请注意,您的team_id只需要是int型数字,否则转换将失败。

要计算这个的长度,只需使用:

Total_Number_of_Teams = len(team_id)