菲fay废 2022-04-10 23:30 采纳率: 100%
浏览 77
已结题

关于名字排列等,三克油

img


想问一下,怎么样将名字排序,希望能具体讲一下此题
file里面包含名字
andrew johnson
carl brown
james wellington
hannah metcalfe
carl faith dyer
maria arnold
colin hudson
edward martin
irene sharp
william ross
elizabeth miller
nicola terry
zoe karen walker
rachael short
alison gibson

  • 写回答

3条回答 默认 最新

  • GavinHaydy 2022-04-11 00:51
    关注
    
    from random import randint
    def id_creat():
        std_id = randint(1,1000000)
        if std_id < 100000:
            std_id = (6 - len(str(std_id))) * '0' + str(std_id)
        return std_id
    
    def modify_file(old_file, new_file):
        id_list = []
        with open(old_file, 'r') as old:
            l_old = old.read()
            a = list(map(lambda x: x.capitalize(),l_old.split('\n'))) # 转list并修改首字母为大写
            for i in range(len(a)):
                id_list.append(id_creat())
            d_n = dict.fromkeys(a, )
            for x in d_n:
                d_n[x] = id_creat()
        with open(new_file, 'a') as new:
            for key, val in d_n.items():
                new.write(f'{key}, id={val}\n')
    modify_file('old.txt', 'names.txt')
    

    old.txt

    andrew johnson
    carl brown
    james wellington
    hannah metcalfe
    carl faith dyer
    maria arnold
    colin hudson
    edward martin
    irene sharp
    william ross
    elizabeth miller
    nicola terry
    zoe karen walker
    rachael short
    alison gibson
    

    生成的names.txt

    Andrew johnson, id=660423
    Carl brown, id=736681
    James wellington, id=474974
    Hannah metcalfe, id=077356
    Carl faith dyer, id=874478
    Maria arnold, id=798678
    Colin hudson, id=795771
    Edward martin, id=631234
    Irene sharp, id=308567
    William ross, id=819149
    Elizabeth miller, id=815311
    Nicola terry, id=128637
    Zoe karen walker, id=479428
    Rachael short, id=124619
    Alison gibson, id=650236
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 4月21日
  • 已采纳回答 4月13日
  • 创建了问题 4月10日