实验四

with open('data6_1.txt','r',encoding='utf-8') as f:
    t=[]
    for line in f.readlines():
        m=line.strip().split()
        t.append(m)
t.sort(key=lambda l:l[2],reverse=True)

with open('data6_2.txt','w+',encoding='utf-8') as n:
        for i in t:
            n.write('  '.join(i)+'\n')

实验四

 

 

import random

def get_lucky_boy(lis:list,trys:int):
    result=[]
    for i in range(trys):
        boy=random.choice(lis)
        lis.remove(boy)
        result.append(boy)
    t=[]
    for i in result:
        t.append(' '.join(i)+'\n')
    return ''.join(t)
ls=[]
with open('data7.txt','r',encoding='utf-8') as f:
   for line in f.readlines():
       m=line.strip().split()
       ls.append(m)
n=int(input('yikutu desuka?'))
boys=get_lucky_boy(lis=ls,trys=n)
print(boys)
with open('lucky.txt','w',encoding='utf-8') as nf:
        nf.write(boys)

实验四

 

 实验四

 

 

import datetime
import random

def get_lucky_boy(lis:list,trys:int):
    result=[]
    for i in range(trys):
        boy=random.choice(lis)
        lis.remove(boy)
        result.append(boy)
    return result

def ls2str(ls:list):
    t=[]
    for i in ls: 
        t.append(' '.join(i)+'\n')
    return ''.join(t)

ls=[]
with open('data7.txt','r',encoding='utf-8') as f:
    ls = []
    with open('data7.txt', 'r', encoding='utf-8') as f:
        for line in f.readlines():
            m = line.strip().split()
            ls.append(m)
    n=int(input('yikutudesuka?'))
    boys=get_lucky_boy(lis=ls,trys=n)
    result=ls2str(boys)
    print(result)
    filename=datetime.datetime.now().strftime('%Y%m%d')
    with open(filename,'w',encoding='utf-8') as nf:
        nf.write(result)

实验四

 

 实验四

 

上一篇:关于给每一个元素绑定事件获取索引值的一个点(超详细讲解)


下一篇:美团笔试题——蛋糕奶茶(python)