我有以下字典:
dict1 =
{0: [48844000000, 51713000000, 22926000000, 4106000000, 22878000000, 12352000000, 162819000000],
1: [105341000000, 37378000000, 32978000000, 175697000000, 338516000000],
2: [46236000000, 37720000000, 5522000000, 6000000000, 5980000000, 10260000000, 105718000000],
3: [91807000000, 50503000000, 142310000000, 248028000000],
4: [0, 45174000000, 45898000000, -584000000, 90488000000],
5: [338516000000]}
dict2 =
{0: [48844000000, 51713000000, 22926000000, 4106000000, 22878000000, 12352000000],
1: [],
2: [46236000000, 37720000000, 5522000000, 5980000000, 10260000000],
3: [],
4: [45174000000, 45898000000, -584000000],
5: []}
我想创建一个与dict1具有相同通用结构的第三个字典,但对于dict1中等于dict2中Key的每个Key,比较值。如果值匹配,则为true,否则为false。基本上,我想比较两个字典之间的每个键:值对,但要以与dict1匹配的格式获取值中的匹配元素。
dictionary 3 should look like:
dict3 =
{0: [true, true, true, true, true, true, false],
1: [false, false, false, false, false],
2: [true, true, true, false, true, true, false],
3: [false, false, false, false],
4: [false, true, true, true, true],
5: [true]}
有办法做到这一点吗?
我最终找到了如下的解决方案。不漂亮,但我是个傻瓜,我所需要的就是让它发挥作用。
import itertools
import pandas as pd
import numpy as np
dict1 ={0: [48844000000, 51713000000, 22926000000, 4106000000, 22878000000, 12352000000, 162819000000], 1: [105341000000, 37378000000, 32978000000, 175697000000, 338516000000], 2: [46236000000, 37720000000, 5522000000, 6000000000, 5980000000, 10260000000, 105718000000], 3: [91807000000, 50503000000, 142310000000, 248028000000], 4: [0, 45174000000, 45898000000, -584000000, 90488000000], 5: [338516000000]}
dict2 = {0: [48844000000, 51713000000, 22926000000, 4106000000, 22878000000, 12352000000], 1: [], 2: [46236000000, 37720000000, 5522000000, 5980000000, 10260000000], 3: [], 4: [45174000000, 45898000000, -584000000], 5: []}
#create a DF for dict1
df1 = df=pd.DataFrame.from_dict(dict1,orient='index')
#create a DF for dict2
df2 = df=pd.DataFrame.from_dict(dict2,orient='index')
#modify df2
df2 = df2.values.tolist() #df2 to a list
df2 = itertools.chain(*df2) #Flatten that list
df2 = [x for x in df2 if str(x) != 'nan'] #take out all nan values from the list
df3 = df1.isin(df2).astype(int) #find any values in common between df1 and df2
df4 =df3[df1.isnull()].fillna(3).replace(0, np.NaN)
df3 = pd.DataFrame(df3.values.ravel(), columns=['prediction']) #flatten the dataframe to a single column
df4 = pd.DataFrame(df4.values.ravel(), columns=['template']) #flatten the dataframe to a single column
df= pd.concat([df3,df4], axis=1, join='inner').dropna()
df= df.reset_index(drop=True)
print(df['prediction'])
基本上,正如我所理解的,您希望循环到您的dictionary中,看看每个值是否与另一个值对应。你可以先自己试试。但这里有一些提示:
-字典有不同的键,每个值都是一个列表,您可以使用两个for
循环
-我写了一个简短的程序来回答你的问题。我建议你一开始就这样创建你的dict三:
dict3 = {
0: [],
1: [],
2: [],
3: [],
4: [],
5: [],
}
因此,要循环到它并执行您想要的操作(在处理列表时,应该考虑使用append()
-你的两个字典形状不一样,所以如果没有可比较的值(例如try...except...else
语句(,你可以处理
最后,我只想指出,像您那样创建字典(通过跳过一行(会导致语法错误。所以你可以这样创建它们:
dict2 = {
0: [48844000000, 51713000000, 22926000000, 4106000000, 22878000000, 12352000000],
1: [],
2: [46236000000, 37720000000, 5522000000, 5980000000, 10260000000],
3: [],
4: [45174000000, 45898000000, -584000000],
5: []
}
希望它是有用的,练习,如果你有任何问题,请随时询问