Python刚刚接触,请教一个简单的问题。谢谢!

2025-06-27 07:52:47
推荐回答(3个)
回答1:

al = open("a.txt").read().split(' ')
bl = open("b.txt").read().split(' ')
cl = open("c.txt").read().split(' ')

print len(set(a)&set(c))*1.0/len(a)
print len(set(b)&set(c))*1.0/len(b)

回答2:

def file2set(fname, spliter=' '):
''' Read from file and generate a set by splited '''
return set(open(fname).read().split(spliter))

def percent(aset, base):
''' return a string of count in base vs all count '''
return '%d / %d' % (len(aset & base) , len(aset))

base = file2set(r'c.txt')
print('\n'.join(map(
lambda fname: percent(file2set(fname), base),
('a.txt','b.txt')
)))

回答3:

al = open("a.txt").read().split()
bl = open("b.txt").read().split()
cl = open("c.txt").read().split()
len(set(al) & set(cl)) #2
len(set(bl) & set(cl)) #2