【问题标题】:Python - drill down to a new line - List in a dictionaryPython - 向下钻取到新行 - 字典中的列表
【发布时间】:2021-04-30 20:09:09
【问题描述】:

我正在练习字典,因此我有以下内容

favorite_place = {
    'ppl1': {
        'person': 'josh',
        'places': ['Central park', 'Hollywood', 'Aspan']
    },
    'ppl2': {
        'person': 'mark',
        'places': ['Ands mountain', 'Himalaya mountain', 'Dead sea']
    },
    'ppl3': {
        'person': 'yossi',
        'places': ['times square', 'Osaka', ',McDonald\'s']
    },

}

for budy, places_info in favorite_place.items():
    ID = f"{budy}"
    PERSON_NAME = f"{places_info['person']}"
    PLACES = f"{places_info['places']}"
    print(f"\nPerson Identified by: {ID}\n"f"For {PERSON_NAME.title()}")
    print(f"The favorite places are:\n"f"\t",str(PLACES).strip('[]').replace('\'', ''))

输出:

..................... ..................
Person Identified by: ppl3
For Yossi
The favorite places are:
     times square, Osaka, ", McDonald's"

我该如何处理,例如:

 times square, Osaka, McDonalds

并使它成为这样 - 用新行分隔并且没有逗号??? :

times square
Osaka
McDonalds

【问题讨论】:

标签: python dictionary


【解决方案1】:
favorite_place = {
    'ppl1': {
        'person': 'josh',
        'places': ['Central park', 'Hollywood', 'Aspan']
    },
    'ppl2': {
        'person': 'mark',
        'places': ['Ands mountain', 'Himalaya mountain', 'Dead sea']
    },
    'ppl3': {
        'person': 'yossi',
        'places': ['times square', 'Osaka', ',McDonald\'s']
    },

}

for budy, places_info in favorite_place.items():
    ID = budy
    PERSON_NAME = places_info['person']
    PLACES = places_info['places']
    print(f"\nPerson Identified by: {ID}\nFor {PERSON_NAME.title()}")
    print(f"The favorite places are:\n", '\n '.join([i for i in PLACES]))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-02
    • 1970-01-01
    相关资源
    最近更新 更多