Re:Show HN: Refurb – A tool for refurbishing and modernizing...
by cdrini on Wednesday September 28, 2022 @11:31AM
(#33009131)
Fun fact: not only is the iteration order of sets in python unordered, it's also undeterministic! A set of the same values, iterated in two python program, could iterate in a different order.
I learned this when a unit test that iterated over a set started failing sporadically :P super confusing bug!
Re:Show HN: Refurb – A tool for refurbishing and modernizing...
by olejorgenb on Wednesday September 28, 2022 @01:21PM
(#33010548)
I think this is just for sets whos items hash derive from `hash(some_string)` (eg.: sets of strings)
This is because the string hash function is randomized on process startup to help prevent DOS attacks: https://stackoverflow.com/q/30585108/1517969
Re:Show HN: Refurb – A tool for refurbishing and modernizing...
by dosisod on Wednesday September 28, 2022 @02:08AM
(#33004737)
You're right! I guess you can't rely on sets being iterated over in order (not that you ever should rely on that). Moreover I was trying to show that you can use lists, tuples, and sets as temporary iterable objects, though but no-one (that I have seen) uses sets in that way.
Re:Show HN: Refurb – A tool for refurbishing and modernizing...
by yunohn on Wednesday September 28, 2022 @03:17AM
(#33005106)
Sets are more useful when you want to dedupe unknown data. Defining a set with constants to use immediately in a for loop is not very helpful compared to a tuple or a list.
Also, you can make anything iterable in python if you implement __iter__.
Re:Show HN: Refurb – A tool for refurbishing and modernizing...
by BeetleB on Wednesday September 28, 2022 @11:00AM
(#33008774)
Set insertion is O(1). This isn't C++.
Re:Show HN: Refurb – A tool for refurbishing and modernizing...
by minhazm on Wednesday September 28, 2022 @01:26PM
(#33010621)
I didn't claim it wasn't O(1). I just said that for sets to automatically be sorted like the parent comment claimed, it would have to be O(logn) insertion since that's the fastest you can insert into a sorted data structure. Obviously sets are not sorted, and thus are able to maintain O(1) insertion.
>>> [i for i in {9,40,49}] [40, 9, 49]