簡體   English   中英

如何打印特定列表?

[英]How to print a specific list?

我想從我的列表中打印一個特定的行; “1. McDonalds”:“周一至周六:早上 7 點至中午 12 點,周日和下午:上午 10 點至晚上 10 點”

StoreOpTime = {
"1. McDonalds       ": "Mon to Sat: 7am to 12am, Sun & PH: 10am to 10pm",
"2. Subway          ": "Mon to Fri: 8am to 9pm, Sat & Sun: 11am to 6pm",
"3. KFC             ": "Mon to Fri: 7.30am to 10pm, Sat & Sun: 11am to 8pm",
"4. Fun World Cafe  ": "Mon to Fri: 8am to 8pm, Sat: 9am to 3​pm",}
StoreList = list(StoreOpTime.keys())  # to get the list of the store only

for x, y in StoreOpTime.items():
            print(x, ":", y)

打印整個列表。


for x, y in StoreOpTime.items():
            print(x[0], ":", y[0]) #print Mcdonalds operating hours

只打印我想要的列表中的第一個字母


print(StoreList[0])

只打印“1. McDonalds”


您需要一個僅匹配一個快餐鍵的元素,然后使用它,例如數字或示例

number = input("Choose a fast-food number in [1:4]")
for x, y in StoreOpTime.items():
    if x.startswith(number):
        print(x, ":", y)
        break

您可以將 StoreList 用作 StoreName 和索引來獲取該商店的 OpTime。

print{f'{StoreList[0]}:{StoreOpTime[StoreList[0]]}'

你不應該為此使用 Python Dict,因為它是一個無序的數據結構。

暫無
暫無

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

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