简体   繁体   中英

generate categorical dataset in python

Suppose I have a list of ten cities :

["paris", "barcelona", "kolkata", "new york"]

How can I simulate n random datasets of two attributes each having 2 cities selected from the above list:

["paris", "barcelona"]
["barcelona", "kolata"]
["new york", "paris"]
["paris", "barcelona"]
["new york", "paris"]

You can use random.sample like this

data = ["paris", "barcelona", "kolkata", "new york"]
import random
print [random.sample(data, 2) for _ in xrange(5)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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