[英]Generating a list of random lists
I'm new to Python, so I might be doing basic errors, so apologies first. 我是Python的新手,所以我可能正在做一些基本错误,因此首先道歉。
Here is the kind of result I'm trying to obtain : 这是我想要获得的结果:
foo = [
["B","C","E","A","D"],
["E","B","A","C","D"],
["D","B","A","E","C"],
["C","D","E","B","A"]
]
So basically, a list of lists of randomly permutated letters without repeat. 因此,基本上,是一个随机排列的字母列表的列表,没有重复。
Here is the look of what I can get so far : 这是到目前为止我能得到的外观:
foo = ['BDCEA', 'BDCEA', 'BDCEA', 'BDCEA']
The main problem being that everytime is the same permutation. 主要的问题是每次都是相同的排列。 This is my code so far :
到目前为止,这是我的代码:
import random
import numpy as np
letters = ["A", "B", "C", "D", "E"]
nblines = 4
foo = np.repeat(''.join(random.sample(letters, len(letters))), nblines)
Help appreciated. 帮助表示赞赏。 Thanks
谢谢
The problem with your code is that the line 您的代码的问题是该行
foo = np.repeat(''.join(random.sample(letters, len(letters))), nblines)
will first create a random permutation, and then repeat that same permutation nblines times. 首先会创建一个随机排列,然后重复相同的排列nblines次。 Numpy.repeat does not repeatedly invoke a function, it repeats elements of an already existing array, which you created with random.sample.
Numpy.repeat不会重复调用函数,它会重复您使用random.sample创建的现有数组的元素。
Another thing is that numpy is designed to work with numbers, not strings. 另一件事是numpy旨在用于数字,而不是字符串。 Here is a short code snippet (without using numpy) to obtain your desired result:
这是一个简短的代码片段(不使用numpy)以获得所需的结果:
[random.sample(letters,len(letters)) for i in range(nblines)]
Result: similar to this: 结果:类似于此:
foo = [
["B","C","E","A","D"],
["E","B","A","C","D"],
["D","B","A","E","C"],
["C","D","E","B","A"]
]
I hope this helped ;) 希望对您有所帮助;)
PS: I see that others gave similar answers to this while I was writing it. PS:我看到其他人在我写这篇文章时也给出了类似的答案。
np.repeat
repeats the same array. np.repeat
重复相同的数组。 Your approach would work if you changed it to: 如果将其更改为:
[''.join(random.sample(letters, len(letters))) for _ in range(nblines)]
Out: ['EBCAD', 'BCEAD', 'EBDCA', 'DBACE']
This is a short way of writing this: 这是编写此代码的一种简短方法:
foo = []
for _ in range(nblines):
foo.append(''.join(random.sample(letters, len(letters))))
foo
Out: ['DBACE', 'CBAED', 'ACDEB', 'ADBCE']
Here's a plain Python solution using a "traditional" style for
loop. 这是一个使用“传统”样式
for
循环的普通Python解决方案。
from random import shuffle
nblines = 4
letters = list("ABCDE")
foo = []
for _ in range(nblines):
shuffle(letters)
foo.append(letters[:])
print(foo)
typical output 典型输出
[['E', 'C', 'D', 'A', 'B'], ['A', 'B', 'D', 'C', 'E'], ['A', 'C', 'B', 'E', 'D'], ['C', 'A', 'E', 'B', 'D']]
The random.shuffle
function shuffles the list in-place. random.shuffle
函数random.shuffle
列表。 We append a copy of the list to foo
using letters[:]
, otherwise foo
would just end up containing 4 references to the one list object. 我们使用
letters[:]
将列表的副本附加到foo
,否则foo
最终将只包含对一个列表对象的4个引用。
Here's a slightly more advanced version, using a generator function to handle the shuffling. 这是一个稍微高级的版本,使用生成器函数来处理混洗。 Each time we call
next(sh)
it shuffles the lst
list stored in the generator and returns a copy of it. 每次我们调用
next(sh)
它都会对生成器中存储的第一个列表进行lst
,并返回其副本。 So we can call next(sh)
in a list comprehension to build the list, which is a little neater than using a traditional for
loop. 因此,我们可以在列表理解中调用
next(sh)
来构建列表,这比使用传统的for
循环更加整洁。 Also, list comprehesions can be slightly faster than using .append
in a traditional for
loop. 同样,列表组合可能比在传统的
for
循环中使用.append
稍快。
from random import shuffle
def shuffler(seq):
lst = list(seq)
while True:
shuffle(lst)
yield lst[:]
sh = shuffler('ABCDE')
foo = [next(sh) for _ in range(10)]
for row in foo:
print(row)
typical output 典型输出
['C', 'B', 'A', 'E', 'D']
['C', 'A', 'E', 'B', 'D']
['D', 'B', 'C', 'A', 'E']
['E', 'D', 'A', 'B', 'C']
['B', 'A', 'E', 'C', 'D']
['B', 'D', 'C', 'E', 'A']
['A', 'B', 'C', 'E', 'D']
['D', 'C', 'A', 'B', 'E']
['D', 'C', 'B', 'E', 'A']
['E', 'D', 'A', 'C', 'B']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.