python的数据结构整理-002-列表,字典,集合,怎么选?

列表,字典,集合,怎么选?

假设现在有一个电商后台存了产品的ID和价格。有一个需求是给定ID如何找出对应价格?

如果使用列表来存储数据结构,并进行查找,那么代码如下。

# products list version
def FindProductPrice(products,product_id):
    for id, price in products:
        if id == product_id:
            return price
    return none

products = [(10001001,100),(10001002,120),(10001003,130)]
print("The price of product 10001003 is {}.".format(FindProductPrice(products,10001003)))

如果使用字典来存储数据结构,那么查找代码如下。

# products dict version
products = {10001001:100,10001002:120,10001003:130}
print("The price of product 10001003 is {}.".format(products[10001003]))

当需求变为找出商品有多少种不同的价格的时候怎么办?

列表版本。

# list version
def FindUniquePriceUsingList(products):
    UniquePriceList = []
    for _, price in products:
        if price not in UniquePriceList:
            UniquePriceList.append(price)
    return len(UniquePriceList)
products = [(10001001,100),(10001002,120),(10001003,130)]
print("Number of unique price is {}.".format(FindUniquePriceUsingList(products)))

集合版本。

# set version
def FindUniquePriceUsingSet(products):
    UniquePriceSet = set()
    for _, price in products:
        UniquePriceSet.add(price)
    return len(UniquePriceSet)
products = [(10001001,100),(10001002,120),(10001003,130)]
print("Number of unique price is {}.".format(FindUniquePriceUsingSet(products)))

列表版本和集合版本的性能比较,可以明显的看到集合版本节省了大量的时间。

import time
id = [x for x in range(0, 100000)]
price = [x for x in range(200000, 300000)]
products = list(zip(id, price))

# list version
StartUsingListTime = time.perf_counter()
FindUniquePriceUsingList(products)
EndUsingListTime = time.perf_counter()
print("time elapse using list: {}".format(EndUsingListTime - StartUsingListTime))

# set version
StartUsingSetTime = time.perf_counter()
FindUniquePriceUsingSet(products)
EndUsingSetTime = time.perf_counter()
print("time elapse using set: {}".format(EndUsingSetTime - StartUsingSetTime ))


time elapse using list: 31.758130708999943
time elapse using set: 0.004608834000009665
posted @ 2021-12-21 20:40  红鲤鱼绿鲤鱼驭驴  阅读(47)  评论(0编辑  收藏  举报