From the python docs, "set.pop() remove and return an arbitrary element from s". While generating some random data to test a program, I noticed strange behavior of this pop() function. Here is my code (python 2.7.3):
testCases = 10
numberRange = 500
poppedValues = []
greaterPercentages = []
for i in range (testCases):
s = Set()
""" inserting 100 random values in the set, in the range [0, numberRange) """
for j in range (100):
s.add(random.randrange(numberRange))
poppedValue = s.pop()
greaterCount = 0
""" counting how many numbers in the set are smaller then the popped value """
for number in s:
if poppedValue > number:
greaterCount += 1
poppedValues.append(poppedValue)
greaterPercentages.append(float(greaterCount) / len(s) * 100)
for poppedValue in poppedValues:
print poppedValue, '',
print
for percentage in greaterPercentages:
print "{:2.2f}".format(percentage), '',
What I'm doing here is,
- Inserting some random values in the set
s
where each element is in the range [0, numberRange
)
- Pop an element from the set (according to the docs, it should be a random one)
- Counting how many elements in the set are smaller then the popped value
I expected that the popped value should be a random one and about 50% of the numbers in the set will be greater then the popped value. But seems that pop()
almost always returns the lowest number in the set. Here are the result for numberRange = 500
. First row denotes the values of the popped element. Second row is the percentage of elements which are smaller then the popped value.
9 0 3 1 409 0 1 2 4 0
0 % 0 % 0 % 0 % 87 % 0 % 0 % 0 % 0 % 0 %
I've conducted this test with different values of numberRange
. It seems that for lower values of the set elements, pop()
almost always returns the lowest element. But for higher values it returns a random element. For numberRange = 1000
, the result is:
518 3586 3594 4103 2560 3087 4095 3079 3076 1622
7 % 72 % 73 % 84 % 54 % 51 % 79 % 63 % 67 % 32 %
which I think is pretty random. Why this strange behavior? Am I doing something wrong?
EDIT: Thanks for everyone's answer and comment, seems that by "arbitrarily", it isn't guaranteed that it will be "random".
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…