小编sha*_*2nd的帖子

通过列表作为值迭代python字典

我正在尝试迭代看起来像这样的字典:

    d = {
    "list_one": [
        "hello",
        "two",
        "three"
    ],
    "list_two": [
        "morning",
        "rain"
    ]
}
Run Code Online (Sandbox Code Playgroud)

我正在使用这个功能:

def combine_words(d):
    for k, v in d.items():
        a = {k: ("|".join(v))}
    return a
Run Code Online (Sandbox Code Playgroud)

当我用print运行它时,我的输出只是一个键,值对.我不确定这里发生了什么.我的理想出局是:

{
'list_one': 'hello|two|three',
'list_two': 'morning|rain'
}
Run Code Online (Sandbox Code Playgroud)

python dictionary iterator

0
推荐指数
1
解决办法
44
查看次数

标签 统计

dictionary ×1

iterator ×1

python ×1