【问题标题】:Create Dict from multiple Lists with duplicate Keys从具有重复键的多个列表创建字典
【发布时间】:2021-09-04 21:06:49
【问题描述】:

我有四个名为 designNamecreatorNamefabric_namesdata 的列表,data 列表包含多个重复值,我想将其映射到相关的设计名称和创建者名称,例如:

Catching Fireflies thestorysmith FABRIC_PETAL_SIGNATURE_COTTON 1.75 10.58 18.22
Catching Fireflies thestorysmith FABRIC_SATIN 1.75 11.85 19.71
Catching Fireflies thestorysmith FABRIC_COTTON_POPLIN_BRAVA 1.75 11.85 19.71....

Spoonflower Color Map spoonflower_help FABRIC_PETAL_SIGNATURE_COTTON N/A N/A 16.4
Spoonflower Color Map spoonflower_help FABRIC_SATIN N/A N/A 17.74
Spoonflower Color Map spoonflower_help FABRIC_COTTON_POPLIN_BRAVA N/A N/A 17.74...

Night Sky Stars Midnight Blue at_the_cottage FABRIC_PETAL_SIGNATURE_COTTON 1.75 10.58 18.22
Night Sky Stars Midnight Blue at_the_cottage FABRIC_SATIN 1.75 11.85 19.71
Night Sky Stars Midnight Blue at_the_cottage FABRIC_COTTON_POPLIN_BRAVA 1.75 11.85 19.71

数据:

desigName = ['Catching Fireflies', 'Spoonflower Color Map', 'Night Sky Stars Midnight Blue']
creatorName = ['thestorysmith', 'spoonflower_help', 'at_the_cottage']
fabric_names = ['FABRIC_PETAL_SIGNATURE_COTTON', 'FABRIC_SATIN', 'FABRIC_COTTON_POPLIN_BRAVA']
data = [('FABRIC_PETAL_SIGNATURE_COTTON', 1.75, 10.58, 18.22),('FABRIC_PETAL_SIGNATURE_COTTON', 1.75, 10.58, 18.22),('FABRIC_PETAL_SIGNATURE_COTTON', 1.75, 10.58, 18.22),... ('FABRIC_SATIN', 1.75, 11.85, 19.71),('FABRIC_SATIN', 1.75, 11.85, 19.71),('FABRIC_SATIN', 1.75, 11.85, 19.71),... ('FABRIC_COTTON_POPLIN_BRAVA', 1.75, 11.85, 19.71),('FABRIC_COTTON_POPLIN_BRAVA', 1.75, 11.85, 19.71),('FABRIC_COTTON_POPLIN_BRAVA', 1.75, 11.85, 19.71),...]

我正在尝试找出将这些数据转换为一个有序字典的最佳方法,如下所示。

{('Catching Fireflies', 'thestorysmith'): {'fabric_name_00': 'FABRIC_PETAL_SIGNATURE_COTTON', 'test_swatch_meter_00': 1.75, 'fat_quarter_meter_00': 10.58, 'meter_00': 18.22, 'fabric_name_01': 'FABRIC_SATIN', 'test_swatch_meter_01': 1.75, 'fat_quarter_meter_01': 11.85, 'meter_01': 19.71, 'fabric_name_02': 'FABRIC_COTTON_POPLIN_BRAVA', 'test_swatch_meter_02': 1.75, 'fat_quarter_meter_02': 11.85, 'meter_02': 19.71}}

我试过了:

for fab in fabric_names:
        print(fab)
    for name, creator in zip(designName, creatorName):
        for fab_type in fabric_names:
            Design_Name = name
            Creator_Name = creator
            test_swatch_meter = data[1]
            fat_quarter_meter = data[2]
            meter = data[3]

            if (name, creator) not in items_dict.keys():
                items_dict[(name, creator)] = {}
            itemCount = len(items_dict[(name, creator)].values()) / 4
            items_dict[(name, creator)].update({'fabric_name_%02d' %itemCount: fab_type,
            'test_swatch_meter_%02d' %itemCount: test_swatch_meter,
            'fat_quarter_meter_%02d' %itemCount: fat_quarter_meter,
            'meter_%02d' %itemCount: meter})
df = pd.DataFrame.from_dict(items_dict, orient='index').reset_index(drop=False)
df = df.rename(columns={'level_0':'designName','level_1':'screenName'})
df.to_csv('scraped_data.csv', index=False)

