根据字典值和python中的一些条件创建列表

时间:2019-11-28 08:16:02

标签: python list dictionary

我需要有关python和字典的帮助。

因此,基本思想是创建一个列表,该列表将在python字典上包含多个值。 我解析dic的每个键,然后如果值的数量> 1,则检查这些值是否包含特定的前缀,如果是,则将不带前缀的值放入列表中。

以下是dic:

defaultdict(<class 'list'>, {'ACTGA': ['YP_3878.3', 'HUYUII.1'], 'ACTGT': ['XP_46744.1', 'JUUIIL.2'], 'ACCTTGG': ['YP_8990.1'], 'ACCTTTT': ['YP_8992.1'], 'ATGA': ['YP_9000.1', 'YP_3222.1'], 'AAATTTGG': ['ORAAA.8', 'OTTTAX']})

这里是prefix_list = ["XP_","YP_"]

让我更好地解释一下:

我实际上想创建一个包含值内容的新sequence_list

所以基本的想法是遍历每个键,如果有> 1个值,我会根据某些条件将n-1个值放入sequence_list中。

这是一个例子:

  • 第一个键是'ACTGA',其中有2个值:YP_3878.3HUYUII.1,然后因为HUYUII.1在{{1 }},然后将其放入prefix_list

    sequence_list

  • 第二个键是print(sequence_list): ["HUYUII.1"],其中有3个值:'ACTGT'XP_46744.1JUUIIL.2,然后是JUUIIL.3和{{1 }}在JUUIIL.2中没有任何前缀,然后我将它们放入JUUIIL.3中:

    prefix_list

  • 第三个键,其中n值> 1是sequence_list,其中有3个值:print(sequence_list): ["HUYUII.1","JUUIIL.2","JUUIIL.3"]'ATGAAA''YP_9000.1',因为{{1} }在'YP_3222.1'中没有任何前缀,然后我将它们放入'HUU3222.1'中,并且由于两个前缀都剩下2个值,因此我也将第一个也放入HUU3222.1中:

    prefix_list

  • 第四个键,其中n值> 1是sequence_list,其中有2个值:sequence_listprint(sequence_list): ["HUYUII.1","JUUIIL.2","JUUIIL.3","YP_9000.1","HUU3222.1"],因为两个都没有{{ 1}},我将第一个放入'AAATTTGG'

    'ORLOP.8'

因此,最后我应该获得sequence_list,例如:

'OTTTAX'

有人有想法吗?我尝试了一些东西,但是这很困难,甚至可能很混乱:

prefix_list

2 个答案:

答案 0 :(得分:1)

如果我的代码正确无误,那么您想要实现以下目标:

prefix_list = ["XP_", "YP_"]
sequence_list = []
have_interesting_prefix = lambda v: any(
      v.startswith(prefix) for prefix in prefix_list
    )
for values in dedup_records.values():
  if len(values) > 1:
    sequence_list.extend(v for v in values if not have_interesting_prefix(v))
    prefixed = filter(have_interesting_prefix, values)
    if len(prefixed) > 1:
      sequence_list.append(prefixed[0])

答案 1 :(得分:1)

您可以通过使用函数使代码更清晰,以便更轻松地读取循环中发生的事情。

此外,出于个人喜好,我建议使用const input = ["/api/", "/docs/getting-started/", "/docs/", "/plugin/", "/plugin/user/", "/plugin/profile/"]; const mapper = input.reduce((acc, path) => { let matches = path.split(/\//).filter(Boolean), mapperKey = null; if (matches.length > 1) mapperKey = matches[0]; if (!acc[mapperKey]) acc[mapperKey] = { title: mapperKey, pages: [] }; const title = matches.pop(); acc[mapperKey].pages.push({ title, path }); return acc; }, {}) const output = Object.values(mapper); console.log(output)作为变量名,而不要使用list_,因为拼写错误可能很难处理。

方法是首先将每个列表分为两组:一组带有前缀,另一组没有前缀。之后,我们只需要验证至少有1个带前缀的项目(在这种情况下,请添加除最后一个带前缀的项目之外的所有项目,并附加所有非前缀的项目),否则我们需要保留1个非前缀的项目项,然后附加所有其他项。

liste

输出:

dedup_records = {'ACTGA': ['YP_3890.3', 'HUYUII.1'], 'ACTGT': ['XP_46744.1', 'JUUIIL.2','JUUIIL.3'], 'ACCTTGG': ['YP_8990.1'], 'ACCTTTT': ['YP_8992.1'], 'ATGAAA': ['YP_9000.1', 'YP_3222.1','HUU3222.1'], 'AAATTTGG': ['ORLOP.8', 'OTTTAX']}

prefix_list = ["XP_","YP_"]

def separate_items_with_prefix(list_, prefix_list):
    '''separates a list into two lists based on prefix
    returns two lists: one for items with prefix
    another for items without prefix
    '''
    with_prefix = []
    without_prefix = []
    for item in list_:
        if any(item.startswith(prefix) for prefix in prefix_list):
            with_prefix.append(item)
        else:
            without_prefix.append(item)
    return with_prefix, without_prefix


sequence_list = []
for val in dedup_records.values():
    if len(val) <= 1:
        continue #skip items with only upto 1 value in them
    with_prefix, without_prefix = separate_items_with_prefix(val, prefix_list)
    if with_prefix: #So there is at least 1 item in the list with prefix
        sequence_list.extend(with_prefix[:-1])
        sequence_list.extend(without_prefix)
    else: #there are no items with a prefix in the list
        sequence_list.extend(without_prefix[:-1])