繁体   English   中英

将列表划分为子列表,使得子列表中任意 2 个元素之间的差异不应超过 k

[英]Divide a list into sublists such that difference between any 2 elements in a sublist should not exceed k

谁能为此提供一种方法。

将列表划分为子列表,使得子列表中任意 2 个元素之间的绝对差不应超过“k”值。子列表的形成与顺序无关。 子列表可以包含任意数量的元素。 子列表的数量应该最少

Example:
arr=[1,5,4,6,8,9,2]
k=3

sublists generated are [[2,1],[5,4,6],[8,9]]

Example 2:
arr=[1,13,6,8,9,3,5]
k=4

sublists generated are [[1,3,5],[6,8],[13,9]]

我们需要返回最小数量的子列表,其中 2 个列表之间的差异不得超过 k。 一个元素只能在 1 个子列表中

我已尽力解决它,但不能。 任何帮助表示赞赏

谢谢

# easier version
arr = sorted(arr)
lst = []
sublist = [arr.pop(0)]
while len(arr) > 0:
    item = arr.pop(0)
    if item - sublist[0] >= k:
        lst.append(sublist)
        sublist = [item]
    else:
        sublist.append(item)
lst.append(sublist)
print(lst)

# shorter version
arr = sorted(arr)
lst = [[arr.pop(0)]]
while len(arr) > 0:
    item = arr.pop(0)
    lst.append([item]) if item - lst[-1][0] >= k else lst[-1].append(item)
print(lst)

这是使用排序数组的简单方法:

注意。 我在这里使用了严格的阈值,更新代码以在接受相等性时使用“<=”。

def split(arr, k):
    out = []
    for i in sorted(arr):
        if not out:
            out = [[i]]
            continue
        if out[-1] and i-out[-1][0] < k:  # use <= to accept equality
            out[-1].append(i)
        else:
            out.append([i])
    return out

例子:

split([1,5,4,6,8,9,2], 3)
# [[1, 2], [4, 5, 6], [8, 9]]

split([1,13,6,8,9,3,5], 4)
# [[1, 3], [5, 6, 8], [9], [13]]
def ex(arr, k):
    arr = sorted(arr)
    new_lst = []
    sub_lst = [arr[0]]
    for i in range(1, len(arr)):
        if arr[i] - sub_lst[0] < k:
            sub_lst.append(arr[i])
        else:
            new_lst.append(sub_lst)
            sub_lst = [arr[i]]
    new_lst.append(sub_lst)
    return new_lst


arr = [1, 13, 6, 8, 9, 3, 5]
k = 4
print(ex(arr, k))

小解释:我们正在对列表进行排序。 比我们检查匹配项的第一个元素。 因为列表是排序的,所以他们将在列表中紧挨着他。 如果下一个元素不匹配,我们可以“关闭”这个 sub_lst,append 到 new_lst,然后用下一个项目初始化 sub_lst。 完成后,我们需要将 append sub_lst 转换为 new_lst,因为我们没有进入那里的 if 语句。

例子:

import random                                                                                                                                                                                 
                                                                                                                                                                                              
k=10                                                                                                                                                                                          
random.seed(10)                                                                                                                                                                               
randomList = []                                                                                                                                                                               
listOfSublist = []                                                                                                                                                                            
for i in range(0,10):                                                                                                                                                                         
    n = random.randint(1,100)                                                                                                                                                                 
    randomList.append(n)
print(randomList)   
# uncomment this to test with the example
# randomList = [1,13,6,8,9,3,5]
# k = 4                                                                                                                                             
randomList.sort()                                                                                                                                                                             
print(randomList)                                                                                                                                                                             
                                                                                                                                                                                              
n = 0                                                                                                                                                                                         
for i in range(len(randomList)-1):                                                                                                                                                            
    if randomList[i+1] - randomList[n] >= k: # when difference exceed k, create sublist                                                                                                        
        listOfSublist.append( randomList[n:i+1])                                                                                                                                              
        n=i+1                                                                                                                                                                                 
                                                                                                                                                                                              
listOfSublist.append( randomList[n:len(randomList)]) # handle the end of the list                                                                                                                                          
print(listOfSublist)

我相信有一个不排序的解决方案,您可以通过删除每一步从 randomList 中选择的数字来改进它,以防 memory 出现问题。

output:

[74, 5, 55, 62, 74, 2, 27, 60, 63, 36]
[2, 5, 27, 36, 55, 60, 62, 63, 74, 74]
[[2, 5], [27, 36], [55, 60, 62, 63], [74, 74]]

暂无
暂无

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

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