但无法将其格式化为上述格式。

【问题讨论】:

  • 欢迎来到Stack Overflow.!如果不查看产生问题的数据和您编写的代码,就很难回答您的问题。请阅读如何提出一个好问题并尝试发布Minimal Reproducible Example,以便我们更好地帮助您。
  • @itprorh66 我用 Minimal Reproducible Example 更新了这个问题。
  • 请勿发布代码、数据、错误消息等的图像 - 复制或在问题中键入文本。请保留将图像用于图表或演示渲染错误,这些无法通过文本准确描述的事情。有关更多信息,请参阅元常见问题解答条目Why not upload images of code/errors when asking a question?
  • @itprorh66 我根据我猜的所有要求更新了代码。
  • 这看起来很像 xy 问题。我怀疑你的数据结构的用处

标签: python python-3.x pandas list dictionary


【解决方案1】:

我不知道你为什么决定你需要一个 OrderedDict 结构来解决这个问题,我已经使用标准字典实现了解决方案,利用了自 Python 3 出现以来,标准字典按顺序返回键的事实他们被插入了。
虽然使用一系列复杂的 zip 调用很可能做到这一点,而且我相信有人会提供这样的解决方案,但我选择使用一个简单地遍历数据结构并首先为顶级字典,然后是内部字典的键值对并将它们组合起来。这是我的解决方案:

def combine_info(dn, cn, fn, dt):
    work_dict = {}
    freq = len(fn)
    for indx, val in enumerate(dn):
        ky = (val, cn[indx])
        inner_dict = {}
        for ptr, inkey in enumerate(fn):
            kp = 0
            while kp+ptr < freq:
                inner_dict[f'fabric_name_{kp:02}'] = data[kp+ptr][0]
                inner_dict[f'test_swatch_meter_{kp:02}'] = data[kp+ptr][1]
                inner_dict[f'fat_quarter_meter_{kp:02}'] = data[kp+ptr][2]
                inner_dict[f'meter_{kp:02}'] = data[kp+ptr][3]
                kp += 1
            work_dict[ky] =   inner_dict
    return work_dict  

运行 combine_info(desigName, creatorName, fabric_names, data) 会产生以下结果:

{('Catching Fireflies',
  'thestorysmith'): {'fabric_name_00': 'FABRIC_PETAL_SIGNATURE_COTTON', 'test_swatch_meter_00': 1.75, 'fat_quarter_meter_00': 10.58, 'meter_00': 18.22, 'fabric_name_01': 'FABRIC_PETAL_SIGNATURE_COTTON', 'test_swatch_meter_01': 1.75, 'fat_quarter_meter_01': 10.58, 'meter_01': 18.22, 'fabric_name_02': 'FABRIC_PETAL_SIGNATURE_COTTON', 'test_swatch_meter_02': 1.75, 'fat_quarter_meter_02': 10.58, 'meter_02': 18.22},
 ('Spoonflower Color Map',
  'spoonflower_help'): {'fabric_name_00': 'FABRIC_PETAL_SIGNATURE_COTTON', 'test_swatch_meter_00': 1.75, 'fat_quarter_meter_00': 10.58, 'meter_00': 18.22, 'fabric_name_01': 'FABRIC_PETAL_SIGNATURE_COTTON', 'test_swatch_meter_01': 1.75, 'fat_quarter_meter_01': 10.58, 'meter_01': 18.22, 'fabric_name_02': 'FABRIC_PETAL_SIGNATURE_COTTON', 'test_swatch_meter_02': 1.75, 'fat_quarter_meter_02': 10.58, 'meter_02': 18.22},
 ('Night Sky Stars Midnight Blue',
  'at_the_cottage'): {'fabric_name_00': 'FABRIC_PETAL_SIGNATURE_COTTON', 'test_swatch_meter_00': 1.75, 'fat_quarter_meter_00': 10.58, 'meter_00': 18.22, 'fabric_name_01': 'FABRIC_PETAL_SIGNATURE_COTTON', 'test_swatch_meter_01': 1.75, 'fat_quarter_meter_01': 10.58, 'meter_01': 18.22, 'fabric_name_02': 'FABRIC_PETAL_SIGNATURE_COTTON', 'test_swatch_meter_02': 1.75, 'fat_quarter_meter_02': 10.58, 'meter_02': 18.22}}  

【讨论】:

    猜你喜欢
    • 2018-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-30
    • 2021-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多