Saturday, February 1, 2014

Python3 set operations

how-to-subtraction-two-list-in-python
Python has a built-in datatype for an unordered collection of (hashable) things, called a set. If you convert both lists to sets, the comparison will be unordered.
set(x) == set(y)
Documentation on set

EDIT: @mdwhatcott points out that you want to check for duplicates. set ignores these, so you need a similar data structure that also keeps track of the number of items in each list. This is called a multiset; the best approximation in the standard library is a collections.Counter:
>>> import collections
>>> compare = lambda x, y: collections.Counter(x) == collections.Counter(y)
>>> 
>>> compare([1,2,3], [1,2,3,3])
False
>>> compare([1,2,3], [1,2,3])
True
>>> compare([1,2,3,3], [1,2,2,3])
False
>>> 

best-way-to-check-if-a-list-is-empty
 
if not a:
  print "List is empty"
Using the implicit booleanness of the empty list is quite pythonic.
 

Python3 command line help

use help(dict) on the Python command line and replace dict with the troublesome object to see a list of its helper methods etc., e.g. items(...) D.items() -> list of D's (key, value) pairs, as 2-tuples


>>> help(list)

Help on class list in module builtins:

class list(object)
 |  list() -> new empty list
 |  list(iterable) -> new list initialized from iterable's items
 | 
 |  Methods defined here:
 | 
 |  __add__(...)
 |      x.__add__(y) <==> x+y
 | 
 |  __contains__(...)
 |      x.__contains__(y) <==> y in x
 | 
 |  __delitem__(...)
 |      x.__delitem__(y) <==> del x[y]
 | 
 |  __eq__(...)
 |      x.__eq__(y) <==> x==y
 | 
 |  __ge__(...)
 |      x.__ge__(y) <==> x>=y
 | 
 |  __getattribute__(...)
 |      x.__getattribute__('name') <==> x.name
 | 
 |  __getitem__(...)
 |      x.__getitem__(y) <==> x[y]
 | 
 |  __gt__(...)
 |      x.__gt__(y) <==> x>y
 | 
 |  __iadd__(...)
 |      x.__iadd__(y) <==> x+=y
 | 
 |  __imul__(...)
 |      x.__imul__(y) <==> x*=y
 | 
 |  __init__(...)
 |      x.__init__(...) initializes x; see help(type(x)) for signature
 | 
 |  __iter__(...)
 |      x.__iter__() <==> iter(x)
 | 
 |  __le__(...)
 |      x.__le__(y) <==> x<=y
 | 
 |  __len__(...)
 |      x.__len__() <==> len(x)
 | 
:

Python3 list operations

how-to-subtraction-two-list-in-python

>>> List1=[3,5,6]
>>> List2=[3,7,2]
>>> [x1 - x2 for (x1, x2) in zip(List1, List2)]
[0, -2, 4]

Python sets
>>> a = [1,2,3,4]
>>> b = [3,4,5,6]
>>> set(a)
{1, 2, 3, 4}
>>> (set(b)
{3, 4, 5, 6}
>>> sa= set(a)
>>> sb= set(b)
>>> sa-sb
{1, 2}
>>> sb-sa
{5, 6}
>>> sa.intersection(sb)
{3, 4}
>>> sa.intersection(sb)
{3, 4}
>>> 3 in sa
True
>>> 10 in sa
False
>>> len(sa)
4
>>> sa.issubset(sb)
False
>>> sa.union(sb)
{1, 2, 3, 4, 5, 6}
>>> sa.difference(sb)
{1, 2}
>>> sb.difference(sa)
{5, 6}
>>> set(a) - set(b)
{1, 2}
>>> set(b) - set(a)
{5, 6}
>>> set(a) - set(a)
set()
>>> sa.symmetric_difference(sb)
{1, 2, 5, 6}
>>> sb.symmetric_difference(sa)
{1, 2, 5, 6}

differences-between-isinstance-and-type-in-python

python: using dictionaries instead of case statements

Switch Statements in Python

response, data = somefunc()
if response == "this":
    do_this_with(data)
elif response == "that":
    do_that_with(data)
elif response == "huh":
    duh(data)
# lots more elifs.
else:
    prevent_horrible_crash(data)


Replace with 

maps-as-switch.py

response_map = {"this": do_this_with,
                "that": do_that_with,
                "huh": duh}
response_map.get(response, prevent_horrible_crash)(data)