繁体   English   中英

添加/编辑字符串列表作为字典的值(Python)

[英]Adding/editing list of strings as values of a dictionary (Python)

我需要帮助将字符串列表添加到字典中的键/值对。 基本上,我想获取用户输入,如果用户输入不存在作为字典中的键之一,我想创建一个新的键:与用户输入对应的值对。 如果用户输入已经作为字典中的键存在,我想向其对应的值添加更多信息。

这是我到目前为止所尝试的:

names1 = [‘string1’, ‘string2’, ‘string3’]
names2 = [’string4’, ‘string5’, ‘string6’]
names3 = [’string7, ‘string8’, ‘string9’]

a_dict = {‘list_one’: names1,
          ‘list_two’: names2,
          ‘list_three’: names3}

new_list = input(‘enter list name: ’)
new_string = input(‘enter word: ‘)

existing_lists = a_dict.keys()
existing_strings = a_dict.values()

if x in existing_lists: #if the animal type alrady exists
    for key, value in a_dict.items(): #for the key that is equal to the new pet's type
        if key == x:
            value = value.extend(x)
    else:
        a_dict.update({new_list : new_string})
print(a_dict)

所需的 output 示例 1:

enter list name: list_one
enter word: string10

a_dict = {‘list_one’: [‘string1’, ‘string2’, ‘string3’, ‘string 10’],
                ‘list_two’: [’string4’, ‘string5’, ‘string6’],
                ‘list_three’: [’string7, ‘string8’, ‘string9’],

所需的 output 示例 2:

enter list name: list_four
enter word: ‘string10’

a_dict = {‘list_one’: [‘string1’, ‘string2’, ‘string3’],
                ‘list_two’: [’string4’, ‘string5’, ‘string6’],
                ‘list_three’: [’string7, ‘string8’, ‘string9’],
                ‘list_four’ : [‘string10’]}

我目前得到的 output 完全用用户输入替换现有值,例如:

enter list name: list_one
enter word: string10

a_dict = {‘list_one’: [‘string 10’],
          ‘list_two’: [’string4’, ‘string5’, ‘string6’],
          ‘list_three’: [’string7, ‘string8’, ‘string9’]}

我真的很感激帮助!

如果键存在,则使用append如果不存在,则创建键并将值存储在列表中

names1 = ['string1', 'string2', 'string3']
names2 = ['string4', 'string5', 'string6']
names3 = ['string7', 'string8', 'string9']

a_dict = {'list_one': names1,
          'list_two': names2,
          'list_three': names3}

new_list = input('enter list name: ')
new_string = input('enter word: ')


if new_list in a_dict:
    a_dict[new_list].append(new_string)
else:
    a_dict[new_list] = [new_string]

print(a_dict)

所用变量的代码中几乎没有错误。 而另一个错位了。 此外,您需要使用 append 而不是扩展。

names1 = ['string1', 'string2', 'string3']
names2 = ['string4', 'string5', 'string6']
names3 = ['string7', 'string8', 'string9']

a_dict = {'list_one': names1,
          'list_two': names2,
          'list_three': names3}

new_list = input('enter list name: ')
new_string = input('enter word: ')

existing_lists = a_dict.keys()
existing_strings = a_dict.values()

if new_list in existing_lists: #if the animal type alrady exists
    for key, value in a_dict.items(): #for the key that is equal to the new pet's type
        if key == new_list:
            value = value.append(new_string)
else:
    a_dict.update({new_list : new_string})
print(a_dict)

单线:

new_list = input('enter list name: ')
new_string = input('enter word: ')

a_dict[new_list] = a_dict.get(new_list,[]) + [new_string]

print(a_dict)

暂无
暂无

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

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