首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何对列表中不连续的元素进行混洗?

如何对列表中不连续的元素进行混洗?
EN

Stack Overflow用户
提问于 2020-12-19 01:21:18
回答 4查看 89关注 0票数 3

我在Python中有一个类似如下的列表:

代码语言:javascript
运行
复制
["Apple", "Banana", "Coconut", "Durian", "Elderberry", "Fig", "Guava"]

我的目标是打乱列表,以便某些元素被打乱,而其他元素则留在原来的地方。

例如,如果要对第0、1、2、4和6个元素进行混洗,则混洗的可能结果为:

代码语言:javascript
运行
复制
["Elderberry", "Banana", "Apple", "Durian", "Guava", "Fig", "Coconut"]

我该怎么做呢?random.shuffle似乎只支持单个对象/切片。单独对每个切片进行混洗只会在每个切片之间进行混洗,而不是在所有切片之间进行。

EN

回答 4

Stack Overflow用户

发布于 2020-12-19 01:36:42

这就完成了任务--它返回列表的一个新副本,不过与random.shuffle()不同。

代码语言:javascript
运行
复制
import random


def shuffle_some(source, shuffle_indices):
    # Get the things to shuffle by the given indices
    to_shuffle = [source[i] for i in shuffle_indices]
    # and shuffle them.
    random.shuffle(to_shuffle)

    # Take a shallow copy of the source list so we can
    # modify it...
    shuffled = source[:]
    # And iterate through pairs of indices in the `to_shuffle`
    # list and the target indices to place them...
    for i, source_index in enumerate(shuffle_indices):
        # ... and put the shuffled items into place.
        shuffled[source_index] = to_shuffle[i]
    return shuffled


s = ["Apple", "Banana", "Coconut", "Durian", "Elderberry", "Fig", "Guava"]

for x in range(5):
    print(shuffle_some(s, (0, 3, 4)))

输出为例如

代码语言:javascript
运行
复制
['Elderberry', 'Banana', 'Coconut', 'Apple', 'Durian', 'Fig', 'Guava']
['Durian', 'Banana', 'Coconut', 'Apple', 'Elderberry', 'Fig', 'Guava']
['Apple', 'Banana', 'Coconut', 'Durian', 'Elderberry', 'Fig', 'Guava']
['Apple', 'Banana', 'Coconut', 'Durian', 'Elderberry', 'Fig', 'Guava']
['Apple', 'Banana', 'Coconut', 'Elderberry', 'Durian', 'Fig', 'Guava']
票数 3
EN

Stack Overflow用户

发布于 2020-12-19 01:30:45

可以把它想象成将一叠纸牌分成两堆--您想要洗牌的那一堆,以及您想要保留其顺序的那一堆。将纸牌从第二堆放在原来的位置,将第一堆洗牌,然后把洗好的牌放入剩余的空格中。

代码语言:javascript
运行
复制
>>> import random
>>> fruits = ["Apple", "Banana", "Coconut", "Durian", "Elderberry", "Fig", "Guava"]
>>> shuffle_indices = (0, 1, 2, 4, 6)
>>> shuffled_items = random.sample([f for i, f in enumerate(fruits) if i in shuffle_indices], len(shuffle_indices))
>>> shuffled_fruits = [shuffled_items.pop() if i in shuffle_indices else f for i, f in enumerate(fruits)]
>>> shuffled_fruits
['Banana', 'Guava', 'Coconut', 'Durian', 'Apple', 'Fig', 'Elderberry']
票数 2
EN

Stack Overflow用户

发布于 2020-12-19 01:30:38

你可以自己硬编码。

您可以提取要混洗的值,然后对这些值进行混洗。然后,将它们重新输入到原始文件中。

代码语言:javascript
运行
复制
def my_shuffle(list_, *args):
    shuffle_these = []
    for i in args:
        shuffle_these.append(list_[i])
    random.shuffle(shuffle_these)
    for integer, i in enumerate(list_):
        if integer in args:
            value = args.index(integer)
            list_[integer] = shuffle_these[value]
    return list_

print(my_shuffle([1, 2, 3, 4, 5, 6, 7], 1, 3, 6)) #this will shuffle the indices 1, 3, and 6

结果:

代码语言:javascript
运行
复制
[1, 4, 3, 2, 5, 6, 7]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65361245

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档