python列表索引超出范围 等于啥_python - IndexError:列表分配索引超出范围,Python
我正在嘗試實現功能。它的工作方式應該是這樣的:
它需要兩個列表。
標記一些索引,最好居中。
父母雙方都切換標記索引。
其他索引按順序轉到其父元素。
如果該父元素中已經存在相同的元素,則它將映射并檢查同一元素在其他父元素的位置并到達那里。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
最佳答案
不知道您的代碼中是否還有其他錯誤(我沒有運行它),但這是對此的解釋。在Python(與其他大多數語言一樣)中,列表(更精確的序列)索引基于0:>>> l = [1, 2, 3, 4, 5, 6]
>>>
>>> for e in l:
... print(e, l.index(e))
...
1 0
2 1
3 2
4 3
5 4
6 5
>>>
>>> l[0]
1
>>> l[5]
6
>>> l[6]
Traceback (most recent call last):
File "", line 1, in
IndexError: list index out of range
總結您的問題:
您的indA和indB列表各有6個元素([1..6])及其索引:[0..5]
您的c1和c2列表也有6個元素(索引也為[0..5])
但是,您使用的是#1中的值。作為#2列表中的索引,而值6是一個問題,因為沒有這樣的索引
要解決您的問題,您應該使用有效的索引值。要么:
在indA和indB中有適當的值(這是我選擇的值):
a, b = pmxCrossover([0, 3, 1, 2, 5, 4], [4, 0, 2, 3, 5, 1])
減去1,無論遇到indA或indB用作索引的值:
c1[indA[i] - 1] = i
作為一般建議:每當遇到錯誤時,請在出現故障的行之前添加打印語句(從中打印(部分)內容),這可能會為您提供線索,從而可以自己解決問題。
@ EDIT0
發布原始代碼(略有修改的版本),并進行索引轉換:
在算法之前:(從每個元素中減去1)具有有效索引
在算法之后:加1返回基于1的索引
code00.py:
#!/usr/bin/env python3
import sys
import random
def pmx_crossover(ind_a, ind_b):
size = min(len(ind_a), len(ind_b))
c1, c2 = [0] * size, [0] * size
# Initialize the position of each indices in the individuals
for i in range(1, size):
c1[ind_a[i]] = i
c2[ind_b[i]] = i
# Choose crossover points
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
# Apply crossover between cx points
for i in range(crosspoint1, crosspoint2):
# Keep track of the selected values
temp1 = ind_a[i]
temp2 = ind_b[i]
# Swap the matched value
ind_a[i], ind_a[c1[temp2]] = temp2, temp1
ind_b[i], ind_b[c2[temp1]] = temp1, temp2
# Position bookkeeping
c1[temp1], c1[temp2] = c1[temp2], c1[temp1]
c2[temp1], c2[temp2] = c2[temp2], c2[temp1]
return ind_a, ind_b
def main():
#initial_a, initial_b = [1, 2, 3, 4, 5, 6, 7, 8], [3, 7, 5, 1, 6, 8, 2, 4]
initial_a, initial_b = [1, 4, 2, 3, 6, 5], [5, 1, 3, 4, 6, 2]
index_offset = 1
temp_a = [i - index_offset for i in initial_a]
temp_b = [i - index_offset for i in initial_b]
a, b = pmx_crossover(temp_a, temp_b)
final_a = [i + index_offset for i in a]
final_b = [i + index_offset for i in b]
print("Initial: {0:}, {1:}".format(initial_a, initial_b))
print("Final: {0:}, {1:}".format(final_a, final_b))
if __name__ == "__main__":
print("Python {0:s} {1:d}bit on {2:s}\n".format(" ".join(item.strip() for item in sys.version.split("\n")), 64 if sys.maxsize > 0x100000000 else 32, sys.platform))
main()
print("\nDone.")
輸出(一種可能性(由于random.randint)):
[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q058424002]> "e:\Work\Dev\VEnvs\py_064_03.07.03_test0\Scripts\python.exe" code00.py
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)] 64bit on win32
Initial: [1, 4, 2, 3, 6, 5], [5, 1, 3, 4, 6, 2]
Final: [1, 3, 2, 4, 6, 5], [5, 1, 4, 3, 6, 2]
Done.
總結
以上是生活随笔為你收集整理的python列表索引超出范围 等于啥_python - IndexError:列表分配索引超出范围,Python的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python查找输出文字_Python基
- 下一篇: python跨目录调用_python 跨