Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 09356b3

Browse files
committed
first commit
0 parents commit 09356b3

37 files changed

+1952
-0
lines changed

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.vscode/

‎01-DataSturcture/N_element.py‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
'''
2+
File: N_element.py
3+
Project: DataSturcture
4+
===========
5+
File Created: Monday, 20th July 2020 11:08:11 am
6+
Author: <<LanLing>> (<<lanlingrock@gmail.com>>)
7+
===========
8+
Last Modified: Monday, 20th July 2020 11:44:06 am
9+
Modified By: <<LanLing>> (<<lanlingrock@gmail.com>>>)
10+
===========
11+
Description: 保留序列中 N 个元素的问题
12+
Copyright <<2020>> - 2020 Your Company, <<XDU>>
13+
'''
14+
15+
import heapq
16+
from collections import deque
17+
18+
# 保留最后 N 个元素
19+
# 先入先出
20+
q = deque(maxlen=3)
21+
q.append(1)
22+
q.append(2)
23+
q.append(3)
24+
print(q)
25+
26+
q.append(6)
27+
print(q)
28+
29+
# 队首插入
30+
q.appendleft(4)
31+
print(q)
32+
33+
# 队尾弹出
34+
a = q.pop()
35+
print(a, q)
36+
37+
# 队首弹出
38+
b = q.popleft()
39+
print(b, q)
40+
41+
42+
# 查找最大或最小的 N 个元素
43+
nums = [1, 2, 3, 4, 5, 6, 6, 6, 1, 1]
44+
print(heapq.nlargest(3, nums))
45+
print(heapq.nsmallest(3, nums))
46+
47+
48+
# 更复杂的数据结构
49+
portfolio = [
50+
{'name': 'IBM', 'shares': 100, 'price': 91.1},
51+
{'name': 'AAPL', 'shares': 50, 'price': 543.22},
52+
{'name': 'FB', 'shares': 200, 'price': 21.09},
53+
{'name': 'HPQ', 'shares': 35, 'price': 31.75},
54+
{'name': 'YHOO', 'shares': 45, 'price': 16.35},
55+
{'name': 'ACME', 'shares': 75, 'price': 115.65}
56+
]
57+
cheap = heapq.nsmallest(3, portfolio, key=lambda s: s['price'])
58+
expensive = heapq.nlargest(3, portfolio, key=lambda s: s['price'])
59+
# 会打印全部记录
60+
print(cheap)
61+
print(expensive)
62+
63+
# 当 N 和集合大小差不多时,建议排序后切片

‎01-DataSturcture/anyobj_sort.py‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'''
2+
File: anyobj_sort.py
3+
Project: 01-DataSturcture
4+
===========
5+
File Created: Tuesday, 21st July 2020 3:16:28 pm
6+
Author: <<LanLing>> (<<lanlingrock@gmail.com>>)
7+
===========
8+
Last Modified: Tuesday, 21st July 2020 3:16:32 pm
9+
Modified By: <<LanLing>> (<<lanlingrock@gmail.com>>>)
10+
===========
11+
Description: 使对象支持排序
12+
Copyright <<2020>> - 2020 Your Company, <<XDU>>
13+
'''
14+
from operator import attrgetter
15+
16+
17+
# 要排序的类
18+
class User:
19+
def __init__(self, user_id, name):
20+
self.user_id = user_id
21+
self.name = name
22+
23+
def __repr__(self):
24+
return 'User({})'.format(self.user_id)
25+
26+
users = [User(23, 'aa'), User(3, 'aa'), User(99, 'bb')]
27+
28+
# 单属性的排序
29+
print(sorted(users, key=attrgetter('user_id')))
30+
31+
# 多属性的排序
32+
print(sorted(users, key=attrgetter('name', 'user_id')))
33+
34+
# 最大值
35+
a = min(users, key=attrgetter('user_id'))
36+
print(a.user_id, a.name)
37+
38+
# 最小值
39+
print(max(users, key=attrgetter('user_id')))

