更多连载请查看【python】
最近一次修订时间为 2020-11-04
Don’t say so much!
文章目录
1 一些例子
列表中出现次数最多的元素
用 max 和 count 结合
a = [1,2,3,4,5,7,1,2,3,4,6,1,2,3,4,6,1,4,5,6,8,9,1,1,1,2,4,5]
# 统计下2出现的次数
print(a.count(2))
# 与max一起使用,找出频数最高的元素
print(max(set(a),key = a.count))
output
4
1
另外一个中方法
a = [1,2,3,4,5,7,1,2,3,4,6,1,2,3,4,6,1,4,5,6,8,9,1,1,1,2,4,5]
from collections import Counter
cnt = Counter(a)
# 输出频数最高的前1项及其频数
print(cnt.most_common(1))
# 输出频数最高的前3项及其频数
print(cnt.most_common(3))
# 输出结果
print(cnt.most_common(1)[0][0])
output
[(1, 7)]
[(1, 7), (4, 5), (2, 4)]
1
列表中最长的字符串
s = ["Hello", "my", "favorite", "baseketball", "star", "is", "Kobe", "Bryant", "!"]
print(max(s,key=len))
output
baseketball
list 去重
from random import randrange
from collections import OrderedDict
import numpy as np
import time
list1 = [randrange(100) for _ in range(1000000)]
# 方法一:新建一个列表,遍历原列表,如果值不在新列表里,便加入到新列表中
def duplicates1(list1):
unique = []
for item in list1:
if item not in unique:
unique.append(item)
return unique
# 方法二:新建一个集合,遍历原列表,如果值不在集合里,便加入到新列表中
def duplicates2(list1):
unique = set()
for item in list1:
if item not in unique:
unique.add(item)
return unique
# 方法三:把列表转化为集合,然后再转化回来
def duplicates3(list1):
return list(set(list1))
# 方法四:使用 dict.fromkeys() 函数,可以保留原有数组的顺序并去重——Python3.6及以上才支持
def duplicates4(list1):
return list(dict.fromkeys((list1)))
# 方法五:在Python3.6 以下,其实也存在 fromkeys 函数,只不过它由 collections 库提供
def duplicates5(list1):
return list(OrderedDict.fromkeys(list1))
time1 = 0
time2 = 0
time3 = 0
time4 = 0
time5 = 0
for _ in range(10):
start = time.time()
duplicates1(list1)
end1 = time.time()
time1 += (end1-start)
duplicates2(list1)
end2 = time.time()
time2 += (end2-end1)
duplicates3(list1)
end3 = time.time()
time3 += (end3-end2)
duplicates4(list1)
end4 = time.time()
time4 += (end4-end3)
duplicates5(list1)
end5 = time.time()
time5 += (end5-end4)
print(np.array([time1, time2, time3, time4, time5])*1000/10)
output
[932.25343227 41.24481678 10.72115898 32.05580711 43.39983463]
五种方式,跑 10 次取的平均值,结果单位是 ms
可以看到,借助 set 速度有明显提升,如果不考虑顺序的话,先转化为 set 再还原为 list 速度最快!
去重后要保持原列表的速度话,需要借助 fromkeys
创建字典,因为字典的 key 是 Hash 的,这点本质上和 set 一样
补充
来自 Python 字典(Dictionary) fromkeys()方法
倒置列表
法一:用python的 reversed
list1 = [1,2,3,4,5]
for i in reversed(list1):
print(i,end='')
output 54321
法二:用列表的 reverse
list1 = [1,2,3,4,5]
list1.reverse()
print(list1)
output [5, 4, 3, 2, 1]
法三:用 python的 sorted
print(list1)
list2 = sorted(list1,reverse=True)
print(list2)
output
[1, 2, 3, 4, 5] [5, 4, 3, 2, 1]
法四:用slice的方法
list1 = [1,2,3,4,5]
list1[::-1]
output
[5, 4, 3, 2, 1]
list[:]
list[:]
深层复制
list1 = [1,2,3,4,5]
list2 = list1
list1[0]=0
list2
output
[0, 2, 3, 4, 5]
list1 = [1,2,3,4,5]
list2 = list1[:]
list1[0]=0
list2
output
[1, 2, 3, 4, 5]
string to list
1)一个单词转化为字母列表
法一:
s = 'bryant'
list1 = [i for i in s]
print(type(list1))
print(list1)
output
<class 'list'>
['b', 'r', 'y', 'a', 'n', 't']
法二:
s = 'bryant'
list1 = list(s)
print(type(list1))
print(list1)
output
<class 'list'>
['b', 'r', 'y', 'a', 'n', 't']
2)一句话转化为单词列表
用 split 方法
s = 'bryant is my brother'
list1 = s.split(' ')
print(type(list1))
print(list1)
output
<class 'list'>
['bryant', 'is', 'my', 'brother']
再来个例子
s = '2019-5-16'
list1 = s.split('-')
print(type(list1))
print(list1)
output
<class 'list'>
['2019', '5', '16']
把 input()
输入映射为整型并转化为列表(刷牛客网上的算法题时比较常用)
nums = list(map(int,input().split(" ")))
list to string
由字符构成的 list 转成 string
list1 = list("KobeBryant")
print(list1)
# 法一
s1 = ""
for i in list1:
s1 += i
# 法二
s2 = "".join(list1)
print(type(s1),s1)
print(type(s2),s2)
output
['K', 'o', 'b', 'e', 'B', 'r', 'y', 'a', 'n', 't']
<class 'str'> KobeBryant
<class 'str'> KobeBryant
two list to dict
list1 = [1,2,3,4,5]
list2 = [6,7,8,9,10]
print(dict(zip(list1,list2)))
output
{
1: 6, 2: 7, 3: 8, 4: 9, 5: 10}
再看一个例子
list1 = [1,1.0,3,4,5]
list2 = [6,7,8,9,10]
print(dict(zip(list1,list2)))
output
{
1: 7, 3: 8, 4: 9, 5: 10}
哈哈, 1 和 1.0 一样,这和字典 key 的 hash 映射的模式有关
将嵌套列表合并为一个列表
list1 = [[1,2,3],[4,5,6],[7,8,9]]
print([num for row in list1 for num in row])
output
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Note:
- 更多连载请查看【python】