+ 2
A text file teams.txt contains the score obtained by all the teams of the Italian Football Championship, not ordered, a team p
A text file teams.txt contains the score obtained by all the teams of the Italian Football Championship, not ordered, a team pre row in the form: Juventus 82 Napoli 67 Roma 80 Inter 78 Write a program in order to read from file the list and provide an output as an ordered list in ascending order Example input: Example output: Juventus 82 Napoli 67 Napoli 67 Inter 78 Roma 80 Roma 80 Inter 78 Juventus 82
7 Réponses
+ 4
Thanks for the code, but as it has no indentation it is difficult to guess the logic of your code. Please provide a code as it should be in the playground, and then link it here.
+ 4
Can you show us your attempt in coding? Please link it here.
+ 3
fhand = open('teams.txt')
team_dict =dict()
teams=[]
scores=[]
for line in fhand:
team,score=line.split()
scores.append(int(score))
teams.append(team)
print(team,score)
##team_dict[team]=score
temp=0
temp_team=""
for i in range (len(teams)):
for j in range (len(scores)):
if scores[i] < scores[j]:
temp =scores[j]
temp_team=teams[j]
scores[j]=scores[i]
teams[j]=teams[i]
scores[i]=temp
teams[i]=temp_team
for i in range(len(teams)):
print(teams[i],scores[i])
+ 3
here is a code based on your code that should cover the most needs:
team_dict = dict()
with open('teams.txt') as fhand:
for line in fhand:
lst = line.strip().split() # split and remove whitespaces
lst[1] = int(lst[1]) # make score an int
team_dict.update({tuple(lst)}) # add tuple to dict
[print(*i) for i in team_dict.items()] # test only !
# variations for sorting and printing:
print()
[print(*i) for i in sorted(team_dict.items(),key=lambda x: x[0])] # asc to key
print()
[print(*i) for i in sorted(team_dict.items(),key=lambda x: x[0], reverse = True)] # dsc to key
print()
[print(*i) for i in sorted(team_dict.items(),key=lambda x: x[1])] # asc to value
print()
[print(*i) for i in sorted(team_dict.items(),key=lambda x: x[1], reverse = True)] # dsc to value
+ 2
Who knows python help
+ 2
Do you know pycharm
+ 2
Install and create text file teams.txt and compile