简体   繁体   English

随机选择一组数据

[英]Random Choose set of data

I have three sets of data: 我有三组数据:

item1 = 'carrot'
property1 = 'vegetable'

item2 = 'apple'
property2 = 'fruit'

item3 = 'steak'
property3 = 'meat'

And so on... Now I want to combine these to sets of data, that should be randomly chosen. 依此类推...现在,我想将这些组合到数据集,应该随机选择。

combination1 = item1 + property1
combination2 = item2 + property2
combination3 = item3 + property3

So that I can use them randomly: 这样我就可以随机使用它们:

random.choice(combination1, combination2, combination3)

In the end the combination has to fit in here: 最后,组合必须符合以下条件:

Client(args.item, args.property)

Problem is, that always the set of item1+property1, item2+property2, aso. 问题是,总是将item1 + property1,item2 + property2,aso组合在一起。 has to be taken. 必须采取。 I can only manage to pick random combinations of items and properties, but don't know how to combine sets of them. 我只能设法随机选择项目和属性的组合,但不知道如何组合它们的集合。

Can anyone help? 有人可以帮忙吗?

import random

#   Instead of doing this...
item1 = 'carrot'
property1 = 'vegetable'

item2 = 'apple'
property2 = 'fruit'

item3 = 'steak'
property3 = 'meat'

#   You might want to do this, so you can do more general work on your data.
items = ['carrot', 'apple', 'steak']
properties = ['vegetable', 'fruit', 'meat']

#   Now if you want to choose one item from both list at random, you can do...
for _ in range(5):
    random_choice = (random.choice(items), random.choice(properties))
    print 'This is a random set of item and property : {}'.format(random_choice)

#   If you want to choose a random pair of pre-computed items, you could...
#   1.  Group your data.
combs = [(item, property) for item, property in zip(items, properties)]

#   2. Choose at random your combined data.
for _ in range(5):
    random_pair = random.choice(combs)
    print 'This is the random selected pair : {}'.format(random_pair)

Outputs : 输出:

# This is a random set of item and property : ('apple', 'fruit')
# This is a random set of item and property : ('apple', 'meat')
# This is a random set of item and property : ('steak', 'vegetable')
# This is a random set of item and property : ('apple', 'vegetable')
# This is a random set of item and property : ('apple', 'vegetable')
# This is the random selected pair : ('steak', 'meat')
# This is the random selected pair : ('steak', 'meat')
# This is the random selected pair : ('steak', 'meat')
# This is the random selected pair : ('apple', 'fruit')
# This is the random selected pair : ('apple', 'fruit')

You could define a class to hold your objects data and have a list of class objects like this. 您可以定义一个类来保存对象数据,并具有一个这样的类对象列表。 This way you could easily add extra fields to your object 这样,您可以轻松地向对象添加额外的字段

import random

class Food:

    def __init__(self, name, type):
        self.name = name
        self.type = type

foods = [Food('carrot', 'vegetable'), Food('apple', 'fruit'), Food('steak', 'meat')]

random_food = random.choice(foods)
print(random_food.name)
print(random_food.type)

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

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