简体   繁体   English

如何从列表中创建具有多个值的 Python 字典?

[英]How to Create a Python dictionary with multiple values from a list?

I have a Python list like below:我有一个如下所示的 Python 列表:

['Phylum_C3.30', 'CDgu97FdFT6pyfQWZmquhFtiKrL1yp', 'pAnstdjgs3Dzzc8I0fOLERPeXNZIuT_legend', 'pAnstdjgs3Dzzc8I0fOLERPeXNZIuT', 'Family_E3.30', 'iKUmlH47RuphW3NbqXykn0ayizhztF', 'ZzTzTLMDCHIkPBo9waDG3lBZi6u2hG_legend', 'ZzTzTLMDCHIkPBo9waDG3lBZi6u2hG', 'Class_C2.60', 'D0RRB3F0dCl39KuEZNqfdD8q9jKzUu', 'MYe9hzd8BTeg1OW00TMQQ0qc60KWIH_legend', 'MYe9hzd8BTeg1OW00TMQQ0qc60KWIH']

I wish to have a dictionary where keys would be any element that starts with 'Pylum' or 'Class' or 'Order' or 'Family' or 'Genus' and values will all values following that element until the next element with 'Pylum' or 'Class' or 'Order' or 'Family' or 'Genus'.我希望有一个字典,其中键可以是任何以“Pylum”或“Class”或“Order”或“Family”或“Genus”开头的元素,并且值将是该元素之后的所有值,直到下一个带有“Pylum”的元素或“类”或“顺序”或“家族”或“属”。

For eg:例如:

The final dictionary will look like:最终的字典将如下所示:

{
    "Phylum_C3.30": [
        'CDgu97FdFT6pyfQWZmquhFtiKrL1yp',
        'pAnstdjgs3Dzzc8I0fOLERPeXNZIuT_legend', 
        'pAnstdjgs3Dzzc8I0fOLERPeXNZIuT'
    ], 
    "Family_E3.30": [
        'iKUmlH47RuphW3NbqXykn0ayizhztF',
        'ZzTzTLMDCHIkPBo9waDG3lBZi6u2hG_legend',
        'ZzTzTLMDCHIkPBo9waDG3lBZi6u2hG'
    ],
    "Class_C2.60": [
        'D0RRB3F0dCl39KuEZNqfdD8q9jKzUu',
        'MYe9hzd8BTeg1OW00TMQQ0qc60KWIH_legend',
        'MYe9hzd8BTeg1OW00TMQQ0qc60KWIH'
    ],
}

Simply loop over the list, and if a value tests as a key store that as the most 'recent' key seen, and add a list to the dictionary for that key.只需循环遍历列表,如果一个值测试为键存储,则作为最近看到的“最近”键,然后将列表添加到该键的字典中。 Then for all other non-key values add to the list associated with the last-seen key:然后将所有其他非键值添加到与最后看到的键关联的列表中:

prefixes = ('Pylum', 'Class', 'Order', 'Family', 'Genus')
output = {}
current_key = None
for elem in inputlist:
    if any(elem.startswith(p) for p in prefixes):
        # this is a key, add it to the output
        current_key = elem
        if current_key not in output:
            output[current_key] = []
    else:
        output[current_key].append(elem)

You can tweak the way a key is handled a bit;你可以稍微调整一个键的处理方式; dropping the if current_key not in output would result in duplicate entries overwriting previous entries.删除if current_key not in output会导致重复条目覆盖以前的条目。 Or you could raise an exception for the if current_key in output case if duplicate entries is supposed to be a bug.或者,如果重复条目应该是错误, if current_key in output您可以if current_key in output案例中为if current_key in output引发异常。

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

相关问题 如何用Python中的键列表和值字典创建字典? - How to create a dictionary from a list of keys and a dictionary of values in Python? Python - 如何从另一个列表创建一个字典列表,该列表具有两个具有多个值的键? - Python - How to create a dictionary list from another list which having two keys with multiple values? 如何从列表中创建具有多个值的字典? - How to create dictionary that has multiple values from a list? 如何为列表中的一个键创建具有多个值的 Python 字典,然后创建具有一列和多行的 pandas 数据框 - How can I create a Python dictionary with multiple values for one key from a list, to then create a pandas dataframe with one column and multiple rows 从多个字典值列表创建 dataframe - Create a dataframe from multiple list of dictionary values Python:使用[0] =键和[1:] =值从列表创建字典 - Python: Create Dictionary From List with [0] = Key and [1:]= Values Python:如何从列表或字典中 range() 多个值? - Python: How to range() multiple values from list or dictionary? 从嵌套字典创建值列表 Python - Create a list of values from nested dictionary Python 从[list]字典python中删除多个值 - Remove multiple values from [list] dictionary python 如何在 Python 中的字典理解中创建值列表 - How to create a list of values in a dictionary comprehension in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM