by 1024core · 8y ago · 0 comments
For example: I'm collecting some stats indexed by (small) ints. In Perl, I'd just say
$collection[$index] = $stat;
Try that in Python when $index is not received in order.Edit: Fixed typo; meant "$stat" and not "%stat"
dict[index] = stat
...works much the same. >>> foo = {}
>>> foo[3] = 72
>>> foo[64] = 80
>>> foo
{3: 72, 64: 80} >>> foo = {}
>>> foo[3][4] = 42
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 3
Compared to: DB<1> $foo{3}{4} = 42
DB<2> x \%foo
0 HASH(0x7f9f4315c940)
3 => HASH(0x7f9f432bb148)
4 => 42 \%stat;