[英]Checking if list elements are in string index list
I have two lists, which ones I want to compare their elements:我有两个列表,我想比较它们的元素:
A remote list called groups
称为
groups
的远程列表
groups = [
{'id': 'xxxx-xxx-xxx-xx-xxxxxxx', 'name': 'digital-academy'},
{'id': 'xxxx-xxx-xxx-xx-xxxxxxx', 'name': 'amazon'},
{'id': 'xxxx-xxx-xxx-xx-xxxxxxx', 'name': 'plaza'},
{'id': 'xxxx-xxx-xxx-xx-xxxxxxx', 'name': 'staging'},
{'id': 'xxxx-xxx-xxx-xx-xxxxxxx', 'name': 'almere-busbaan'},
{'id': 'xxxx-xxx-xxx-xx-xxxxxxx', 'name': 'eneco-zoc'},
{'id': 'xxxx-xxx-xxx-xx-xxxxxxx', 'name': 'healthcare'},
{'id': 'xxxx-xxx-xxx-xx-xxxxxxx', 'name': 'test'},
{'id': 'xxxx-xxx-xxx-xx-xxxxxxx', 'name': 'parametric-lab'},
{'id': 'xxxx-xxx-xxx-xx-xxxxxxx', 'name': 'femtest'},
{'id': 'xxxx-xxx-xxx-xx-xxxxxxx', 'name': 'bridge'},
{'id': 'xxxx-xxx-xxx-xx-xxxxxxx', 'name': 'geluidsschermen' },
{'id': 'xxxx-xxx-xxx-xx-xxxxxxx', 'name': 'speckle-staging'},
{'id': 'xxxx-xxx-xxx-xx-xxxxxxx', 'name': 'bern'},
{'id': 'xxxx-xxx-xxx-xx-xxxxxxx', 'name': 'maritime-research'},
{'id': 'xxxx-xxx-xxx-xx-xxxxxxx', 'name': 'aviation'},
{'id': 'xxxx-xxx-xxx-xx-xxxxxxx', 'name': 'mijnbouwstraat'},
{'id': 'xxxx-xxx-xxx-xx-xxxxxxx', 'name': 'evoluon'},
{'id': 'xxxx-xxx-xxx-xx-xxxxxxx', 'name': 'treehouse'},
{'id': 'xxxx-xxx-xxx-xx-xxxxxxx', 'name': 'training'},
{'id': 'xxxx-xxx-xxx-xx-xxxxxxx', 'name': 'monarch'},
{'id': 'xxxx-xxx-xxx-xx-xxxxxxx', 'name': 'ferdi'}
] ]
And a local list called local_groups
还有一个名为
local_groups
的本地列表
local_groups = ['speckle-staging', 'eneco-zoc', 'parametric-lab', 'digital-academy',
'amazon', 'almere-busbaan', 'healthcare', 'femtest', 'maritime-research',
'mijnbouwstraat', 'evoluon', 'treehouse', 'training', 'ferdi',
'bern', 'bridge', 'plaza', 'monarch', 'staging', 'aviation',
'geluidsschermen', 'test']
groups
and local_groups
lists contain the same amount (and same names) of groups in their name
attribute. groups
和local_groups
列表在其name
属性中包含相同数量(和相同名称)的组。 I want to check if groups
elements are present in local_groups
, that is not a problem at all.我想检查
local_groups
中是否存在groups
元素,这根本不是问题。 But when I check if local_groups
elements are in groups
, I got the following behavior:但是当我检查
local_groups
元素是否在groups
中时,我得到了以下行为:
local_str_groups = " ".join(str(x) for x in local_groups)
print("String of local groups \n", local_str_groups)
for group in groups:
if group['name'] not in local_groups:
print(group['name'], "aad group, is not present in local_users file")
elif local_str_groups not in group['name']:
print("group doesn’t exist on remote list, please create it")
else:
print("AAD groups and local groups are synchronized")
I got group doesn't exist on remote list, please create it
, so looks like local_str_groups not in group['name']
condition is getting true despite the amount and content of items are the same in both list...我的
group doesn't exist on remote list, please create it
,所以看起来local_str_groups not in group['name']
条件正在变为真,尽管两个列表中项目的数量和内容相同......
How can I make it distinct when the local_list
elements are different to groups
list elements?当
local_list
元素与groups
列表元素不同时,如何使其与众不同?
Use set() in order to find difference between the groups使用 set() 来找出组之间的差异
local_groups = ['speckle-staging', 'eneco-zoc', 'parametric-lab', 'digital-academy',
'amazon', 'almere-busbaan', 'healthcare', 'femtest', 'maritime-research',
'mijnbouwstraat', 'evoluon', 'treehouse', 'training', 'ferdi',
'bern', 'bridge', 'plaza', 'monarch', 'staging', 'aviation',
'geluidsschermen', 'test']
local_groups_set = set(local_groups)
groups = [
{'id': 'xxxx-xxx-xxx-xx-xxxxxxx', 'name': 'digital-academy'},
{'id': 'xxxx-xxx-xxx-xx-xxxxxxx', 'name': 'amazon'},
{'id': 'xxxx-xxx-xxx-xx-xxxxxxx', 'name': 'plaza'},
{'id': 'xxxx-xxx-xxx-xx-xxxxxxx', 'name': 'staging'},
{'id': 'xxxx-xxx-xxx-xx-xxxxxxx', 'name': 'almere-busbaan'},
{'id': 'xxxx-xxx-xxx-xx-xxxxxxx', 'name': 'eneco-zoc'},
{'id': 'xxxx-xxx-xxx-xx-xxxxxxx', 'name': 'healthcare'},
{'id': 'xxxx-xxx-xxx-xx-xxxxxxx', 'name': 'test'},
{'id': 'xxxx-xxx-xxx-xx-xxxxxxx', 'name': 'parametric-lab'},
{'id': 'xxxx-xxx-xxx-xx-xxxxxxx', 'name': 'femtest'},
{'id': 'xxxx-xxx-xxx-xx-xxxxxxx', 'name': 'bridge'},
{'id': 'xxxx-xxx-xxx-xx-xxxxxxx', 'name': 'geluidsschermen'},
{'id': 'xxxx-xxx-xxx-xx-xxxxxxx', 'name': 'speckle-staging'},
{'id': 'xxxx-xxx-xxx-xx-xxxxxxx', 'name': 'bern'},
{'id': 'xxxx-xxx-xxx-xx-xxxxxxx', 'name': 'maritime-research'},
{'id': 'xxxx-xxx-xxx-xx-xxxxxxx', 'name': 'aviation'},
{'id': 'xxxx-xxx-xxx-xx-xxxxxxx', 'name': 'mijnbouwstraat'},
{'id': 'xxxx-xxx-xxx-xx-xxxxxxx', 'name': 'evoluon'},
{'id': 'xxxx-xxx-xxx-xx-xxxxxxx', 'name': 'treehouse'},
{'id': 'xxxx-xxx-xxx-xx-xxxxxxx', 'name': 'training'},
{'id': 'xxxx-xxx-xxx-xx-xxxxxxx', 'name': 'monarch'},
{'id': 'xxxx-xxx-xxx-xx-xxxxxxx', 'name': 'ferdi12'}
]
groups_set = set(x['name'] for x in groups)
print(local_groups_set - groups_set)
print(groups_set- local_groups_set)
output output
{'ferdi'}
{'ferdi12'}
I think you want to find group names that exist in one list but not two lists.我认为您想查找存在于一个列表中但不是两个列表中的组名。
So here is my solution:所以这是我的解决方案:
group_names = [f['name'] for f in groups] #only names
for i in group_names:
if i not in local_groups:print(f"remote group #{group_names.index(i)} not in local_groupes.")
for i in local_groups:
if i not in group_names:print(f"local group #{local_groups.index(i)} not in remote groupes.")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.