foreword
previous articles
[Python Learning] Lists and Tuples
dictionaries and collections
A dictionary is a combination of a series of unordered elements, its length and size are variable, and elements can be deleted and changed arbitrarily. But note that the elements here are a pair of key (key) and value (value)
Compared with lists and tuples, dictionaries have better performance, especially for lookup, addition and deletion, dictionaries can be completed in constant time complexity
The collection and the dictionary are basically the same, the only difference is that the pairing of the collection without key and value is a series of unordered and unique combination of elements.
d1 = {
'name': 'jason', 'age': 20, 'gender': 'male'}
d2 = dict({
'name': 'jason', 'age': 20, 'gender': 'male'})
d3 = dict([('name', 'jason'), ('age', 20), ('gender', 'male')])
d4 = dict(name='jason', age=20, gender='male')
d1 == d2 == d3 ==d4
True
s1 = {
1, 2, 3}
s2 = Set([1, 2, 3])
s1 == s2
True
Collections do not support index operations, because collections are essentially a hash table, unlike lists
s = {
1, 2, 3}
s[0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'set' object does not support indexing
To judge whether an element is in a dictionary or a collection, we can use value in dict/set
s = {
1, 2, 3}
1 in s
True
10 in s
False
d = {
'name': 'Runsen', 'age': 20}
'name' in d
True
'location' in d
False
Dictionary additions, deletions and modifications
In [1]: d = {
'name': 'Runsen', 'age': 20}^M
...:
In [2]: d['gender'] = 'male'
In [3]: d['birthday'] = '1999-10-01'
In [4]: d
Out[4]: {
'name': 'Runsen', 'age': 20, 'gender': 'male', 'birthday': '1999-10-01'}
In [5]: d['birthday'] = '1999/10/01'
In [6]: d.pop('birthday')
Out[6]: '1999/10/01'
In [8]: d
Out[8]: {
'name': 'Runsen', 'age': 20, 'gender': 'male'}
In [9]: s = {
1, 2, 3}^M
...:
In [10]: s.add(4)
In [11]: s
Out[11]: {
1, 2, 3, 4}
In [12]: s.remove(4)
In [13]: s
Out[13]: {
1, 2, 3}****
Ascending and descending sorting of dictionaries
d = {
'b': 1, 'a': 2, 'c': 10}
d_sorted_by_key = sorted(d.items(), key=lambda x: x[0]) # 根据字典键的升序排序
d_sorted_by_value = sorted(d.items(), key=lambda x: x[1]) # 根据字典值的升序排序
d_sorted_by_key
[('a', 2), ('b', 1), ('c', 10)]
d_sorted_by_value
[('b', 1), ('a', 2), ('c', 10)]
add, delete, search
Dictionaries and sets are highly performance-optimized data structures, especially for lookup, add, and delete operations
list of practices
# list version
def find_unique_price_using_list(products):
unique_price_list = []
for _, price in products: # A
if price not in unique_price_list: #B
unique_price_list.append(price)
return len(unique_price_list)
# products id 和 price
products = [
(143121312, 100),
(432314553, 30),
(32421912367, 150),
(937153201, 30)
]
print('number of unique price is: {}'.format(find_unique_price_using_list(products)))
# 输出
number of unique price is: 3
collection of practices
# set version
def find_unique_price_using_set(products):
unique_price_set = set()
for _, price in products:
unique_price_set.add(price)
return len(unique_price_set)
products = [
(143121312, 100),
(432314553, 30),
(32421912367, 150),
(937153201, 30)
]
print('number of unique price is: {}'.format(find_unique_price_using_set(products)))
# 输出
number of unique price is: 3
Comparing run times, aka performance
import time
id = [x for x in range(0, 100000)]
price = [x for x in range(200000, 300000)]
products = list(zip(id, price))
# 计算列表版本的时间
start_using_list = time.perf_counter()
find_unique_price_using_list(products)
end_using_list = time.perf_counter()
print("time elapse using list: {}".format(end_using_list - start_using_list))
## 输出
time elapse using list: 41.61519479751587
# 计算集合版本的时间
start_using_set = time.perf_counter()
find_unique_price_using_set(products)
end_using_set = time.perf_counter()
print("time elapse using set: {}".format(end_using_set - start_using_set))
# 输出
time elapse using set: 0.008238077163696289
Ensemble exhaustive lists in terms of performance
For dictionaries, the hash table stores the hash value, the key and the value, which are three elements
Both dictionaries and collections are unordered data structures, and their internal hash table storage structure ensures the efficiency of search, insertion, and deletion operations. Therefore, dictionaries and collections are usually used to look up elements and remove duplicates
There are two ways to initialize the dictionary, which one is more efficient,
In [20]: timeit a ={
'name':"runsen",'age':20}
127 ns ± 0.8 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
In [21]: timeit b =dict({
'name':"runsen",'age':20})
438 ns ± 3.41 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
The first one, because there is no need to call related functions
Can the keys of a dictionary be a list? In the following code, is the initialization of the dictionary correct?
In [22]: d = {
'name': 'Runsen', ['education']: [' primary school', 'junior middle school']}^M
...:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-22-13cd196aef11> in <module>
----> 1 d = {
'name': 'Runsen', ['education']: [' primary school', 'junior middle school']}
TypeError: unhashable type: 'list'
In [23]: d = {
'name': 'Runsen', ('education'): [' primary school', 'junior middle school']}^M
...:
...:
In [24]: d
Out[24]: {
'name': 'Runsen', 'education': [' primary school', 'junior middle school']}
Using a list as a key is not allowed here, because the list is a dynamically changing data structure, and the key in the dictionary is required to be immutable, and the reason is well understood.
The key is not repeated first. If the Key can be changed, then as the Key changes, there may be repeated Keys here, which violates the definition of the dictionary; if the list here is replaced by the previous We talked about tuples because tuples are immutable.
At last
Baozi, who is new to Python, you can private message me if you don’t understand anything.
I have also prepared a lot of free video tutorials, PDF e-books, and source codes! Just pick up the business card at the end of the article!