如果满足某些条件,则将两个列表与一个列表列表合并

2024-04-25 12:59:14 发布

您现在位置:Python中文网/ 问答频道 /正文

如果满足某个条件,我将尝试合并两个列表(在列表的列表中)。你知道吗

示例:

li = [[18, 19, 20, 21, 22], [25, 26, 27], [59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69]]

li2 = [[15, 16, 17], [32, 33, 34, 35], [89, 90, 91], [95, 96, 97, 98]]

条件是,如果每个列表之间的差异(或者更确切地说是距离)小于7个单位,则该列表将被合并。列表合并后,我想填写缺少的数字。你知道吗

因此预期结果如下:

li = [[18, 19, 20, 21, 22, 23, 24, 25, 26, 27], [59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69]]

li2 = [[15, 16, 17], [32, 33, 34, 35], [89, 90, 91, 92, 93, 94, 95, 96, 97, 98]]

这是我正在处理的当前代码:

new_li = []
for i in np.arange(len(li) - 1):
    current_item, next_item = li[i], li[i+1]

    if next_item[0] - current_item[-1] <= 7:
        new_li.append(current_item + next_item)

    else:
        new_li.append(next_item)

这给了我:

new_li = [[18, 19, 20, 21, 22, 25, 26, 27], [59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69]] 

应用li2的代码:

new_li2 = [[32, 33, 34, 35], [89, 90, 91], [89, 90, 91, 92, 93, 95, 96, 97, 98]]

在我开始填写缺失的值之前,我的代码是不正确的,似乎无法正确地获得代码的最后一部分。任何帮助或提示,以改善我的代码是非常感谢!你知道吗


Tags: 代码距离示例列表new单位数字li
3条回答

如果已经添加了下一个列表,则需要跳过该列表。另外,如果当前集合与下一集合不符合条件,则需要将当前集合添加到结果列表,而不是下一集合。你知道吗

以下代码已更正:

li = [[18, 19, 20, 21, 22], [25, 26, 27], [59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69]]
li2 = [[15, 16, 17], [32, 33, 34, 35], [89, 90, 91], [95, 96, 97, 98]]

new_li = []
for i in range(len(li2) - 1):
    current_item, next_item = li2[i], li2[i+1]

    if next_item[0] - current_item[-1] <= 7:
        new_li.append(current_item + next_item)
        i+=1
    else:
        new_li.append(current_item)


for item in new_li:
    print (item)

对于这类任务,我更喜欢使用生成器函数(例如"What does the “yield” keyword do?"),因为您可以在循环中轻松地使用它们(无需创建完整的列表),并在必要时将它们转换为列表(或其他容器)。你知道吗

基本上这将满足您的要求:

def merge(li):
    current = None
    for sublist in li:
        if current is None:
            # First iteration, just set the "current" then process the next sublist.
            current = sublist
            continue
        if sublist[0] - current[-1] <= 7:
            # first append the missing values
            current.extend(range(current[-1] + 1, sublist[0]))
            # then append the next sublist
            current.extend(sublist)
        else:
            # Difference is greater than 7 yield the current list and then reset it.
            yield current
            current = sublist
    # Exhausted, yield the last current then exit.
    yield current

这为您的案例提供了预期的结果(请注意,它需要在生成器函数调用周围使用list()):

>>> list(merge([[18, 19, 20, 21, 22], [25, 26, 27], [59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69]]))
[[18, 19, 20, 21, 22, 23, 24, 25, 26, 27], [59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69]]

>>> list(merge([[15, 16, 17], [32, 33, 34, 35], [89, 90, 91], [95, 96, 97, 98]]))
[[15, 16, 17], [32, 33, 34, 35], [89, 90, 91, 92, 93, 94, 95, 96, 97, 98]]

它还可以合并几个子列表,这在您的案例中是个问题:

>>> list(merge([[1,2], [5, 6], [10,11], [200]))
[[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], [200]]

您可以按以下步骤进行:

def merge(li):
  return [li[i] + range(max(li[i]) + 1, min(li[i+1])) + li[i+1] 
          if min(li[i+1]) - max(li[i])<7 
          else li[i] for i in range(len(li)-1)]

因为列表是排序的,所以可以用list_name[-1]替换max(list_name),用list_name[0]替换min(list_name)。因此,您可以将解决方案重写如下:

 def merge(li):
     return [li[i] + range(li[i][-1]+1, li[i+1][0]) + li[i+1] 
              if li[i+1][0] - li[i][-1] <7 
              else li[i] for i in range(len(li)-1)]

执行细节:

In [94]: def merge(li):
    ...:     return [li[i]+range(max(li[i])+1, min(li[i+1]))+li[i+1] 
             if min(li[i+1])-max(li[i])<7 
             else li[i] for i in range(len(li)-1)]
    ...: 
    ...: 

In [95]: merge(li)
Out[95]: [[18, 19, 20, 21, 22, 23, 24, 25, 26, 27], [25, 26, 27]]

In [96]: merge(li2)
Out[96]: [[15, 16, 17], [32, 33, 34, 35], [89, 90, 91, 92, 93, 94, 95, 96, 97, 98]]

相关问题 更多 >