‎01-DataSturcture/dict_add_element.py‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
'''
2+
File: dict_add_element.py
3+
Project: DataSturcture
4+
===========
5+
File Created: Monday, 20th July 2020 12:17:31 pm
6+
Author: <<LanLing>> (<<lanlingrock@gmail.com>>)
7+
===========
8+
Last Modified: Monday, 20th July 2020 12:17:37 pm
9+
Modified By: <<LanLing>> (<<lanlingrock@gmail.com>>>)
10+
===========
11+
Description: 字典元素的高级添加
12+
Copyright <<2020>> - 2020 Your Company, <<XDU>>
13+
'''
14+
from collections import defaultdict
15+
16+
17+
# 一个键映射多个值,那么你就需要将这多个值放到另外的容器中
18+
d = {
19+
'a' : [1, 2, 3],
20+
'b' : [4, 5]
21+
}
22+
23+
print(d['a'])
24+
25+
26+
# 保持元素的插入顺序, 使用列表
27+
d = defaultdict(list)
28+
d['a'].append(1)
29+
d['a'].append(1)
30+
d['b'].append(2)
31+
print(d)
32+
33+
# 去掉重复元素, 使用集合, 不关注元素顺序
34+
d = defaultdict(set)
35+
d['a'].add(1)
36+
d['a'].add(1)
37+
d['b'].add(4)
38+
print(d)
39+
40+
41+
# 将数据添加到多值映射的字典
42+
pairs = {
43+
('a', 12),
44+
('a', 13),
45+
('b', 14)
46+
}
47+
# 不用先创建 d['a'] = []
48+
d = defaultdict(list)
49+
for key, value in pairs:
50+
d[key].append(value)
51+
print(d)

‎01-DataSturcture/dict_calc.py‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
'''
2+
File: dict_calc.py
3+
Project: 01-DataSturcture
4+
===========
5+
File Created: Monday, 20th July 2020 5:32:11 pm
6+
Author: <<LanLing>> (<<lanlingrock@gmail.com>>)
7+
===========
8+
Last Modified: Monday, 20th July 2020 5:32:17 pm
9+
Modified By: <<LanLing>> (<<lanlingrock@gmail.com>>>)
10+
===========
11+
Description: 字典计算
12+
Copyright <<2020>> - 2020 Your Company, <<XDU>>
13+
'''
14+
15+
# 字典计算,找最小值等
16+
prices = {
17+
'ACME': 45.23,
18+
'AAPL': 612.78,
19+
'ABM': 205.55,
20+
'HPQ': 37.20,
21+
'FB': 10.75
22+
}
23+
24+
# 只返回键
25+
print(min(prices))
26+
print(max(prices))
27+
28+
# 返回值和键
29+
# 按照值排序
30+
print(min(zip(prices.values(), prices.keys())))
31+
# 按照键排序
32+
print(max(zip(prices.keys(), prices.values())))
33+
34+
35+
# 字典比较,交、并、补等
36+
a = {
37+
'x' : 1,
38+
'y' : 2,
39+
'z' : 3
40+
}
41+
42+
b = {
43+
'w' : 10,
44+
'x' : 11,
45+
'y' : 2
46+
}
47+
48+
# 查找共同的键
49+
print(a.keys() & b.keys())
50+
# 键在 a,但不在 b
51+
print(a.keys() - b.keys())
52+
# 键值都相同
53+
print(a.items() & b.items())
54+
55+
56+
# 创建一个新字典
57+
c = {key:a[key] for key in (a.keys() & b.keys()) - {'z', 'w'}}
58+
print(c)
59+
60+
61+
# 取字典元素的最小值
62+
portfolio = [
63+
{'name':'GOOG', 'shares': 50},
64+
{'name':'YHOO', 'shares': 75},
65+
{'name':'AOL', 'shares': 20},
66+
{'name':'SCOX', 'shares': 65}
67+
]
68+
69+
min_shares = min(s['shares'] for s in portfolio)
70+
print(min_shares)

‎01-DataSturcture/dict_merge.py‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'''
2+
File: dict_merge.py
3+
Project: 01-DataSturcture
4+
===========
5+
File Created: Tuesday, 21st July 2020 4:49:05 pm
6+
Author: <<LanLing>> (<<lanlingrock@gmail.com>>)
7+
===========
8+
Last Modified: Tuesday, 21st July 2020 4:49:09 pm
9+
Modified By: <<LanLing>> (<<lanlingrock@gmail.com>>>)
10+
===========
11+
Description: 字典合并
12+
Copyright <<2020>> - 2020 Your Company, <<XDU>>
13+
'''
14+
from collections import ChainMap
15+
16+
17+
a = {'x': 1, 'z': 3 }
18+
b = {'y': 2, 'z': 4 }
19+
20+
# 合并字典
21+
# 在内部创建了一个容纳这些字典的列表
22+
# 相同的键,只保留在第一个字典的
23+
c = ChainMap(a, b)
24+
print(c['x'])
25+
print(c['y'])
26+
print(c['z'])
27+
print(list(c.keys()))
28+
print(list(c.values()))
29+
30+
# 所有的操作都只会影响第一个字典
31+
c['z'] = 10
32+
c['w'] = 20
33+
print(a, b)
34+
35+
# 个人感觉 defaultdict 会好一些

