Python 2.7 如何在Python2中检查字典值列表?
你好,我有一个字符串列表和字典列表 我想比较字典值列表上的字符串列表。 例如:Python 2.7 如何在Python2中检查字典值列表?,python-2.7,Python 2.7,你好,我有一个字符串列表和字典列表 我想比较字典值列表上的字符串列表。 例如: # List of strings my_list = ['john', 'james', 'joey'] # List of dictionary results = [{name: 'john', age: 20}, {name: 'james', age: 25}] # This is my code and its wrong. for name in my_list: # getting error h
# List of strings
my_list = ['john', 'james', 'joey']
# List of dictionary
results = [{name: 'john', age: 20}, {name: 'james', age: 25}]
# This is my code and its wrong.
for name in my_list:
# getting error here that said 'list indices must be integers, not unicode'
if name is not in results['name']:
# I want to add joey here
new_person = {
'name' = 'joey',
'age' = 30
}
results.append(new_person)
@Mark,这里我展示了3种解决问题的方法。请检查它,让我知道它是否满足您的问题与一些新的输入和预期的输出集。 我将在此基础上更新代码 第一种方法-使用列表理解,extend(),set() 在网上试试吧 »输出
[{'age': 20, 'name': 'john'}, {'age': 25, 'name': 'james'}, {'age': 30, 'name': 'joey'}]
[{'name': 'john', 'age': 20}, {'name': 'james', 'age': 25}, {'name': 'joey', 'age': 30}]
»第二种方法-使用set()
在网上试试吧
&»输出
[{'age': 20, 'name': 'john'}, {'age': 25, 'name': 'james'}, {'age': 30, 'name': 'joey'}]
»第三种方法-使用嵌套循环
在网上试试吧
»输出
[{'age': 20, 'name': 'john'}, {'age': 25, 'name': 'james'}, {'age': 30, 'name': 'joey'}]
[{'name': 'john', 'age': 20}, {'name': 'james', 'age': 25}, {'name': 'joey', 'age': 30}]
假设在
my_列表
或结果
中没有重复的名称。您可以使用set()
简化您想要使用的任何交叉口操作:
# List of strings, Preferably convert this to a set
my_list = set(['john', 'james', 'joey'])
# List of dictionary
results = [{name: 'john', age: 20}, {name: 'james', age: 25}]
names_in_list = set([result['name'] for result in results])
names_to_add = names_in_list ^ my_list # This returns the difference between the two sets
# Add each of the names to the list
for name in names_to_add:
new_person = {
'name': name,
'age': 30
}
results.append(new_person)
一种简单的方法是检查字典列表中的每一项是否存在于my_list
中,如下所示:
my_list = ['john', 'james', 'joey']
names_in_list = [result['name'] for result in results]
for name in my_list:
if name not in names_in_list:
# Add the name to the results list
new_person = {
'name': name,
'age' : 30
}
results.append(new_person)
[{'age': 20, 'name': 'john'},
{'age': 25, 'name': 'james'},
{'age': 30, 'name': 'joey'}]
结果如下:
my_list = ['john', 'james', 'joey']
names_in_list = [result['name'] for result in results]
for name in my_list:
if name not in names_in_list:
# Add the name to the results list
new_person = {
'name': name,
'age' : 30
}
results.append(new_person)
[{'age': 20, 'name': 'john'},
{'age': 25, 'name': 'james'},
{'age': 30, 'name': 'joey'}]
您的词典列表无效。你确定它不是
'name'
而只是name
作为键吗?是的,我确定。。这只是一个例子,但真正的例子是来自APIs的响应,所以你想在my_list
中找到results
中不存在的人的列表,对于那些不存在的人,你想把他们添加到results
字典中,对吗?是的,先生@SudheeshSinganamalla,我刚刚接触pythonAre你还保证我的字典列表的长度要大于我的字典列表的长度吗?这是一件非常昂贵的事情。您正在检查my_list
中每个项目的整个results
列表,然后相应地添加成员列表。我刚刚修改了代码,使其正常工作。让我找到最简单的代码,然后更新它。谢谢。你的代码对我有用。。但是,如果结果将是数千或更多,这将需要一些时间more@Mark现在,我使用set()
的概念添加了另一种方法。也在寻找其他方法。请检查。@RishikeshAgrawani好的,先生。。我会回来看看你的例子。。非常感谢。