簡體   English   中英

如何調用列表名稱在變量中的列表

[英]How to call a list where the name of the list is in a variable

休息 10 年后,我回到 python,我似乎忘記了基礎知識。

我編寫了以下有效的代碼,但它比應有的更長。

def index():
    manu = request.args.get('manufacturer')
    print(manu)
    url = random.choice(get_list(manu))
    print(url)
    return render_template("index.html", url=url)


def get_list(list_name):
    if list_name == "audi":
        return [
                "https://cars-docker-images.s3.eu-west-2.amazonaws.com/audi/audi_01.jpg",
                "https://cars-docker-images.s3.eu-west-2.amazonaws.com/audi/audi_02.jpg",
                "https://cars-docker-images.s3.eu-west-2.amazonaws.com/audi/audi_03.jpg",
                "https://cars-docker-images.s3.eu-west-2.amazonaws.com/audi/audi_04.jpg",
                "https://cars-docker-images.s3.eu-west-2.amazonaws.com/audi/audi_05.jpg"
            ]
    else:
        return [
                "https://cars-docker-images.s3.eu-west-2.amazonaws.com/bmw/bmw_01.jpg",
                "https://cars-docker-images.s3.eu-west-2.amazonaws.com/bmw/bmw_02.jpg",
                "https://cars-docker-images.s3.eu-west-2.amazonaws.com/bmw/bmw_03.jpg",
                "https://cars-docker-images.s3.eu-west-2.amazonaws.com/bmw/bmw_04.jpg",
                "https://cars-docker-images.s3.eu-west-2.amazonaws.com/bmw/bmw_05.jpg"
            ]

在我看來,第二個 function 不應該是必需的,因為應該有一種方法可以按名稱調用圖像列表,即使它位於從請求中收集的變量中。 但實際發生的是變量是一個字符串,而選擇來自該字符串。

有沒有一種簡單的方法可以從變量中調用列表,以便列表位於同一個 function 中? 這種,我知道這不起作用:

url = random.choice(list(manu)

您可以嘗試將字符串存儲在字典中的列表中。 IE

x = dict()
x["audi"] = [
                "https://cars-docker-images.s3.eu-west-2.amazonaws.com/audi/audi_01.jpg",
                "https://cars-docker-images.s3.eu-west-2.amazonaws.com/audi/audi_02.jpg",
                "https://cars-docker-images.s3.eu-west-2.amazonaws.com/audi/audi_03.jpg",
                "https://cars-docker-images.s3.eu-west-2.amazonaws.com/audi/audi_04.jpg",
                "https://cars-docker-images.s3.eu-west-2.amazonaws.com/audi/audi_05.jpg"
            ]

你可以試試這個

def index():
    manu = request.args.get('manufacturer')
    print(manu)
    audi = "https://cars-docker-images.s3.eu-west-2.amazonaws.com/audi/audi_0"
    bmw = "https://cars-docker-images.s3.eu-west-2.amazonaws.com/bmw/bmw_0"
    _list = {
        "audi": [ audi + str(i) + ".jpg" for i in range(1, 6) ],
        "bmw": [ bmw + str(i) + ".jpg" for i in range(1, 6) ]
    }
    url = random.choice( _list[manu] )
    print(url)
    return render_template("index.html", url=url)

由於每個圖像的 URL 除了圖像編號之外都是相同的,我們可以通過將相似部分存儲到變量中來創建列表。

並使用audibmw作為鍵將 URL 存儲到字典中。 Dictionary 將幫助我們輕松調用帶有 key 的列表進入random方法。

以下應該有效:

def index():
    manu = request.args.get('manufacturer')
    print(manu)

    no_of_images_for_manu = {'audi': 10,
                             'bmw': 5}
    all_urls = dict()
    for manu, no_of_images in no_of_images_for_manu.items():
        if manu not in all_urls:
            all_urls[manu] = []

        for manu_index in range(1, no_of_images + 1):
            # Zero padding for manu_index using manu_index:02 only works for Python >= 3.6. For other versions the code
            # is different.
            all_urls[manu].append(f'https://cars-docker-images.s3.eu-west-2.amazonaws.com/{manu}/{manu}_{manu_index:02}.jpg')

    url = random.choice(all_urls[manu])
    return render_template("index.html", url=url)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM