如何生成列表,其中元素与所需列表相距固定距离

时间:2018-07-02 09:01:08

标签: python combinations iterable hamming-distance

我有一个可能性列表和一个所需的输入内容:

SortedSet

我想生成关闭列表。示例:

possibles = [20, 30, 40, 50, 60, 70, 80, 100]
desired = [20, 30, 40]

我当前的版本一次只改变一个元素,因此,当距离大于1时,我就缺少很多组合。

# Distance of 1 (i.e. 1 element changes to a close-by)
[30, 30, 40]
[20, 40, 40]
[20, 30, 30]
[20, 30, 50]

# Distance of 2:
[40, 30, 40]
[30, 30, 50]
[30, 40, 40]
...

我很确定应该有一个模块可以进行这种迭代(itertools?),您能指出我写函数吗?

谢谢。

编辑:

有关尝试的更新...

我试图生成一个所需大小相同的列表,其中每个元素对应于我必须移动所需元素的数量。

def generate_close_by(possibles, desired):
    for k in range(1, 4):
        for i, elt in enumerate(desired):
            id = possibles.index(elt)

            new = desired[:]
            if id < len(possibles)-k-1:
                new[i] = possibles[id+k]
                yield (new)

            if id > k:
                new[i] = possibles[id-k]
                yield (new)

# Output
[30, 30, 40]
[20, 40, 40]
[20, 30, 50]
[20, 30, 30]
[40, 30, 40]
[20, 50, 40]
[20, 30, 60]
[50, 30, 40]
[20, 60, 40]
[20, 30, 70]

然后计划尝试创建新列表,如果不能(超出范围),则继续。尚不可行,但这可能是一个好方法。

4 个答案:

答案 0 :(得分:1)

是的,itertools在这里确实很有用。您想要的是找到possibles列表中具有所需长度的所有子集(包含重复项),并且该功能的集合为itertools.product

from itertools import product

possibles = [20, 30, 40, 50, 60, 70, 80, 100]
desired = [20, 30, 40]

def fake_hamming(cur, desired, possibles):
    assert len(cur) == len(desired)

    hamm = 0
    for i in range(len(cur)):
        assert cur[i] in possibles
        assert desired[i] in possibles
        hamm += abs(possibles.index(cur[i]) - possibles.index(desired[i]))

    return hamm

def generate_close_by(desired, possibles, dist):
    all_possible_lists = product(possibles, repeat=len(desired))
    return [l for l in all_possible_lists if fake_hamming(l, desired, possibles) == dist]

print(generate_close_by(desired, possibles,1))
>>> [(20, 20, 40), (20, 30, 30), (20, 30, 50), (20, 40, 40), (30, 30, 40)]

编辑,您可以更改产品的组合(请参见下面的@tobias_k注释),这也是fake_hamming函数xD 同样,大列表的速度确实很慢,但这是最通用的方法

答案 1 :(得分:1)

def distribute(number, bucket):
  if bucket == 1:
    yield [number]
    if number != 0:
      yield [-1 * number]
  elif number == 0:
    yield [0]*bucket
  else:
    for i in range(number+1):
      for j in distribute(number-i, 1):
        for k in distribute(i, bucket-1):
          yield j+k

def generate(possibles, desired, distance):
  for index_distance_tuple in distribute(distance, len(desired)):
    retval = desired[:]
    for i, index in enumerate(index_distance_tuple):
      if index + i < 0 or index + i >= len(possibles):
        break
      retval[i] = possibles[index + i]
    else:
      yield retval

对于距离1:

for i in generate(possibles, desired, 1):
  print(i)

输出:

[30, 30, 40]
[20, 40, 40]
[20, 20, 40]
[20, 30, 50]
[20, 30, 30]

对于距离2:

for i in generate(possibles, desired, 2):
  print(i)

输出:

[40, 30, 40]
[30, 40, 40]
[30, 20, 40]
[30, 30, 50]
[30, 30, 30]
[20, 50, 40]
[20, 40, 50]
[20, 40, 30]
[20, 20, 50]
[20, 20, 30]
[20, 30, 60]
[20, 30, 20]

答案 2 :(得分:1)

我想我将展示一个更漫长的方法,它可以更容易地推广。

我首先写下问题。

possible_pts = [20, 30, 40, 50, 60, 70, 80, 100]
starting_pt_in_idx = [0, 1, 2]
distance = 2

有3个轴可以“改变”。我首先找到轴变化的组合。

N = len(starting_pt_in_idx)
axis = list(range(N))

import itertools
axismoves = list(itertools.combinations_with_replacement(axis, distance))
print(axismoves)

接下来,我们将其装箱。例如,如果我看到轴0出现两次,则它变为[2,0,0]。

abs_movements = []
for combi in axismoves:
    move_bin = [0] * N
    for i in combi:
        move_bin[i] += 1
    abs_movements.append(move_bin)
print(abs_movements)

以上给出了绝对的动作。要找到实际的运动,我们必须考虑沿该轴的变化可以是正值或负值。

import copy
actual_movements = []
for movement in abs_movements:
    actual_movements.append(movement)
    for i, move in enumerate(movement):
        if move != 0:
            _movement = copy.deepcopy(movement)
            _movement[i] = - move
            actual_movements.append(_movement)
print(actual_movements)

最后一步是将索引转换为实际位置。因此,我们首先编写此辅助函数。

def translate_idx_to_pos(idx_vect, points):
    idx_bounds = [0, len(points) - 1]
    pos_point = [0] * len(idx_vect)
    for i, idx_pos in enumerate(idx_vect):
        if idx_pos < idx_bounds[0] or idx_pos > idx_bounds[1]:
            return None
        else:
            pos_point[i] = points[idx_pos]
    return pos_point

使用实际运动对起点索引进行操作,然后将其转换回位置。

from operator import add
final_pts = []
for movement in actual_movements:
    final_pt_in_idx = list(map(add, starting_pt_in_idx, movement))
    final_point = translate_idx_to_pos(final_pt_in_idx, possible_pts)
    if final_point is not None:
        final_pts.append(final_point)

print(final_pts)

这给出了

[40, 30, 40]
[30, 40, 40]
[30, 20, 40]
[30, 30, 50]
[30, 30, 30]
[20, 50, 40]
[20, 40, 50]
[20, 20, 50]
[20, 40, 30]
[20, 30, 60]
[20, 30, 20]

答案 3 :(得分:0)

您可以尝试一种递归方法:跟踪剩余距离并生成仅相关元素的组合。

def get_with_distance(poss, des, dist, k=0):
    if k < len(des):
        i = poss.index(des[k])
        for n in range(-dist, dist+1):
            if 0 <= i + n < len(poss):
                for comb in get_with_distance(poss, des, dist - abs(n), k+1):
                    yield [poss[i + n]] + comb
    elif dist == 0:
        yield []

如果仍然还有dist,但des列表为空,这仍然会遇到一些“死角”,但是总的来说,与生成所有预先组合,然后检查它们的距离。

如果可能的元素列表较长,则可能要先创建一个dict映射到其索引的元素,因此不必每次都poss.index(first)

示例:

possibles = [20, 30, 40, 50, 60, 70, 80, 100]
desired = [20, 30, 40]
for x in get_with_distance(possibles, desired, 2):
    print(x)

输出:

[20, 20, 30]
[20, 20, 50]
[20, 30, 20]
[20, 30, 60]
[20, 40, 30]
[20, 40, 50]
[20, 50, 40]
[30, 20, 40]
[30, 30, 30]
[30, 30, 50]
[30, 40, 40]
[40, 30, 40]