如何通过使用附加整数的帐户名来区分不同的帐户名,来详细说明我的程序

  • 本文关键字:说明 程序 整数 何通过 python python-3.x
  • 更新时间 :
  • 英文 :


这是我的问题,对于上面的帐户名,如果帐户名已经存在,请使用附加整数的帐户名来区分不同的帐户名。例如,如果有三名患者具有相同的帐户名称"abranch",则第二名患者应命名为"abranch1",第三名患者应名称为"abrench2"。如何通过使用附加整数的帐户名来区分不同的帐户名,来详细说明我的程序?

这是我生成名字列表的代码:

with open('all_patients.txt', 'r') as fh:

for line in fh:
splited_list = line.split()
surname = splited_list[1]
given_name = splited_list[0][0]
patient_name_u = given_name + surname
patient_name.append(patient_name_u.lower())
fh.close()
print(patient_name)

如果你的文件看起来像这个

abranch
abranch
abranch

然后您可以使用Counter

from collections import Counter
with open("file.txt") as f:
d = [f"{names}_{time}" for names, times in Counter(f.read().splitlines()).items() for time in range(times) ]
print(d)
#['abranch_0', 'abranch_1', 'abranch_2']

最新更新