我有这样一个嵌套字典:
{108: {Wallmart: {'ca': {'good': 'busy'}}},
204: {Wallmart: {'ny': {'good': 'busy'}}},
205: {Wallmart: {'ny': {'great': 'busy'}}},
110: {CVS: {'ny': {'great': 'busy'}}},
184: {Wallmart: {'fl': {'great': 'busy'}}},
185: {Wallmart: {'fl': {'bad': 'busy'}}},
105: {Wallmart: {'ga': {'bad': 'busy'}}},
497: {Wallmart: {'ga': {'bad': 'busy'}}},
400: {RiteAid: {'dc': {'good': 'busy'}}},
406: {RidaAid: {'dc': {'geat': 'busy'}}},
367: {Other: {'tx': {'bad': 'busy'}}}}
我需要做的是迭代这些数据,找到状态相同但店名不同的密钥。例如,根据上面提供的数据,输出结果应仅为:
204: {Wallmart: {'ny': {'good': 'busy'}}},
205: {Wallmart: {'ny': {'great': 'busy'}}},
110: {CVS: {'ny': {'great': 'busy'}}},
因为州(纽约)匹配,但店名(沃尔玛、cvs)不匹配
然后,我会做一个user_input
询问此代码的用户,以指定他们是想要“Wallmart”还是“CVS”。这一部分我没问题,但从这本词典中提取信息是很困难的
因此,您要做的是根据州对数据进行分组
输出:
您的dict结构是:
使用以下结构会更容易:
对于这种转换,我们需要通过获取每个级别中的唯一键并使用它访问下一个级别来获得状态。我们还将使用^{} 为每个状态动态创建列表:
现在,您只需访问带有所需状态的
states
。这是一个示例打印功能:并使用它:
我试图通过以下方法部分解决这个问题
DataFrame
其思想是使用
recursion
并将字典扁平化为res
然后使用5的滑动窗口连续提取5个元素-
divide_chunks_sliding
利用上述功能,创建
df
并与store
上的self合并df_merge
将包含具有相同store
和不同Brand
的所有行,但是将其转换回原始结构仍处于挂起状态相关问题 更多 >
编程相关推荐