我试图在一个实时问题中使用到目前为止所学的知识,但似乎我仍然遗漏了一些部分,我想请求您的帮助
我有一个名为“debug.txt”的文件。该文件包含一些医生姓名和评论数量。有些医生的名字重复了不止一次,所以我想做的是对同一位医生的评论进行汇总,然后从评论次数最多的医生名字开始排序
Umberto Napoli 11
Prof.Giancarlo Pecorari 33
Edro Colombini 3
Fabrizio malandrinate 18
Nicola Kafalas 1
Maxillo Facciale 11
Ottaviani 4
Luca Cravero 2
Egle Muti 2
Massimiliano Garzaro 25
Salvatore Carlucci 34
Savino Bufo 185
Andrea Milanese 11
VincenzoDel Gaudio 221
Marco Marchetti 6
Andrea Nizza 9
Cosmer Torino 10
Mariafranca Maietta 14
Massimiliano Giuliano 24
Vito Contreas 23
Bellone Donato 69
AndreaRossi Todde 135
Franco Maniglia 33
Francesco Leva 11
MariaLuisa Pozzuoli 81
LaCliniqueof Switzerland
Luca Cravero 59
TheSwiss Clinic 9
GiulioMaria maggi 173
Umberto Napoli 55
Benoit Menye 243
Cristina Sartorio 6
Amisano Massimo 3
Massimo Dolcet 25
AlessandroMaria Caboni 236
Stefano Karoschitz 31
Alessandro Ticozzi 21
Francisco Malatesta 36
Massimo Dolcet 39
Corrado Adorno 8
Umberto Napoli 5
Mariarosa Romeo 117
Francesco Leva 17
Francesco Malatesta 23
Daniele Bollero 32
Paolo Tagliabue 2
Salvatore Carlucci 2
Gianluca Beninca 12
Paolo Gottarelli 23
Salvatore Carlucci 21
Dr.Massimo Dolcet
Salvatore Carlucci 20
Emanuele Zavattero 1
Luca Cravero 55
Marco Marchetti 3
Ssa Pozzuoli 51
Fabrizio Malandrinò 12
Enrico Donde 11
Alessandro Rivolin 8
Daniele Bollero 120
Nicola Vione 2
Egle Muti 6
Luigi Cursio 82
Salvatore Carlucci 21
Luca Cravero 1
Massimo Dolcet 7
GiulioMaria Maggi 35
Enrico Giachero
Dott Arturi 5
Marcello Cavallero 6
Stefano bruschi 4
Paolo Balocco 3
不幸的是,我只清理了一点txt,这是我正在使用的代码:
name = "debugging.txt"
handle = open(name)
for line in handle:
line = line.rstrip()
line = line.split()
try:
linezero = line[0]
lineone = line[1]
name = linezero + " " + lineone
review = line[2]
except:
continue
print(name,review)
这是你能做到的方法
如果您想打印医生的姓名及其评论,请将最后一行ie.
print(list(counter))
替换为print(dict(counter))
你已经把名字和评论分开了。您可能希望使用int()将审阅从str转换为int。
对于存储和排序,可以使用defaultdict。请参见下面的示例:
我建议使用检查每一行都在做什么,python有哪些工具
相关问题 更多 >
编程相关推荐