Saturday, February 1, 2014

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}