Toot

Written by Fabian N. T. on 2024-11-01 at 21:40

How “truly random” is set.pop() in #Python. The docs say: »Remove […] an arbitrary element from the set.«

How arbitrary is arbitrary? I guess the elements are arranged in the set’s tree structure by their hash(), so it all depends on that hash function, right?

I want to randomly remove half of the entries of a set.

[#] This seems more correct 🧑‍🏫
s = {el for el in s if getrandbits(1)}

[#] But this is a mu faster 🥺
for _ in range(len(s) // 2):
    s.pop()

=> More informations about this toot | View the thread | More toots from fabian@floss.social

Mentions

Tags

=> View python tag

Proxy Information
Original URL
gemini://mastogem.picasoft.net/toot/113409864766759936
Status Code
Success (20)
Meta
text/gemini
Capsule Response Time
223.212409 milliseconds
Gemini-to-HTML Time
0.521892 milliseconds

This content has been proxied by September (ba2dc).