简体   繁体   English

如何打印字典键(如果存在)及其指定格式的列表值?

[英]How to print dictionary key if it exists and its value of lists in specified format?

I'm trying to figure out how to print a dictionary key (if it exists) and its value of lists in this format (some records have a "fourth string", "fifth string"), etc. :我试图弄清楚如何以这种格式打印字典键(如果存在)及其列表值(某些记录具有“第四个字符串”、“第五个字符串”)等:

    First Name
    2018-11-05 10:12:15
    First string
    Second string
    Third string

I tried to code it this way but it didn't work我试图用这种方式编码,但没有用

name = {'First Name':['2018-11-05 10:12:15','First string', 'Second String', 
        'Third String']} 

 answer = input("Enter name: ")
            for k, v in name.items():
                if answer == k:
                    print(k, v)
                else:
                    break
                 

You can access the key directly and print the list this way to match your format:您可以直接访问密钥并以这种方式打印列表以匹配您的格式:

if answer in name:
    print(answer)
    for item in name[answer]:
        print(item)

First you need to find the value for dictionary key, then you need to iterate through the array values:首先你需要找到字典键的值,然后你需要遍历数组值:

answer = input("Enter name: ")
if answer in name:
    value = name[answer]
    for item in value:
        print (item)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM