import time
# 根据商品ID求价格,用列表
def find_product_price(products,product_id):
    for id,price in products:
        if id == product_id:
            return price
    return None
products = [
    (1,100),
    (2,400),
    (3,50),
    (4,400)
]
def find_unique_price(products):
    unique_price_list = []
    for _,price in products:
        if price not in unique_price_list:
            unique_price_list.append(price)
    return len(unique_price_list)
# 字典版
def find_unique_price_set(products):
    unique_price_set = set()
    for _,price in products:
        unique_price_set.add(price)
    return len(unique_price_set)
if __name__ == "__main__":
    # 初始化字典和集合
    d1 = {"name":"jody","age":32,"gender":"male"}
    d2 = dict({"name":"jody","age":32,"gender":"male"})
    d3 = dict([('name','jody'),('age',32),('gender','male')])
    d4 = dict(name='jody',age=32,gender='male')
    print(d1 == d2 == d3 == d4)
True
s1 = {1,2,3}
s2 = set([1,2,3])
print(s1 == s2)
True
# 混合类型
s = {1,"hello",5.0}
print(s)
{1, 'hello', 5.0}
# 元素访问
d = {"name":"jody","age":32}
print(d["name"])
print(d.get("age"))
print(d.get("locate",None))
jody
32
None
s = {1,2,3}
print(s[1]) # wrong code
---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

<ipython-input-10-09ae81e9a7b6> in <module>
      1 s = {1,2,3}
----> 2 print(s[1]) # wrong code


TypeError: 'set' object does not support indexing
# 判断元素是否在字典/集合内
s = {1,2,3}
print(1 in s)
print(10 in s)
d = {"name":"jody","age":32}
print("name" in d)
print("\"location\" in d:","location" in d)
True
False
True
"location" in d: False
# 增删改查函数
d = {"name":"jody","age":32}
d["gender"] = "male" #add element
d["dob"] = "1988-11-12"
print(d)
d.pop("dob") #删除键值
print(d)
{'name': 'jody', 'age': 32, 'gender': 'male', 'dob': '1988-11-12'}
{'name': 'jody', 'age': 32, 'gender': 'male'}
s = {1,2,3}
s.add(4) #增加元素
print(s)
s.remove(4) #删除元素
print(s)
{1, 2, 3, 4}
{1, 2, 3}
# 字典排序
d = {'b':1,'a':2,'c':10}
# 根据字典键的升序排序
d_sorted_by_key = sorted(d.items(),key = lambda x:x[0])
print(d_sorted_by_key)
# 根据字典**值**的升序排序
d_sorted_by_value = sorted(d.items(),key = lambda x:x[1])
print(d_sorted_by_value)
[('a', 2), ('b', 1), ('c', 10)]
[('b', 1), ('a', 2), ('c', 10)]
# 对集合排序
s = [3,4,2,1]
s_sorted = sorted(s)
print(s_sorted)
[1, 2, 3, 4]
print("products {}".format(products))
products [(1, 100), (2, 400), (3, 50), (4, 400)]
# 根据商品ID找商品价格
print("id为2对商品价格为{}".format(find_product_price(products,2)))
id为2对商品价格为400
# 用字典来存储
products_set = {
    1:100,
    2:400,
    3:50,
    4:400
}
print("id为3的商品价格为{}".format(products_set[3]))
id为3的商品价格为50
# 看商品里有多少种不同的价格
#列表版 O(N²)
print("不同价格的数目为{}".format(find_unique_price(products)))
不同价格的数目为3
#字典版 O(N)
print("不同价格的数目为{}".format(find_unique_price_set(products)))
不同价格的数目为3
# 原始在unit4
def find_unique_price(products):
    # 初始化列表 这里从[]改为list() 同样有效
    unique_price_list = list()
    for _,price in products:
        if price not in unique_price_list:
            unique_price_list.append(price)
    return len(unique_price_list)
print("不同价格的数目为{}".format(find_unique_price(products)))
不同价格的数目为3
# 计算列表版本的时间
start_using_list = time.perf_counter()
find_unique_price(products)
end_using_list = time.perf_counter()
list_using_time = end_using_list - start_using_list
print("使用列表耗时:{}".format(list_using_time))

start_using_set = time.perf_counter()
find_unique_price_set(products)
end_using_set = time.perf_counter()
set_using_time = end_using_set - start_using_list
print("使用列表耗时:{}".format(set_using_time))
使用列表耗时:5.663299816660583e-05
使用列表耗时:0.00023742299526929855

作者:ukyo--阿涅丝