Python: set

setsPython 2.3で追加され,2.6で撤廃された.
2.6以降は組み込み型のsetを使う.

listやnumpy.arrayを使っても似たようなことができそうだけど,集合は集合ということでsetを使った方が良い.

import random

if __name__ == "__main__":
    # define a ground set
    V = set(range(10))
    S = set([])
    K = 3

    while(len(S) < K):
        i = random.sample(V.difference(S), 1)[0]
        S.add(i)
        print S
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s