37,720
社区成员
发帖
与我相关
我的任务
分享
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import math
import random
intCount =30
intInterval=10
j = [ i for i in range(intCount)]
k = ['S{:0>3}'.format(i) for i in range(intCount)]
for i in range(math.ceil(len(k)/intInterval)):
random.shuffle(j[i * intInterval:(i+1)*intInterval])
random.shuffle(k[i * intInterval:(i+1)*intInterval])
print(k,j)
#结果是 ['S000', 'S001', 'S002', 'S003', 'S004', 'S005', 'S006', 'S007', 'S008', 'S009', 'S010', 'S011', 'S012', 'S013', 'S014', 'S015', 'S016', 'S017', 'S018', 'S019', 'S020', 'S021', 'S022', 'S023', 'S024', 'S025', 'S026', 'S027', 'S028', 'S029'] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
[Finished in 0.2s]
#并没有对数组按切片洗牌。
j = [i for i in range(30)]
ndarray=np.array(j)
_j= j[0:10]
random.shuffle(_j)
random.shuffle(j[10:])
random.shuffle(ndarray)
j[0:10]=_j
print j
#前10个已排序 out:[6, 9, 3, 5, 1, 0, 8, 2, 7, 4, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
print ndarray
#已排序
#综上所述:列表切片产生的是列表的副本,而不是同一份空间;ndarray相反。 out:[ 7 0 9 5 14 23 27 20 4 12 3 13 22 19 18 28 8 11 1 2 21 10 15 17 24 25 6 16 26 29]
random.shuffle(j)
random.shuffle(k)