小编His*_*shi的帖子

通过迭代字典来创建列表

我定义了这样的字典(list是整数列表):

my_dictionary = {'list_name' : list, 'another_list_name': another_list}
Run Code Online (Sandbox Code Playgroud)

现在,我想通过迭代这个字典来创建一个新列表.最后,我希望它看起来像这样:

my_list = [list_name_list_item1, list_name_list_item2, 
                list_name_list_item3, another_list_name_another_list_item1]
Run Code Online (Sandbox Code Playgroud)

等等.

所以我的问题是:我怎么能意识到这一点?

我试过了

for key in my_dictionary.keys():
    k = my_dictionary[key]
    for value in my_dictionary.values():
        v = my_dictionary[value]
        v = str(v)
        my_list.append(k + '_' + v)
Run Code Online (Sandbox Code Playgroud)

但是,在此示例的第4行中,我收到了类型错误(不可用类型:'list'),而不是所需的输出.

python dictionary loops list

4
推荐指数
1
解决办法
368
查看次数

标签 统计

dictionary ×1

list ×1

loops ×1

python ×1