【发布时间】:2020-02-13 20:46:58
【问题描述】:
我正在尝试实现功能。它的工作方式应该是这样的:
- 需要两个列表。
- 标记一些索引,最好是中心几个。
- 父母双方切换标记的索引。
- 其他索引按顺序转到其父元素。
- 如果同一个元素已经存在于该父元素中,它会映射并检查其他同一个父元素所在的位置并去那里。
import random
def pm(indA, indB):
size = min(len(indA), len(indB))
c1, c2 = [0] * size, [0] * size
# Initialize the position of each indices in the individuals
for i in range(1,size):
c1[indA[i]] = i
c2[indB[i]] = i
crosspoint1 = random.randint(0, size)
crosspoint2 = random.randint(0, size - 1)
if crosspoint2 >= crosspoint1:
crosspoint2 += 1
else: # Swap the two cx points
crosspoint1, crosspointt2 = crosspoint2, crosspoint1
for i in range(crosspoint1, crosspoint2):
# Keep track of the selected values
temp1 = indA[i]
temp2 = indB[i]
# Swap the matched value
indA[i], indA[c1[temp2]] = temp2, temp1
indB[i], indB[c2[temp1]] = temp1, temp2
# Position bookkeeping
c1[temp1], c1[temp2] = c1[temp2], c1[temp1]
c2[temp1], c2[temp2] = c2[temp2], c2[temp1]
return indA, indB
a,b = pm([3, 4, 8, 2, 7, 1, 6, 5],[4, 2, 5, 1, 6, 8, 3, 7])
错误:
in pm
c1[indA[i]] = i
IndexError: list assignment index out of range
【问题讨论】:
-
抱歉,“跨界”?这和基因测序有关吗?
-
@KarlKnechtel 是的,正在尝试实现部分映射的交叉。
标签: python python-3.x list pycharm