![](/img/trans.png)
[英]How to shuffle a python list such that the occurrence of some elements are preserved?
[英]Python - shuffle only some elements of a list
我試圖將第3個列表中的元素洗牌到最后一個位置,所以前兩個將始終保持原位,例如
list = ['a?','b','c','d','e']
成
list = ['a?','b','d','e','c']
由於某種原因,這不起作用:
list = ['a?','b','c','d','e']
import random
random.shuffle(list[2:])
print list
誰知道我做錯了什么?
對我來說唯一有用的是迄今為止(EDITED):
lists = [['a?','b','c','d','e'],['1?','2','3','4','5','6','7']]
import random
for list in lists:
copy = list[2:]
random.shuffle(copy)
list[2:] = copy
print lists
認為這正是我所需要的。
你做的是這樣的:
copy = list[2:]
random.shuffle(copy)
這對原始列表沒有太大作用。 嘗試這個:
copy = list[2:]
random.shuffle(copy)
list[2:] = copy # overwrite the original
如果你想在不復制的情況下進行隨機播放,你可以嘗試編寫自己的可變切片類,如下所示(這是一個粗略的實現草圖,沒有邊界檢查等):
class MutableSlice(object):
def __init__(self, baselist, begin, end=None):
self._base = baselist
self._begin = begin
self._end = len(baselist) if end is None else end
def __len__(self):
return self._end - self._begin
def __getitem__(self, i):
return self._base[self._begin + i]
def __setitem__(self, i, val):
self._base[i + self._begin] = val
然后將原始列表包裝到其中並輸入標准shuffle:
>>> mylist = [1,2,3,4,5,6]
>>> slice = MutableSlice(mylist, 2)
>>> import random
>>> random.shuffle(slice)
>>> mylist
[1, 2, 4, 3, 5, 6]
您可以創建自己的shuffle函數,以允許您在可變序列中對切片進行洗牌。 它處理切片副本的采樣並重新分配回切片。 您必須傳遞slice()
參數而不是更熟悉的[2:]
表示法。
from random import sample
def myShuffle(x, *s):
x[slice(*s)] = sample(x[slice(*s)], len(x[slice(*s)]))
用法:
>>> lst = ['a?','b','c','d','e'] #don't use list as a name
>>> myShuffle(lst, 2) #shuffles lst[:2]
>>> lst
['b', 'a?', 'c', 'd', 'e']
>>> myShuffle(lst, 2, None) #shuffles lst[2:]
>>> lst
['b', 'a?', 'd', 'e', 'c']
要在沒有副本的情況下對列表中的一部分進行洗牌 ,我們可以使用Knuth shuffle :
import random
def shuffle_slice(a, start, stop):
i = start
while (i < stop-1):
idx = random.randrange(i, stop)
a[i], a[idx] = a[idx], a[i]
i += 1
它與random.shuffle完全相同,除了在切片上:
>>> a = [0, 1, 2, 3, 4, 5]
>>> shuffle_slice(a, 0, 3)
>>> a
[2, 0, 1, 3, 4, 5]
l[2:]
構造一個新列表, random.shuffle
嘗試“就地”更改列表,這對l
本身沒有影響。
你可以使用random.sample
:
l[2:] = random.sample(l[2:], len(l)-2)
使用列表快速刪除並插入和刪除以前的解決方案這一事實( https://stackoverflow.com/a/25229111/3449962 ):
項目清單
這將使用具有內存開銷的就地操作,這取決於列表中固定元素的數量。 線性時間。 一個可能更普遍的shuffle_subset實現:
#!/usr/bin/env python
"""Shuffle elements in a list, except for a sub-set of the elments.
The sub-set are those elements that should retain their position in
the list. Some example usage:
>>> from collections import namedtuple
>>> class CAnswer(namedtuple("CAnswer","x fixed")):
... def __bool__(self):
... return self.fixed is True
... __nonzero__ = __bool__ # For Python 2. Called by bool in Py2.
... def __repr__(self):
... return "<CA: {}>".format(self.x)
...
>>> val = [3, 2, 0, 1, 5, 9, 4]
>>> fix = [2, 5]
>>> lst = [CAnswer(v, i in fix) for i, v in enumerate(val)]
>>> print("Start ", 0, ": ", lst)
Start 0 : [<CA: 3>, <CA: 2>, <CA: 0>, <CA: 1>, <CA: 5>, <CA: 9>, <CA: 4>]
Using a predicate to filter.
>>> for i in range(4): # doctest: +NORMALIZE_WHITESPACE
... shuffle_subset(lst, lambda x : x.fixed)
... print([lst[i] for i in fix], end=" ")
...
[<CA: 0>, <CA: 9>] [<CA: 0>, <CA: 9>] [<CA: 0>, <CA: 9>] [<CA: 0>, <CA: 9>]
>>> for i in range(4): # doctest: +NORMALIZE_WHITESPACE
... shuffle_subset(lst) # predicate = bool()
... print([lst[i] for i in fix], end=" ")
...
[<CA: 0>, <CA: 9>] [<CA: 0>, <CA: 9>] [<CA: 0>, <CA: 9>] [<CA: 0>, <CA: 9>]
Exclude certain postions from the shuffle. For example, exclude the
first two elements:
>>> fix = [0, 1]
>>> lst = [CAnswer(v, i in fix) for i, v in enumerate(val)]
>>> print("Start ", 0, ": ", lst)
Start 0 : [<CA: 3>, <CA: 2>, <CA: 0>, <CA: 1>, <CA: 5>, <CA: 9>, <CA: 4>]
>>> for i in range(4): # doctest: +NORMALIZE_WHITESPACE
... shuffle_subset(lst, fix)
... print([lst[i] for i in fix], end=" ")
...
[<CA: 3>, <CA: 2>] [<CA: 3>, <CA: 2>] [<CA: 3>, <CA: 2>] [<CA: 3>, <CA: 2>]
Using a selector with the same number of elements as lst:
>>> fix = [0, 1]
>>> lst = [CAnswer(v, i in fix) for i, v in enumerate(val)]
>>> sel = [(i in fix) for i, _ in enumerate(val)]
>>> print("Start ", 0, ": ", lst)
Start 0 : [<CA: 3>, <CA: 2>, <CA: 0>, <CA: 1>, <CA: 5>, <CA: 9>, <CA: 4>]
>>> for i in range(4): # doctest: +NORMALIZE_WHITESPACE
... shuffle_subset(lst, sel)
... print([lst[i] for i in fix], end=" ")
...
[<CA: 3>, <CA: 2>] [<CA: 3>, <CA: 2>] [<CA: 3>, <CA: 2>] [<CA: 3>, <CA: 2>]
A generator as selector works fine too:
>>> fix = [0, 1]
>>> lst = [CAnswer(v, i in fix) for i, v in enumerate(val)]
>>> print("Start ", 0, ": ", lst)
Start 0 : [<CA: 3>, <CA: 2>, <CA: 0>, <CA: 1>, <CA: 5>, <CA: 9>, <CA: 4>]
>>> for i in range(4): # doctest: +NORMALIZE_WHITESPACE
... sel = ((i in fix) for i, _ in enumerate(val))
... shuffle_subset(lst, sel)
... print([lst[i] for i in fix], end=" ")
...
[<CA: 3>, <CA: 2>] [<CA: 3>, <CA: 2>] [<CA: 3>, <CA: 2>] [<CA: 3>, <CA: 2>]
"""
from __future__ import print_function
import random
def shuffle_subset(lst, predicate=None):
"""All elements in lst, except a sub-set, are shuffled.
The predicate defines the sub-set of elements in lst that should
not be shuffled:
+ The predicate is a callable that returns True for fixed
elements, predicate(element) --> True or False.
+ If the predicate is None extract those elements where
bool(element) == True.
+ The predicate is an iterable that is True for fixed elements
or len(predicate) == len(lst).
+ The predicate is a list of indices of fixed elements in lst
with len(predicate) < len(lst).
"""
def extract_fixed_elements(pred, lst):
try:
if callable(pred) or pred is None:
pred = bool if pred is None else pred
fixed_subset = [(i, e) for i, e in enumerate(lst) if pred(e)]
elif (hasattr(pred, '__next__') or len(pred) == len(lst)):
fixed_subset = [(i, lst[i]) for i, p in enumerate(pred) if p]
elif len(pred) < len(lst):
fixed_subset = [(i, lst[i]) for i in pred]
else:
raise TypeError("Predicate {} not supported.".format(pred))
except TypeError as err:
raise TypeError("Predicate {} not supported. {}".format(pred, err))
return fixed_subset
#
fixed_subset = extract_fixed_elements(predicate, lst)
fixed_subset.reverse() # Delete fixed elements from high index to low.
for i, _ in fixed_subset:
del lst[i]
random.shuffle(lst)
fixed_subset.reverse() # Insert fixed elements from low index to high.
for i, e in fixed_subset:
lst.insert(i, e)
if __name__ == "__main__":
import doctest
doctest.testmod()
我從random.shuffle復制了shuffle函數並對其進行了調整,以便它只在定義的范圍內對列表進行洗牌:
import random
a = range(0,20)
b = range(0,20)
def shuffle_slice(x, startIdx, endIdx):
for i in reversed(xrange(startIdx+1, endIdx)):
# pick an element in x[:i+1] with which to exchange x[i]
j = random.randint(startIdx, i)
x[i], x[j] = x[j], x[i]
#Shuffle from 5 until the end of a
shuffle_slice(a, 5, len(a))
print a
#Shuffle b from 5 ... 15
shuffle_slice(b, 5, 15)
print b
上面的代碼只是對指定范圍內的元素進行洗牌。 shuffle在原地完成,即不創建列表的副本。
試試這個..它更簡單,不會制作列表的任何副本。
只需使用列表索引即可保持任何元素的固定。
工作:
創建一個僅包含要隨機播放的元素的新列表。
洗牌新名單。
從原始列表中刪除您想要隨機播放的元素。
將新創建的列表插入到適當索引的舊列表中
import random list = ['a?', 'b', 'c', 'd', 'e'] v = [] p = [v.append(list[c]) for c in range(2,len(list))] #step 1 random.shuffle(v) #step 2 for c in range(2,len(list)): list.remove(list[c]) #step 3 list.insert(c,v[c-2]) #step 4 #c-2 since the part to be shuffled begins from this index of list print(list)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.