如何打印特定列表?

时间:2019-11-04 15:36:59

标签: python list

我想从列表中打印特定的行; “ 1. McDonalds”:“周一至周六:上午7点至12点,周日和PH:上午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.麦当劳”


2 个答案:

答案 0 :(得分:1)

您需要一个仅与一个快餐键匹配的元素,然后使用它,例如数字或示例

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

答案 1 :(得分:0)

您可以将StoreList用作StoreName和索引,以获取该商店的OpTime。

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

您不应为此使用Python Dict,因为它是无序的数据结构。