我有这些:
df1=[('f', {'abe': 1}), ('f', {'abeli': 1}), ('f', {'mos': 1}), ('f', {'esc': 1})]
df2= [('l', {'mos': 1}), ('l', {'esc': 1})]
我想生成两个数组
L1=[1,1,1,1]
L2=[0,0,1,1]
0因为'abe'和'abeli'在第二个数组中不存在。
dict1_part = [sc[1] for sc in df1]
dict2_part = [sc[1] for sc in df2]
L1=len(dict1_part)
L2=len(dict2_part)
print(L1 , L2)
buckets=[]
for items in dict1_part:
pl=items.keys()
Value=items.values()
print("pl",plant)
print("dict2_part",dict2_part)
if pl in dict2_part.keys():
buckets.append(1)
else:
buckets.append(0)
print(buckets)
答案 0 :(得分:0)
首先,在df1
中找到每个字典的键,然后使用dict.get
:
df1=[('f', {'abe': 1}), ('f', {'abeli': 1}), ('f', {'mos': 1}), ('f', {'esc': 1})]
df2= [('l', {'mos': 1}), ('l', {'esc': 1})]
df_keys= [list(b.keys())[0] for _, b in df1]
first_array = [b.get(c) for (_, b), c in zip(df1, df_keys)]
second_array = [0 if not any(x in c for _, c in df2) else [b[x] for _, b in df2 if x in b][0] for x in df_keys]
输出:
[1, 1, 1, 1]
[0, 0, 1, 1]