while parent2[j] in child1[split_point1:i]:
时间: 2024-01-06 19:06:05 浏览: 28
这是一个 while 循环语句,它的条件是 parent2[j] 在 child1[split_point1:i] 中出现。
在这个语句中,parent2 和 child1 是两个列表,split_point1 和 i 是两个索引。while 循环的目的是找到 parent2 列表中在 child1[split_point1:i] 中出现的元素。
具体地说,while 循环会在 parent2[j] 在 child1[split_point1:i] 中出现时执行。循环体中的语句会被执行,然后 j 的值会增加 1。循环会一直执行,直到 parent2[j] 不再在 child1[split_point1:i] 中出现为止。
相关问题
if parent2[i] not in child1[split_point1:i]: j = i while parent2[j] in child1[split_point1:i]: j = parent1.index(parent2[j]) child1[i], child1[j] = child1[j], child1[i] # 查找第二个交叉位置在parent1中对应的值 if parent1[i] not in child2[split_point1:i]: j = i while parent1[j] in child2[split_point1:i]: j = parent2.index(parent1[j]) child2[i], child2[j] = child2[j], child2[i] return child1, child2修改以上代码
这段代码是遗传算法中的交叉操作,其中存在一个错误。在第8行和第14行,应该将parent1和parent2更换为child1和child2。修改后的代码如下:
```
def crossover(parent1, parent2, split_point1, split_point2):
child1 = parent1.copy()
child2 = parent2.copy()
for i in range(split_point1, split_point2):
# 查找第一个交叉位置在child1中对应的值
if parent2[i] not in child1[split_point1:i]:
j = i
while parent2[j] in child1[split_point1:i]:
j = child1.index(parent2[j])
child1[i], child1[j] = child1[j], child1[i]
# 查找第二个交叉位置在child2中对应的值
if parent1[i] not in child2[split_point1:i]:
j = i
while parent1[j] in child2[split_point1:i]:
j = child2.index(parent1[j])
child2[i], child2[j] = child2[j], child2[i]
return child1, child2
```
def check(i,j): t=[0]*5 for x in range(4): t[d[i][x]]=1 #检查同一行 t[d[x][j]]=1 #检查同一列 #检查对角线 if i<=1 and j<=1: t[d[1-i][1-j]]=1 elif i<=1 and j>=2: t[d[1-i][5-j]]=1 elif i>=2 and j<=1: t[d[5-i][1-j]]=1 elif i>=2 and j>=2: t[d[5-i][5-j]]=1 k=0 for a in range(1,5): if t[a]==0: __________ else: k+=1 if __________: return n else: return 0 d=[[0 for j in range(4) ] for i in range(4) ] for i in range(4): d[i]=list(map(int,input().split(","))) flag=False while __________: flag=True for i in range(4): for j in range(4): if d[i][j]==0: d[i][j]=check(i,j) if __________: flag=False for i in range(4): for j in range(4): print(d[i][j],end=" ") print(" ")
缺失的代码应该是:
```python
if k == 4:
return n
else:
return 0
```
```python
while flag:
flag = False
for i in range(4):
for j in range(4):
if d[i][j] == 0:
d[i][j] = check(i, j)
if d[i][j] != 0:
flag = True
```
完整的代码如下:
```python
def check(i, j):
t = [0] * 5
for x in range(4):
t[d[i][x]] = 1 #检查同一行
t[d[x][j]] = 1 #检查同一列
#检查对角线
if i <= 1 and j <= 1:
t[d[1 - i][1 - j]] = 1
elif i <= 1 and j >= 2:
t[d[1 - i][5 - j]] = 1
elif i >= 2 and j <= 1:
t[d[5 - i][1 - j]] = 1
elif i >= 2 and j >= 2:
t[d[5 - i][5 - j]] = 1
k = 0
for a in range(1, 5):
if t[a] == 0:
k += 1
if k == 4:
return n
else:
return 0
d = [[0 for j in range(4)] for i in range(4)]
for i in range(4):
d[i] = list(map(int, input().split(",")))
flag = False
while flag:
flag = False
for i in range(4):
for j in range(4):
if d[i][j] == 0:
d[i][j] = check(i, j)
if d[i][j] != 0:
flag = True
for i in range(4):
for j in range(4):
print(d[i][j], end=" ")
print(" ")
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)