社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

一个很好的方法来洗牌然后取消对python列表的缓冲

Tenshi • 5 年前 • 1191 次点击  

假设我有一个元组列表

l=[(1,2),(3,4),(5,6),(7,8),(9,10)]

我想在一个特定的规则之后重新调整值,这样在重新调整列表之后,我可以使用相同的方法重新调整它。

一个例子是我用一个位置把整个列表移到右边,然后我可以用一个位置把无序列表移到左边,这样我就可以得到原始列表。

但这看起来有点简单所以我想知道他们是不是更有创意的方法

编辑:我的想法是,如果我给某人发送经过洗牌的列表,他可以在不知道原始列表的情况下对其进行取消洗牌,只知道用于洗牌的方法

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/40987
 
1191 次点击  
文章 [ 3 ]  |  最新文章 5 年前
aws_apprentice
Reply   •   1 楼
aws_apprentice    6 年前

你在找 deque ?

from collections import deque

d = deque([(1,2),(3,4),(5,6),(7,8),(9,10)])

d.rotate(1)
deque([(9, 10), (1, 2), (3, 4), (5, 6), (7, 8)])

d.rotate(-1)
deque([(1, 2), (3, 4), (5, 6), (7, 8), (9, 10)])
canton7
Reply   •   2 楼
canton7    6 年前

我想你可以应用任何你喜欢的洗牌,只要你可以种子你的随机源。

拿一张数字从0到N的单子,洗牌。使用此列表的顺序对元组列表进行无序排列,例如,如果无序排列后列表的第一个元素是 5 ,则无序元组列表中的第一个元素是 l[5] . 然后可以做相反的操作:无序元组列表中的第一个元素是无序元组列表中的第五个元素。

如果你种子随机源,你可以通过在0到n的同一个列表上运行相同的shuffle来重新创建随机数列表。然后你可以使用这个来取消对随机元组列表的shuffled,就像以前一样。

编辑 :trincot的答案实现了这个想法,但使用了工作示例代码。

trincot
Reply   •   3 楼
trincot    6 年前

您可以选择一些算法来确定一个种子,该种子可以从列表本身派生,而不依赖于其顺序。

例如,对于示例数据结构,seed可以是所有值的总和。然后使用这个种子,您将生成一个从0到n-1的随机(但确定性)数字排列。这种排列可以用作shuffle和unshuffle函数的基础:

import random

def getperm(l):
    seed = sum(sum(a) for a in l)
    random.seed(seed)
    perm = list(range(len(l)))
    random.shuffle(perm)
    random.seed() # optional, in order to not impact other code based on random
    return perm

def shuffle(l):
    perm = getperm(l)
    l[:] = [l[j] for j in perm]

def unshuffle(l):
    perm = getperm(l)
    res = [None] * len(l)
    for i, j in enumerate(perm):
        res[j] = l[i]
    l[:] = res

示例调用:

l=[(1,2),(3,4),(5,6),(7,8),(9,10)]   
print(l)    
shuffle(l)
print(l) # shuffled
unshuffle(l)
print(l)  # the original