python实用技巧1

1、列表中的操作

#在字典,list,元组中挑选数据
from random import randint
l = [randint(-10, 10) for _ in range(10)]
print(l) #[-2, -8, 3, -3, -3, -9, 0, 3, -3, -8]
#挑选出大于等于0的元素

#法1
l_one = [x for x in l if x >= 0]
print(l_one) #3 0 3
#法2 filter

l_two = filter(lambda x: x >= 0, l)
print(list(l_two)) #3 0 3

2、字典中的操作

#创建学生字典,并且挑出成绩大于等于90的学生

student = {
    'student_%d' % i: randint(50, 100) for i in range(1, 11)
}
print(student) #{'student_1': 60, 'student_2': 58, 'student_3': 70, 'student_4': 72, 'student_5': 86, 'student_6': 72, 'student_7': 85, 'student_8': 65, 'student_9': 75, 'student_10': 84}


#法1
student_one = {k: v for k, v in student.items() if v >= 90}
print(student_one) #自行进行运行啦

#法2
student_two = filter(lambda x: x[1] >= 90, student.items())
print(dict(student_two)) #自行进行运行啦

3、元组中的操作

#元组的命名操作提高可读性
from collections import namedtuple

student_information = namedtuple("student_1", ['name', 'age', 'sex'])
s1 = student_information("weierLin", 23, "male")
print(s1[0]) #weierLin
print(s1.name) #weierLin

#对dict进行value进行排序

student_sort = {k: randint(50, 100) for k in ('abcdef')}
print(student_sort) #{'a': 96, 'b': 100, 'c': 95, 'd': 50, 'e': 66, 'f': 76}
#法1 用元组或者zip进行操作再进行排序
student_sort_zip = zip(student_sort.values(), student_sort.keys())
print(list(student_sort_zip))

print(sorted((student_sort_zip))) #[(100,'b'), (96, 'a'), (95, 'c'), (76, 'f'), (66, 'e'), (50, 'd')]

#法2 用sorted里面的key进行操作
p = sorted(student_sort.items(), key = lambda item: item[1], reverse=True)
print(p) #[('b', 100), ('a', 96), ('c', 95), ('f', 76), ('e', 66), ('d', 50)]

#用enumerate来增加一个标号
q = enumerate(p, 1)
print(list(q))

q_1 = {i: (k, v) for i, (k, v) in enumerate(p, q)}
print(q_1) #[(1, ('b', 100)), (2, ('a', 96)), (3, ('c', 95)), (4, ('f', 76)), (5, ('e', 66)), (6, ('d', 50))]

4、统计字典中频率前k的元素出现的频率

#统计元素出现的频率最大的前k个
from random import randint
from collections import Counter
fre_alp = [randint(1, 10) for _ in range(20)]
print(fre_alp) #[6, 1, 8, 10, 8, 4, 7, 3, 3, 1, 7, 2, 9, 7, 5, 1, 5, 2, 5, 4]

fre_dict = Counter(fre_alp)
print(fre_dict) #Counter({1: 3, 7: 3, 5: 3, 8: 2, 4: 2, 3: 2, 2: 2, 6: 1, 10: 1, 9: 1})
print(fre_dict.most_common(3)) #这样就找出了前三个最大频率的元素了呀 [(1, 3), (7, 3), (5, 3)]

#拿来文本来进行玩耍
import re
from collections import Counter
txt = open('text.txt').read()
print(txt)
txt_split = re.split('\W+', txt) #以非字母的进行切分,具体自己看正则表达式
print(txt_split) #['here', 'are', 'moments', 'in', 'life', 'when', 'you', 'miss', 'someone', 'so', 'much', 'that', 'you', 'just', 'want', 'to', 'pick', 'them', 'from', 'your', 'dreams', 'and', 'hug', 'them', 'for', 'real', 'Dream', 'what', 'you', 'want', 'to', 'dream', 'go', 'where', 'you', 'want', 'to', 'go', 'be', 'what', 'you', 'want', 'to', 'be', 'because', 'you', 'have', 'only', 'one', 'life', 'and', 'one', 'chance', 'to', 'do', 'all', 'the', 'things', 'you', 'want', 'to', 'do', 'May', 'you', 'have', 'enough', 'happiness', 'to', 'make', 'you', 'sweet', 'enough', 'trials', 'to', 'make', 'you', 'strong', 'enough', 'sorrow', 'to', 'keep', 'you', 'human', 'enough', 'hope', 'to', 'make', 'you', 'happy', 'Always', 'put', 'yourself', 'in', 'others鈥檚hoes', 'If', 'you', 'feel', 'that', 'it', 'hurts', 'you', 'it', 'probably', 'hurts', 'the', 'other', 'person', 'too', '']
txt_dict = Counter(txt_split)
print(txt_dict) #Counter({'you': 14, 'to': 10, 'want': 5, 'enough': 4, 'make': 3, 'in': 2, 'life': 2, 'that': 2, 'them': 2, 'and': 2, 'what': 2, 'go': 2, 'be': 2, 'have': 2, 'one': 2, 'do': 2, 'the': 2, 'it': 2, 'hurts': 2, 'here': 1, 'are': 1, 'moments': 1, 'when': 1, 'miss': 1, 'someone': 1, 'so': 1, 'much': 1, 'just': 1, 'pick': 1, 'from': 1, 'your': 1, 'dreams': 1, 'hug': 1, 'for': 1, 'real': 1, 'Dream': 1, 'dream': 1, 'where': 1, 'because': 1, 'only': 1, 'chance': 1, 'all': 1, 'things': 1, 'May': 1, 'happiness': 1, 'sweet': 1, 'trials': 1, 'strong': 1, 'sorrow': 1, 'keep': 1, 'human': 1, 'hope': 1, 'happy': 1, 'Always': 1, 'put': 1, 'yourself': 1, 'others鈥檚hoes': 1, 'If': 1, 'feel': 1, 'probably': 1, 'other': 1, 'person': 1, 'too': 1, '': 1})

print(txt_dict.most_common(3)) #[('you', 14), ('to', 10), ('want', 5)]

猜你喜欢

转载自blog.csdn.net/qq_37982109/article/details/88659763