2017-09-26 103 views
1

如果满足某些条件,我试图合并两个列表(在列表内)。如果满足某些条件,将两个列表与列表进行合并

一个例子:

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]] 

之前,我甚至可以开始填补缺失值,我的代码是不正确,似乎无法得到正确的代码的最后一部分。任何帮助或提示,以改善我的代码非常感谢!

+0

为什么要用'np.arange'因此,你可以如下改写解决? '范围'会更快,你不需要NumPy作为依赖。 – MSeifert

回答

0

如果已经添加,则需要跳过下一个列表。此外,如果当前设置不符合下一次设置的条件,则需要将当前设置添加到结果列表中,而不是下一组。

下面的代码是纠正:

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) 
0

我喜欢发生器功能(参见例如"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]] 
0

您可以进行如下操作:

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]min(list_name)list_name[0]取代max(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]] 
相关问题