随机交换字典里面的键值对,急急急
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 choice
from itertools import combinations
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
}
print(OV)
print('-'*40)
a=choice(list(combinations(OV.keys(),2)))
OV],OV]=OV],OV]
print(OV) 冬雪雪冬 发表于 2018-11-14 14:43
字典是无序的
字典的遍历是根据什么的 塔利班 发表于 2018-11-14 14:51
这个程序只是随机交换了值,但是键没有交换到 JeremyFeng 发表于 2018-11-14 14:59
这个程序只是随机交换了值,但是键没有交换到
嗯。字典是无序的,你这么修改顺序的意义是什么,访问还是按哈希值得到的放哪里又有什么关系 塔利班 发表于 2018-11-14 15:02
嗯。字典是无序的,你这么修改顺序的意义是什么,访问还是按哈希值得到的放哪里又有什么关系
这个程序只是我要做的东西的部分,key和value是固定搭配的,不能只修改一个,或者除了字典,还有其他方法实现吗 用列表呗,每个元素是原来字典的key,value二元组就行了
就实现了你所谓的有序 塔利班 发表于 2018-11-14 15:14
用列表呗,每个元素是原来字典的key,value二元组就行了
就实现了你所谓的有序
可以帮敲一下吗 from random import choice
from itertools import combinations
OV =[['a',1],['b',2],['c',3]]
print(OV)
print('-'*40)
a=choice(list(combinations(OV,2)))
a,a=a,a
print(OV) 塔利班 发表于 2018-11-14 15:22
我把我要做的东西发给你了,帮忙看一下,谢谢你 塔利班 发表于 2018-11-14 15:22
字典,数组,链表,我不确定用哪一种方法做比较简单,一个station除了demand,还有坐标,需要进行计算 嗯,我上面的好像还是换值,你要的是值和键绑定,对其进行更换吧
你刚才问的是最后一项?还是都做了? 塔利班 发表于 2018-11-14 16:11
嗯,我上面的好像还是换值,你要的是值和键绑定,对其进行更换吧
你刚才问的是最后一项?还是都做了?
我上传了一个附件,其实是每一个station,demand,坐标是绑定在一起的,是一个局部搜索,寻找最短路径
给定一个系列,将这个序列打乱,寻找最优解
我之前用链表试了一下,感觉行不通,现在完整的一项都还没有做完呢,好慌
请大神请教 from random import randint,sample
station=
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
L=L[:a]+L
b=randint(0,length-2)
L.insert(b,temp)
def move2():
global L
a=randint(0,length-2)
temp=L
L=L[:a]+L
b=randint(0,length-3)
L=L[:b]+temp+L
def move3():
global L
a=randint(0,length-3)
temp=L
L=L[:a]+L
b=randint(0,length-4)
L=L[:b]+temp+L
def move4():
global L
while True:
a,b=sorted(sample(range(0,length-1),2))
if (b-a)!=1:
break
temp1=L
temp2=L
L=L[:a]+L+L
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
temp2=L
L=L[:a]+]+L+]+L
有一种字典可以有序,就是OrderDict类
你可以:
from collections import OrderDict
你可以查询一下它的用法 塔利班 发表于 2018-11-14 16:38
好的,谢谢,再帮我看一下这个,图片里面的的操作,要怎么敲 喜欢吃菠菜 发表于 2018-11-15 13:59
有一种字典可以有序,就是OrderDict类
你可以:
from collections import OrderDict
好的,谢谢你 第一个
from random import randint
demand=
print(demand)
print('-'*40)
def ins(x):
l=len(demand)
if x in demand:
t=randint(0,l-1)
demand.insert(t,x)
else:
for i in range(2):
t=randint(0,l-1+i)
demand.insert(t,x)
ins(3)
#ins(33)
print(demand) 第二个,你说的切割要每个单元不能为空吧
from random import randint,shuffle
L=
print(L)
print('----------cut----------')
l=len(L)
t=[]
for i in range(4):
l=len(L)
a=randint(1,l-4+i+1)
if i==3:
t.append(L)
else:
t.append(L[:a])
L=L
L=[]
print(*t)
print('----------reunion----------')
shuffle(t)
for each in t:
L+=each
print(L)
页:
[1]
2