‎01-DataSturcture/dict_sort.py‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'''
2+
File: dict_sort.py
3+
Project: DataSturcture
4+
===========
5+
File Created: Monday, 20th July 2020 5:24:37 pm
6+
Author: <<LanLing>> (<<lanlingrock@gmail.com>>)
7+
===========
8+
Last Modified: Monday, 20th July 2020 5:24:58 pm
9+
Modified By: <<LanLing>> (<<lanlingrock@gmail.com>>>)
10+
===========
11+
Description: 字典排序
12+
Copyright <<2020>> - 2020 Your Company, <<XDU>>
13+
'''
14+
from collections import OrderedDict
15+
from operator import itemgetter
16+
17+
18+
# 保留元素插入时的顺序
19+
# 大小是一个普通字典的两倍,因为它内部维护着另外一个插入顺序排序的链表。
20+
d = OrderedDict()
21+
d['foo'] = 1
22+
d['spam'] = 3
23+
d['bar'] = 2
24+
d['grok'] = 4
25+
# Outputs "foo 1", "bar 2", "spam 3", "grok 4"
26+
for key in d:
27+
print(key, d[key])
28+
29+
30+
# 按照某一规定排序
31+
data = [
32+
{'fname': 'Brian', 'lname': 'Jones', 'uid': 1003},
33+
{'fname': 'David', 'lname': 'Beazley', 'uid': 1002},
34+
{'fname': 'John', 'lname': 'Cleese', 'uid': 1001},
35+
{'fname': 'Big', 'lname': 'Jones', 'uid': 1004}
36+
]
37+
38+
# 传入排序的参数
39+
rows_by_fname = sorted(data, key=itemgetter('fname'))
40+
rows_by_uid = sorted(data, key=itemgetter('uid'))
41+
42+
print(rows_by_fname)
43+
print(rows_by_uid)
44+
45+
# 传入多个参数,先比较第一个,相同则比较第二个
46+
rows_by_lfname = sorted(data, key=itemgetter('lname','fname'))
47+
print(rows_by_lfname)
48+
49+
# 输出最大值和最小值
50+
b = min(data, key=itemgetter('uid'))
51+
a = max(data, key=itemgetter('uid'))
52+
53+
print(a, '\n', b)

‎01-DataSturcture/element_count.py‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
'''
2+
File: element_count.py
3+
Project: 01-DataSturcture
4+
===========
5+
File Created: Monday, 20th July 2020 10:41:10 pm
6+
Author: <<LanLing>> (<<lanlingrock@gmail.com>>)
7+
===========
8+
Last Modified: Monday, 20th July 2020 10:41:15 pm
9+
Modified By: <<LanLing>> (<<lanlingrock@gmail.com>>>)
10+
===========
11+
Description: 序列中出现次数最多的元素
12+
Copyright <<2020>> - 2020 Your Company, <<XDU>>
13+
'''
14+
from collections import Counter
15+
16+
17+
# 出现频率最高的3个单词
18+
words = [
19+
'look', 'into', 'my', 'eyes', 'look', 'into', 'my', 'eyes',
20+
'the', 'eyes', 'the', 'eyes', 'the', 'eyes', 'not', 'around', 'the',
21+
'eyes', "don't", 'look', 'around', 'the', 'eyes', 'look', 'into',
22+
'my', 'eyes', "you're", 'under'
23+
]
24+
word_counts = Counter(words)
25+
# 出现频率最高的3个单词
26+
top_three = word_counts.most_common(3)
27+
print(top_three)
28+
# 字典,将元素映射到出现次数中
29+
print(word_counts['look'])
30+
31+
32+
# 更新词汇表
33+
morewords = ['why', 'are', 'you', 'not', 'looking', 'in', 'my', 'eyes']
34+
word_counts.update(morewords)
35+
print(word_counts['eyes'])
36+
37+
38+
# Counter 对象在几乎所有需要制表或者计数数据的场合是非常有用的工具。
39+
# 在解决这类问题的时候你应该优先选择它,而不是手动的利用字典去实现。
40+
a = Counter(words)
41+
b = Counter(morewords)
42+
43+
# 直接操作
44+
c = a + b
45+
d = a - b
46+
print(c, '\n', d)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /