1、给定一个list,元素都是字符串,查找出现次数最多的前 k 位字符串
from collections import Counter
def getStrs(lists,k):
if len(lists)<k:
return 0
lists1=Counter(lists).most_common(k)
lists2=[]
for l in range(k):
lists2.append(l[0])
return lists2
2、给定一个 list,查找出无重复的元素 并返回
from collections import Counter
def newlists(l):
if not 1:
return None
l1=dict(Counter(l))
l2=[]
for ch,i in l1.items():
if i==1:
l2.append(ch)
return l2
3、python 读取大文件,查找字符串出现次数
import os
def getStrNum(file_path,strs,size):
if not os.path.exists(file_path):
return 0
with open(file_path,'r',encoding='utf-8') as f:
counts=0
while True:
line=f.read(size)
if line:
yeild line