|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
OV ={
1: -7,
2: -3,
3:-3,
4: 0,
5:10,
6 : 6,
7: 3,
8: -5,
9: 0,
10: 0,
11: 9,
12:4,
13:-3,
14: -6,
15: 4,
16: -7,
17:-3,
18:4,
19:-7,
20: 4
}
随机交换键值对,比如1:-7与5:10,成对交换位置,结果应该是
OV ={
5:10,
2: -3,
3:-3,
4: 0,
1:-7,
6 : 6,
7: 3,
8: -5,
9: 0,
10: 0,
11: 9,
12:4,
13:-3,
14: -6,
15: 4,
16: -7,
17:-3,
18:4,
19:-7,
20: 4
}
- from random import randint,sample
- station=[i for i in range(1,20)]
- demand=[-7,-3,-3,0,10,6,3,-5,0,0,9,4,-3,-6,4,-7,-3,4,-7,4]
- L=list(zip(station,demand))
- print(L)
- print('-'*40)
- length=len(L)
- def move1():
- global L
- a=randint(0,length-1)
- temp=L[a]
- L=L[:a]+L[a+1:]
- b=randint(0,length-2)
- L.insert(b,temp)
- def move2():
- global L
- a=randint(0,length-2)
- temp=L[a:a+2]
- L=L[:a]+L[a+2:]
- b=randint(0,length-3)
- L=L[:b]+temp+L[b:]
- def move3():
- global L
- a=randint(0,length-3)
- temp=L[a:a+3]
- L=L[:a]+L[a+3:]
- b=randint(0,length-4)
- L=L[:b]+temp+L[b:]
- def move4():
- global L
- while True:
- a,b=sorted(sample(range(0,length-1),2))
- if (b-a)!=1:
- break
- temp1=L[a]
- temp2=L[b]
- L=L[:a]+L[a+1:b]+L[b+1:]
- c=randint(0,length-3)
- L.insert(c,temp1)
- d=randint(0,length-2)
- L.insert(d,temp2)
- def move5():
- global L
- a,b=sorted(sample(range(0,length-1),2))
- temp1=L[a]
- temp2=L[b]
- L=L[:a]+[L[b]]+L[a+1:b]+[L[a]]+L[b+1:]
-
-
复制代码
|
|