You are reading a single comment by @Drakien and its replies. Click here to read the full conversation.
  • How come you used a dictionary with all values of 1, rather than a list or a set?

    Because searching through a list is just like a for loop, it needs to check each item in turn until it finds the item or runs out of items to check.

    A python dict is based on a hash table, lookups are almost immediate and lookup performance is consistent regardless of the number of items stored in the dict (assuming you've got a sensible dict implementation). A dict takes a little longer to setup and insert values (and consumes a bit more memory) but that's a small price to pay for the massively faster lookup performance.

  • Ah ok, I thought a set was just a special case of a dict where there are no values, only keys.

    Yes but none of the above examples were using a set. (I've ninja edited the [and sets] out of my previous reply.)

    $ cat z.py
    expense_list = [int(line) for line in open('1.inp', 'r')]
    
    print type(expense_list)
    $ python z.py
    <type 'list'>
    

    And I didn't use a set because I don't really do python and don't know how to instantiate one reliably. Most of my programming is in C where there are no such fancy datatypes, or perl (for hacky prototypes), and perl only has the equivalents of scalars, lists and dicts.

    Ah.

    $ cat z.py
    expense_list = {int(line) for line in open('1.inp', 'r')}
    
    print type(expense_list)
    $ python z.py
    <type 'set'>
    
About

Avatar for Drakien @Drakien started