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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
| class BaseTopic:
@staticmethod
def day_of_year(year: int, month: int, day: int):
"""给定一个日期,计算这是这一年中的第几天"""
import datetime
date1 = datetime.date(year=year, month=month, day=day)
date2 = datetime.date(year=year, month=1, day=1)
return (date1 - date2).days + 1
@staticmethod
def upset_list(alist: list):
"""打乱列表"""
import random
# 改变原列表, 不返回值
random.shuffle(alist)
return alist
@staticmethod
def sort_by_value_in_dict(adict: dict):
"""根据字典值进行排序"""
# x[0]代表用key进行排序, x[1]代表用value进行排序
new_dict = sorted(adict.items(), key=lambda x: x[1])
return new_dict
@staticmethod
def reverse_str(astr: str):
"""反转字符串"""
return astr[::-1]
@staticmethod
def str_to_dict():
"""将字符串 "k:1 |k1:2|k2:3|k3:4",处理成字典 {k:1,k1:2,...}"""
str1 = "k:1 |k1:2|k2:3|k3:4"
dict1 = dict()
for items in str1.split('|'):
key, value = items.split(':')
dict1[key] = value
return dict1
@staticmethod
def sort_by_dict_in_list():
"""按alist中元素的age由大到小排序"""
alist = [
{'name': 'a', 'age': 20},
{'name': 'b', 'age': 30},
{'name': 'c', 'age': 25}
]
new_alist = sorted(alist, key=lambda x: x['age'], reverse=False)
return new_alist
@staticmethod
def search_for_two_list():
"""找出两个列表的相同元素和不同元素"""
list1 = [1, 2, 3, 4, 5]
list2 = [3, 4, 5, 6, 7]
set1 = set(list1)
set2 = set(list2)
same = set1 & set2
diff = set1 ^ set2
return same, diff
@staticmethod
def del_sample_element_in_list():
"""删除列表中的相同元素"""
list1 = [1, 1, 2, 1, 3, 4]
list2 = set(list1)
return list2
if __name__ == '__main__':
# print(BaseTopic.day_of_year(2022, 5, 24))
# print(BaseTopic.upset_list([1, 2, 3, 4, 5]))
# print(BaseTopic.sort_by_value_in_dict({'a': 13, 'y': 0, 'x': 9, 's': 2}))
# print(BaseTopic.reverse_str("123456789"))
# print(BaseTopic.str_to_dict())
# print(BaseTopic.sort_by_dict_in_list())
# print(BaseTopic.search_for_two_list())
print(BaseTopic.del_sample_element_in_list())
|