idiosynchronous lists:

28 January 2018

more weirdness

i was playing with some of the code in the numpy tutorial in CS231n Convolutional Neural Networks for Visual Recognition.

there’s a part where we iterate through various squares and print the output:

nums = list(range(40))
squares = []
for x in nums:
    squares.append(x ** 2)
print(squares)   # Prints [0, 1, 4, 9, 16]


squares = [x ** 2 for x in nums]
print(squares)   # Prints [0, 1, 4, 9, 16]


even_squares = [x ** 2 for x in nums if x % 2 == 0]
print(even_squares)  # Prints "[0, 4, 16]"


even_num_to_square = {x: x ** 2 for x in nums if x % 2 == 0}
print(even_num_to_square)  # Prints "{0: 0, 2: 4, 4: 16}"

look at what happens in the last output of the last function:

why do the orders wrap around like that??

[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441, 484, 529, 576, 625, 676, 729, 784, 841, 900, 961, 1024, 1089, 1156, 1225, 1296, 1369, 1444, 1521]
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441, 484, 529, 576, 625, 676, 729, 784, 841, 900, 961, 1024, 1089, 1156, 1225, 1296, 1369, 1444, 1521]
[0, 4, 16, 36, 64, 100, 144, 196, 256, 324, 400, 484, 576, 676, 784, 900, 1024, 1156, 1296, 1444]
{0: 0, 32: 1024, 2: 4, 4: 16, 38: 1444, 6: 36, 8: 64, 10: 100, 12: 144, 34: 1156, 14: 196, 16: 256, 18: 324, 20: 400, 22: 484, 24: 576, 36: 1296, 26: 676, 28: 784, 30: 900}
[Finished in 0.9s]

the 32nd entry wraps back around to the beginning. ie: [ index 0: 0, index 1: 32, index 2: 2 ]

gasp

what is the size of python’s list struct??????????????????????