简体   繁体   English

random.choice 总是打印相同的结果

[英]random.choice prints always the same result

i'm pretty new to python but i know how to use most of the things in it, included random.choice.我对 python 很陌生,但我知道如何使用其中的大部分内容,包括 random.choice。 I want to choose a random file name from 2 files list .我想从 2 个文件列表中选择一个随机文件名。

To do so, i'm using this line of code:为此,我正在使用这行代码:

minio = Minio('myip',
                access_key='mykey',
                secret_key='mykey',
              )

images = minio.list_objects('mybucket', recursive=True)

for img2 in images:
    names = img2.object_name

print(random.choice([names]))

Everytime i try to run it, it prints always the same file's name (c81d9307-7666-447d-bcfb-2c13a40de5ca.png)每次我尝试运行它时,它总是打印相同的文件名(c81d9307-7666-447d-bcfb-2c13a40de5ca.png)

I tried to put the "print" function in the "for" block, but it prints out both of the files' names我试图将“打印” function 放在“for”块中,但它会打印出两个文件的名称

You are setting the variable names to one specific instsance of images right now.您现在正在将变量names设置为一个特定的图像实例。 That means it is only a single value.这意味着它只是一个单一的值。 Try adding them to an array or similar instead.尝试将它们添加到数组或类似中。

For example:例如:

names = [img2.object_name for img2 in images]

print(random.choice(names))
minio = Minio('myip',
                access_key='mykey',
                secret_key='mykey',
              )

images = minio.list_objects('mybucket', recursive=True)
names = []

for img2 in images:
    names.append(img2.object_name)

print(random.choice([names]))

Try this, the problem may that your names was not list varible试试这个,问题可能是你的名字没有列出变量

If you want to choose one of the file names, try this:如果要选择其中一个文件名,请尝试以下操作:

minio = Minio('myip',
                access_key='mykey',
                secret_key='mykey',
              )

images = minio.list_objects('mybucket', recursive=True)

names = []
for img2 in images:
    names.append(img2.object_name)

print(random.choice(names))

The first code tries to randomly select a value from the array, [names] , but this only contain a single value, the value of the names variable after the last iteration of the for loop.第一个代码尝试从数组[names]中随机 select 一个值,但这仅包含一个值,即 for 循环最后一次迭代后的names变量的值。 Instead of doing that, create an array, names and append the values of img2.object_name from each iteration of the for loop to this array.而不是这样做,创建一个数组, namesimg2.object_name从 for 循环的每次迭代到该数组的 img2.object_name 的值。 And use this array to get the random name.并使用此数组来获取随机名称。

Your names is just a simple variable that contains the most recently seen image name, rather than a list full of them.你的names只是一个简单的变量,包含最近看到的图像名称,而不是一个完整的列表。

The very last line of your script works as the one below:脚本的最后一行如下所示:

value = 2
print(random.choice([value]))

which always prints 2. To get what you want, you need a list of all images.总是打印 2。要得到你想要的,你需要一个所有图像的列表。 Here is your code with a simple fix:这是您的代码,有一个简单的修复:

names = []
for img2 in images:
    names.append(img2.object_name)

print(random.choice(names))

These code can be made considerably shorter with list comprehension :使用列表推导可以大大缩短这些代码:

names = [img2.object_name for img2 in images]
print(random.choice([names]))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM