1

有没有办法随机打乱哪些键对应于哪些值?我找到了random.sample,但我想知道是否有更pythonic/更快的方法来做到这一点。

例子:a = {"one":1,"two":2,"three":3}

洗牌:a_shuffled = {"one":2,"two":3,"three":1}

4

2 回答 2

6
In [47]: import random

In [48]: keys = a.keys()

In [49]: values = a.values()

In [50]: random.shuffle(values)

In [51]: a_shuffled = dict(zip(keys, values))

In [52]: a_shuffled
Out[52]: {'one': 2, 'three': 1, 'two': 3}

或者,更简洁的是:

In [56]: dict(zip(a.keys(), random.sample(a.values(), len(a))))
Out[56]: {'one': 3, 'three': 2, 'two': 1}

(但我想这是您已经提出的解决方案。)


请注意,虽然 usingrandom.sample更简洁,但 usingrandom.shuffle更快一点:

import random
import string
def using_shuffle(a):
    keys = a.keys()
    values = a.values()
    random.shuffle(values)
    return dict(zip(keys, values))

def using_sample(a):
    return dict(zip(a.keys(), random.sample(a.values(), len(a))))

N = 10000
keys = [''.join(random.choice(string.letters) for j in range(4)) for i in xrange(N)]
a = dict(zip(keys, range(N)))

In [71]: %timeit using_shuffle(a)
100 loops, best of 3: 5.14 ms per loop

In [72]: %timeit using_sample(a)
100 loops, best of 3: 5.78 ms per loop
于 2013-08-01T15:03:21.967 回答
3

抱歉,让它更快的唯一方法是使用 numpy :/. 无论你做什么,它都必须以某种方式打乱所有需要时间的索引——所以在 C 中这样做会有所帮助。此外,纯粹随机和这种随机之间的区别在于你不能有重复的索引。

抱歉现在有点长 - 所以你必须做一些滚动

E.g.                                                                                                                                              


# made for python 2.7 but should be able to work in python 3
import random
import numpy as np
from time import time


def given_seq():
#general example
    start = time()
    a = {"one":1,"two":2,"three":3}
    keys = a.keys()
    random.shuffle(keys)
    a = dict(zip(keys, a.values()))

#Large example

a = dict(zip(range(0,100000), range(1,100001)))

def random_shuffle():
    keys = a.keys()
    random.shuffle(keys)
    b = dict(zip(keys, a.values()))

def np_random_shuffle():
    keys = a.keys()
    np.random.shuffle(keys)
    b = dict(zip(keys, a.values()))

def np_random_permutation():
    #more concise and using numpy's permutation option
    b = dict(zip(np.random.permutation(a.keys()), a.values()))

#if you precompute the array key as a numpy array

def np_random_keys_choice():
    akeys = np.array(a.keys())
    return dict(zip(akeys[np.random.permutation(len(akeys))],a.values()))

def np_random_keys_shuffle():
    key_indexes = np.arange(len(a.keys()))
    np.random.shuffle(key_indexes)
    return dict(zip(np.array(a.keys())[key_indexes],a.values()))

#fixed dictionary size
key_indexes = np.arange(len(a.keys()))
def np_random_fixed_keys_shuffle():
    np.random.shuffle(key_indexes)
    return dict(zip(np.array(a.keys())[key_indexes],a.values()))


#so dstack actually slows things down
def np_random_shuffle_dstack():
    keys = a.keys()
    np.random.shuffle(keys)
    return dict(np.dstack((keys, a.values()))[0])

if __name__=='__main__':
    import timeit
    # i can use global namespace level introspection to automate the below line but it's not needed yet
    for func in ['given_seq', 'random_shuffle', 'np_random_shuffle', 'np_random_permutation', 'np_random_keys_choice',
            'np_random_keys_shuffle', 'np_random_fixed_keys_shuffle']:
        print func, timeit.timeit("{}()".format(func), setup = "from __main__ import {}".format(''.join(func)), number = 200)
given_seq 0.00103783607483
random_shuffle 23.869166851
np_random_shuffle 16.3060112
np_random_permutation 21.9921720028
np_random_keys_choice 21.8105020523
np_random_keys_shuffle 22.4905178547
np_random_fixed_keys_shuffle 21.8256559372

使用 Choice/Permutation 可能看起来更好 - 但无论如何它都不会更快。不幸的是,复制通常很慢,除非它是一个小尺寸——而且没有办法传递指针/引用而不需要占用额外的一行——尽管我争论这是否使它成为“非pythonic”

即,如果您查看Python 的 Zen或者只是import this在 python 会话中执行以下操作,那么其中一行是:

虽然实用胜过纯洁。

所以它当然可以解释:)

于 2013-08-01T15:15:00.403 回答