Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python列表中随机移动一定数量的项_Python_Algorithm_List_Sorting - Fatal编程技术网

在Python列表中随机移动一定数量的项

在Python列表中随机移动一定数量的项,python,algorithm,list,sorting,Python,Algorithm,List,Sorting,所以我一直在做一些排序算法,我想运行一个测试函数,生成不同种类的列表来运行我的排序算法 一个这样的列表将是一个已经排序的列表,其中列表中的n个项目已经随机地排列(但不是全部,列表仍然应该排序,而不是n个项目) 这给了我一个大小为10的独特项目的排序列表,但我不确定如何将列表中这10个项目的n移动到一个随机位置,只是为了混合排序 import random import numpy as np # Make a sorted list of integers. x = np.array(rang

所以我一直在做一些排序算法,我想运行一个测试函数,生成不同种类的列表来运行我的排序算法

一个这样的列表将是一个已经排序的列表,其中列表中的n个项目已经随机地排列(但不是全部,列表仍然应该排序,而不是n个项目)


这给了我一个大小为10的独特项目的排序列表,但我不确定如何将列表中这10个项目的n移动到一个随机位置,只是为了混合排序

import random
import numpy as np

# Make a sorted list of integers.
x = np.array(range(100))

# Choose 10 indices at random.
r = random.sample(range(len(x)), 10)

# Copy this list and shuffle.
s = r.copy()
random.shuffle(s)

# Replace the indices with the shuffled ones.
x[r] = x[s]

请注意,这可能会使某些索引保持不变。

类似的方法可以工作。基本上,只需随机选择两个索引并切换它们,而不是多次切换同一个索引。如果在排序的数组上运行,它确实保证
n
元素是未排序的。但仅适用于偶数替换

array = list(range(10))

def shuffle_n(array, n):
    assert n <= len(array) and n % 2 == 0
    indexes = set(range(len(array)))
    for i in range(n // 2):
        a, b = random.sample(indexes, 2)
        indexes.remove(a)
        indexes.remove(b)
        array[a], array[b] = array[b], array[a]
array=list(范围(10))
def随机播放\u n(阵列,n):

assert n这里是一个受控实现。 随机选取四个索引进行切换。重新排列这些值,并将它们放回指定的四个位置。请注意,这并不保证新值都会不同

import random
import copy

testlist = random.sample(range(0, 20), 10)
testlist.sort()
print testlist

n = 4
move_set = set()
while len(move_set) < n:
    move_set.add(random.randrange(0, 10))

print move_set
move_list = list(move_set)

# Replace n elements
test1 = copy.copy(testlist)
migrant = [test1[_] for _ in move_set]
random.shuffle(migrant)
print migrant
test_case = []
for i in range(n):
    test1[move_list[i]] = migrant[i]
print test1

在这次运行中,所有四个索引都是列表中的值,这只是巧合。元素9、3、4和5的值分别为17、5、7和8。洗牌将四个移动到新位置。

为什么移动其中的n个位置很重要?似乎您可以生成随机列表,将其传递给您的代码,然后调用
testlist.sort()
,然后生成“正确”的值。您是否必须移动它们,或者是否足以重新生成它们--可能得到一个无序的值?@AustinHastings我已经打算这样做了,但是我基本上比较了很多常见排序算法在不同列表上的运行时,比如排序列表、反向排序列表、随机排序列表,我将生成各种大小的,在运行时方面,我想将移动了一些元素的排序列表与完全排序的列表进行比较,您能更清楚地定义“移动”吗?如果排序后的列表为[0,1,2,3],并且我从中生成[1,2,3,0],那么移动了多少项?答案可能是4(移动=更改索引)或1(需要拉出并重新插入以获得排序列表的最小项目数)。我猜是后者(更难实现),但其他答案似乎认为是前者。@gill我所说的“移动”是指如果我们有排序列表[0,1,2,3,4],然后我想移动n=2个随机选择的项目,一个可能的新列表将是[4,1,2,3,0],这实际上是一种非常有趣的实现方式
import random
import copy

testlist = random.sample(range(0, 20), 10)
testlist.sort()
print testlist

n = 4
move_set = set()
while len(move_set) < n:
    move_set.add(random.randrange(0, 10))

print move_set
move_list = list(move_set)

# Replace n elements
test1 = copy.copy(testlist)
migrant = [test1[_] for _ in move_set]
random.shuffle(migrant)
print migrant
test_case = []
for i in range(n):
    test1[move_list[i]] = migrant[i]
print test1
[0, 3, 4, 5, 7, 8, 9, 12, 16, 17]
set([9, 3, 4, 5])
[5, 17, 8, 7]
[0, 3, 4, 17, 8, 7, 9, 12, 16, 5]