简体   繁体   English

从一个包含列表的列表中制作一个关于 python 的字典

[英]make a dictonary on python from a list that has lists in it

i have created a list that has lists in it;我创建了一个列表,其中包含列表;

[['2020/10/07', 'AA123', '19.24', '22.00'],
 ['2020/11/17', 'BBB123', '23.59', '00.00'],
 ['2020/14/67', 'AAA123', '08.00', '16.00']]

Problem 1: However I'm unsure on how to turn this list into a dictionary.问题 1:但是我不确定如何将此列表转换为字典。 I've tried several times and nothing works for me.我已经尝试了几次,但没有任何效果对我有用。 I want the second value in each list(index 1) to be the key for the dictionary and the rest to be values.我希望每个列表中的第二个值(索引 1)作为字典的键,其余的作为值。

Problem 2: If the second element in each list exists in more than one list, I want them to be found under the same key.问题 2:如果每个列表中的第二个元素存在于多个列表中,我希望它们在同一个键下找到。

Try this:尝试这个:

my_dict = {}
for sub_list in vals:
    val_key = sub_list[1]
    if val_key in my_dict:
        my_dict[val_key].extend([sub_list[0]] + sub_list[2:])
    else:
        my_dict[val_key] = [sub_list[0]] + sub_list[2:]
  1. Create an empty dictionary to place your values into.创建一个空字典来放置您的值。
  2. Go through all entries in your data.浏览数据中的所有条目。
  3. Key the key for that entry ( sub_list[1] ), we'll use this to index our dictionary.键入该条目的键 ( sub_list[1] ),我们将使用它来索引我们的字典。
  4. If that key already exists, extend the value stored at that list with this item's non [1] values (eg ['a', 'b', 'c'] becomes ['a', 'b', 'c', 'd', 'e'] for example).如果该键已存在,则使用此项的非[1]值扩展该列表中存储的值(例如['a', 'b', 'c']变为['a', 'b', 'c', 'd', 'e']例如)。
  5. If that key doesn't exist, just create a new key in the dictionary with the non [1] values.如果该键不存在,只需在字典中使用非[1]值创建一个新键。

You can first extract the keys and values from you first list using list comprehension:您可以首先使用列表理解从第一个列表中提取键和值:

keys = [row[1] for row in l]
values = [[row[0], row[2],row[3]] for row in l]

And then create the dictionnary using the same technique:然后使用相同的技术创建字典:

{k:v for k,v in zip(keys,values)}

(the zip function allows to iterate over the 2 lists keys and values simultaneously) zip函数允许同时迭代 2 个列表键和值)

For Problem 2: unfortunately you can't have the same key multiple times in a dictionnary, you may have to use len(keys) == len(set(keys)) to test if your keys list have duplicates (the set function extracts all unique values from the list).对于问题 2:不幸的是,您不能在字典中多次使用相同的键,您可能必须使用len(keys) == len(set(keys))来测试您的键列表是否有重复项( set函数提取列表中的所有唯一值)。

raw = [
#     <date>      <key>     <val1>   <val2>
  ['2020/10/07', 'AAA123', '19.24', '22.00'],
  ['2020/11/17', 'BBB123', '23.59', '00.00'],
  ['2020/14/67', 'AAA123', '08.00', '16.00'],
]

(edited your example slightly to have a duplicate key) (稍微编辑您的示例以具有重复的键)

It sounds like you want your output to be something like this:听起来您希望您的输出是这样的:

{key: [info, ...], ...}

we can do this as follows:我们可以这样做:

from collections import defaultdict, namedtuple
Data = namedtuple('Data', ('date', 'val1', 'val2'))
res = defaultdict(list)
for date, key, val1, val2 in raw:
    data = Data(date, val1, val2)
    res[key].append(data)
res = dict(res)  # Strip defaultdict behavior

then we can see然后我们可以看到

import pprint
pprint.pprint(res)

gives us a result similar to that desired:给我们一个类似于我们想要的结果:

{'AAA123': [Data(date='2020/10/07', val1='19.24', val2='22.00'),
            Data(date='2020/14/67', val1='08.00', val2='16.00')],
 'BBB123': [Data(date='2020/11/17', val1='23.59', val2='00.00')]}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM