import random
lst =["数学", "语文", "英语", "物理", "化学", "政治", "生物", "地理", "历史"]
# 随机选择一个
res1 = random.choice(lst)
print(res1)
# 随机选择多个
res2 = random.sample(lst, 3)
print(res2)
# 判断多个词是否在某个字符串中
words = ['a', 'b', 'c', 'd']
s = "apple"
if any([w in s and w for w in words]):
print("yes")
else:
print("false")
# 取出路径下的所有文件夹名称
import os
src = "./"
filenames = [name for name in os.listdir(src) if os.path.isdir(name)]
print(filenames)
# 打乱一个排好序的list对象alist
import random
random.shuffle(alist)
list列表推导式
# 循环平方
squares = [x**2 for x in range(1,10)]
# 循环去除两边空格
mybag = [' glass',' apple','green leaf '] #有的前面有空格,有的后面有空格
[one.strip() for one in mybag]
# 取出在列表lst1但不在lst2的元素
lst1 = ['a', 'b', 'c', 'd']
lst2 = ['a', 'e', 'f']
res = [i for i in lst1 if i not in lst2]
print(res)
# 取出以lst2内元素结尾的lst1中的元素
lst1 = ['apple', 'egg', 'python', 'list', 'dict']
lst2 = ['e', 't']
res = []
for word in lst1:
if any([word.endswith(i) for i in lst2]):
res.append(word)
print(res)
# 危险词去除
riskword = ["a", "b", "c"]
words = ["apple", "egg", "python"]
res = []
for word in words:
if all([rw not in word for rw in riskword]):
res.append(word)
print(res)
将列表中的第一个移到最后一位。
lst = [0,1,2,3,4,5]
last = lst.pop(0)
lst.append(last)
# 目标: [1, 1, 1, 2, 2, 2, 3, 3, 4, 4, 5] --> [[1, 1, 1], [2, 2, 2], [3, 3], [4, 4], [5]]
from collections import Counter
box = [1, 1, 1, 2, 2, 2, 3, 3, 4, 4, 5]
# 方法一
last = box[0]
temp = [box[0]]
cutbox = [temp]
for item in box:
if item == last:
temp.append(item)
else:
last = item
temp = [item]
cutbox.append(temp)
# 方法二
cou = Counter(box)
cutbox = []
for key, value in dict(cou).items():
cutbox.append([key]*value)
print(cutbox)
# 方法三
cutbox = []
for i in set(box):
cutbox.append([i]*box.count(i))
print(cutbox)
将列表数据分为每三个一组
lists= [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
n = 3 # 表示多少个一组
list = [lists[i:i+n] for i in range(0,len(lists),n)]
print(list) # [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11], [12]]
两个列表的全组合
# 方式一:
import itertools
list1 = [1, 2, 3, 4]
list2 = [5, 6, 7, 8,9]
print(list(itertools.product(list1, list2)))
# 方式二:
list3 = [[x,y] for x in list1 for y in list2]
print(list3)
# 方式三:
from itertools import permutations
a = [1, 2, 3]
b = [4, 5, 6]
for p in permutations(b):
print(b)
lst = [list(zip(a, p)) for p in permutations(b)]
print(lst)
统计列表中出现的元素个数
# 统计字符串中分词后各个词出现的次数
import jieba
from collections import Counter
text = "1经常保持微笑智慧泡2常运动智慧泡3保持充足的睡眠智慧泡4乐于助人智慧泡5学会和各种人愉快的相处智慧泡6保持高度的自信心智慧泡7持有童真智慧泡8充满好奇智慧泡9具有幽默感智慧泡10装酷也可以智慧泡11服装要潮流智慧泡12做到风度翩智慧泡13有自己的兴趣爱好智慧泡14勇于突破危险智慧泡15有接受意外的心态智慧泡16有创意智慧泡17知道怎样利用能源比如拖地智慧泡18一定要有追求目标智慧泡19对未来有展望智慧泡20保护弱小智慧泡21与兄弟姐妹相亲相爱智慧泡22要学会合作,创造奇迹智慧泡23再怎么艰难,都不要放弃所爱的人智慧泡24重要的东西还是不要轻易失去例如初吻智慧泡25不要总是面带色相智慧泡26千万不要得罪女朋友智慧泡27不要为失恋而茶不思,饭不想智慧泡28不要欺压别人智慧泡29不要随便惊吓朋友智慧泡30有空来家坐智慧泡31看到好贴一定要回复智慧泡32不要恶意灌水智慧泡"
text = jieba.cut_for_search(text)
res = list(text)
print(res)
print(len(res))
# 统计列表中各元素出现次数,most_conmmon获取最多的前N个,数据格式[(item, num)]
most = Counter(res).most_common(1)
most_num = most[0][1]
对两个不同长度的列表进行迭代操作
a = [1,2,3,4,5]
b = ["python","www.itdiffer.com","qiwsir"]
(1)
d = []
for x, y in zip(a, b)
d.append(x+y)
(2)
length = len(a) if len(a)<len(b) else len(b)
for i in range(length):
c.append(str(a[i]) + ":" + b[i])