[ create a new paste ] login | about

Link: http://codepad.org/AjmZyyyQ    [ raw code | output | fork ]

Python, pasted on Jul 9:
#!/usr/bin/env python
#-*- coding:utf8 -*-
import random

def random_choice_unique(lists, num=4):
    if not lists:
        return []

    if len(lists) <= num:
        return lists

    return random.sample(lists, num)

l = ['1', '2', '3', '4', '5']
print random_choice_unique(l)

l = [1, 2, 3, 4, 5]
print random_choice_unique(l)

l = [[0, 1], [1, 2], [3, 4], [5, 6], [7, 8]]
print random_choice_unique(l)

l = [{'ae': 35}, {'shin': 30}, {'haru': 860}, {'key': 3}, {'easy': 1126}]
print random_choice_unique(l)

class BuchoLover():

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

    def __repr__(self):
        return self.name + " is a bucho lover"

l = [BuchoLover(name) for name in ['ae35', 'shin', 'm_432', 'haru', 'feiz']]
print random_choice_unique(l)


Output:
1
2
3
4
5
['5', '4', '1', '2']
[2, 3, 4, 5]
[[3, 4], [5, 6], [7, 8], [0, 1]]
[{'ae': 35}, {'easy': 1126}, {'haru': 860}, {'key': 3}]
[ae35 is a bucho lover, shin is a bucho lover, m_432 is a bucho lover, feiz is a bucho lover]


Create a new paste based on this one


Comments: