Select an item from a set in Python

24,577

Solution 1

What about converting to list and sorting?

my_list = list(my_set)
my_list.sort()
chosen_element = my_list[0]

Solution 2

you could use a function with memoization

def get_random(my_set,memo={}):
    if id(my_set) not in memo:
       memo[id(my_set)] = random.choice(list(my_set))
    return memo[id(my_set)]

a_set = set([1,2,3,4,5])
print get_random(a_set)
print get_random(a_set)

this would always give you the same value as long as you passed in a_set ... (a different set would give a different answer)

if you wanted to make sure the item was still in the set you could change the memo if check

def get_random(my_set,memo={}):
    if id(my_set) not in memo or memo[id(my_set)] not in my_set:
       memo[id(my_set)] = random.choice(list(my_set))
    return memo[id(my_set)]
Share:
24,577
kyrenia
Author by

kyrenia

CEO and founder of Menai Insight Menai Insight helps management and governance researchers characterize organizational communications, including managerial backgrounds, committee activities, and performance evaluations

Updated on December 18, 2021

Comments

  • kyrenia
    kyrenia over 2 years

    I am looking to select one item from a set. It does not matter which item it is, but I want it to be the same item every time the function is called.

    For example, if I had the set:

    my_set = set(["ABC","ABC inc", "ABC ltd"])
    

    Then I want to return any one of those, but the same one whenever the function is run. [i.e. so I would always get "ABC", or always get "ABC inc" whenever I ran with the above set.] If I was using lists, I would just take the first item, but sets don't have a first per se.

    This is in contrast to selecting randomly [e.g. How do I pick 2 random items from a Python set? ] where it will change every time it is run.

  • Alan
    Alan over 8 years
    Dangerous in so many ways. E.g., remove the memoized item from the set, and it will still show up as the answer. (You'll also need to make a list of my_set before applying random.choice.)
  • Joran Beasley
    Joran Beasley over 8 years
    meh its what OP asked for ... you could always do something like or memo[id(my_set)] not in my_set
  • Alan
    Alan over 8 years
    Never count on a set iterating through its elements in a certain order.
  • hackbot89
    hackbot89 over 8 years
    I think you should be good as long as you are using pop to pull the elements out, because after a pop the order still remains the same. Do you have an example that explains why I should "Never count ... " ? : )
  • Alex Huszagh
    Alex Huszagh over 8 years
    You can combine this into a single step: sorted(my_set) returns a list in both Python2 and Python3.
  • kyrenia
    kyrenia over 8 years
    ... I very quickly ran into issues using this [e.g. when re-initialized the set with new value, the prior result was returned]
  • Joran Beasley
    Joran Beasley over 8 years
    if the memory location is the same then its the same set and it returns to you the first value it returned ... ... which is what was requested ... anyway I altered it to check for the items existance still in the set ...
  • Alex Huszagh
    Alex Huszagh over 8 years
    If memory is also an issue and you don't want to duplicate the set in memory, you could grab the first value from an iterator rather than random.choice(list(my_set)), which is an advantage of this approach rather than generating a list.
  • Alan
    Alan over 8 years