Я использовал Pycharm, чтобы исправить эту проблему. Я попытался указать конкретный диапазон. Я рассматривал количество пользователей +=1 каждый раз, когда что-то печаталось, но не мог ввести это в код без возникновения ошибки.
мой вывод
07: Rules of Engagement; Medium; Lux Video Theatre
08: Mama; Barney Miller; Castle
10: Will & Grace; Smallville; Modern Family; Friends
11: Cheers; The Jeffersons
12: Murder, She Wrote; NYPD Blue
14: Dallas; Bonanza
15: ER
20: Gunsmoke; Law & Order; Law & Order: Special Victims Unit
30: The Simpsons
''''
expected output
7: Rules of Engagement; Medium; Lux Video Theatre
8: Mama; Barney Miller; Castle
10: Will & Grace; Smallville; Modern Family; Friends
11: Cheers; The Jeffersons
12: Murder, She Wrote; NYPD Blue
14: Dallas; Bonanza
15: ER
20: Gunsmoke; Law & Order; Law & Order: Special Victims Unit
30: The Simpsons
'''
''''
file = input() #user file input
with open(file) as f: #open file
data = f.readlines()
dict_info = {}
for i in range(0, len(data)-1, 2):
season = data.strip()
name = data[i+1].strip()
if(season in dict_info):
dict_info[season].append(name)
else:
dict_info[season] = [name]
#OUTPUT KEYS
keys = list(dict_info.keys()) #will list the dictionary keys
keys.sort() #will sort the keys from min to max
with open('output_keys.txt', 'w') as f:
for key in keys:
names = '; '.join(name for name in dict_info[key]) #will print the name with number
f.write(str(key)+': '+names+"\n")
names = [] #Dictionary value
for item in dict_info: #will add name to dict
for name in dict_info[item]:
names.append(name)
#OUTPUT TITTLE
names.sort() #sort names from min to max
with open('output_titles.txt', 'w') as f: #open the file
for name in names: #will write name plus a new line
f.write(name+'\n')
Подробнее здесь: https://stackoverflow.com/questions/656 ... -and-lists
Лабораторная работа: Сортировка телепередач (словари и списки) ⇐ Python
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение