1 Star 0 Fork 8

chenp / blog

forked from 1264644959 / blog 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
python1000000个数,怎么找出重复的数字.md 1.88 KB
一键复制 编辑 原始数据 按行查看 历史
1264644959 提交于 2020-09-07 19:18 . commit

set + list

import random
import time

list_test = []
for i in range(100000):
    list_test.append(random.randint(0,1000000))
myset = set(list_test)
k = 0
start_time = time.time()
for i in myset:
    if list_test.count(i) >= 1:
        k += 1
print("重复数字有%d个" % k)
print("共花费%s" % str(time.time() - start_time))

image-20200907184143934

Counter

counter用法https://www.cnblogs.com/Eva-J/articles/7291842.html

from collections import Counter
import random
import time

list_test = []
for i in range(100000):
    list_test.append(random.randint(0,1000000))
start_time = time.time()
a = Counter(list_test)
k = 0
for key, val in a.items():
    if val >= 1:
        k += 1

print('重复数字有%d个' % k)
print("共花费%s" % str(time.time() - start_time))

image-20200907185440276

defaultdict

from collections import Counter, defaultdict
import random
import time

list_test = []
for i in range(100000):
    list_test.append(random.randint(0,1000000))
start_time = time.time()
count_dict = defaultdict(int)
for item in list_test:
    count_dict[item] += 1
k = 0
for key, val in count_dict.items():
    if val >= 1:
        k += 1
print('重复数字有%d个' % k)
print("共花费%s" % str(time.time() - start_time))

image-20200907190134840

list + dict

from collections import Counter, defaultdict
import random
import time

list_test = []
for i in range(100000):
    list_test.append(random.randint(0,1000000))
start_time = time.time()
a = {}
for i in list_test:
    if list_test.count(i) >= 1:
        a[i] = list_test.count(i)
print(len(a))
print("共花费%s" % str(time.time() - start_time))

速度很慢

总结

collections模块的两个库速度比较快

马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/nchenp/blog.git
git@gitee.com:nchenp/blog.git
nchenp
blog
blog
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891