In [1]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
from pandas import DataFrame, Series

In [2]:
sorted([7, 1, 2, 6, 0, 3, 2])


Out[2]:
[0, 1, 2, 2, 3, 6, 7]

In [3]:
sorted('horse race')


Out[3]:
[' ', 'a', 'c', 'e', 'e', 'h', 'o', 'r', 'r', 's']

In [4]:
sorted(set('this is just some string'))


Out[4]:
[' ', 'e', 'g', 'h', 'i', 'j', 'm', 'n', 'o', 'r', 's', 't', 'u']

In [5]:
seq1 = ['foo', 'bar', 'baz']
seq2 = ['one', 'two', 'three']
zip(seq1, seq2)


Out[5]:
[('foo', 'one'), ('bar', 'two'), ('baz', 'three')]

In [6]:
seq3 = [False, True]
zip(seq1, seq2, seq3)


Out[6]:
[('foo', 'one', False), ('bar', 'two', True)]

In [7]:
for i, (a, b) in enumerate(zip(seq1, seq2)):
    print( '%d: %s, %s' % (i, a, b))


0: foo, one
1: bar, two
2: baz, three

In [8]:
pitchers = [('Nolan', 'Ryan'), ('Roger', 'Clemens'),
           ('Schilling', 'Curt')]
first_names, last_names = zip(*pitchers)
first_names


Out[8]:
('Nolan', 'Roger', 'Schilling')

In [9]:
last_names


Out[9]:
('Ryan', 'Clemens', 'Curt')

In [10]:
zip(pitchers[0], pitchers[1], pitchers[2])


Out[10]:
[('Nolan', 'Roger', 'Schilling'), ('Ryan', 'Clemens', 'Curt')]

In [11]:
list(reversed(range(10)))


Out[11]:
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

In [12]:
empty_dict = {}
d1 = {'a' : 'some value', 'b' : [1, 2, 3, 4]}
d1


Out[12]:
{'a': 'some value', 'b': [1, 2, 3, 4]}

In [14]:
d1[7] = 'an integer'
d1


Out[14]:
{7: 'an integer', 'a': 'some value', 'b': [1, 2, 3, 4]}

In [15]:
d1['b']


Out[15]:
[1, 2, 3, 4]

In [16]:
'b' in d1


Out[16]:
True

In [18]:
d1[5] = 'some value'
d1


Out[18]:
{5: 'some value', 7: 'an integer', 'a': 'some value', 'b': [1, 2, 3, 4]}

In [19]:
del d1[5]

In [20]:
d1


Out[20]:
{7: 'an integer', 'a': 'some value', 'b': [1, 2, 3, 4]}

In [23]:
d1['dummy'] = 'another value'
ret = d1.pop('dummy')
ret


Out[23]:
'another value'

In [24]:
d1.keys()


Out[24]:
['a', 'b', 7]

In [25]:
d1.values()


Out[25]:
['some value', [1, 2, 3, 4], 'an integer']

In [26]:
d1.update({'b' : 'foo', 'c' : 12})
d1


Out[26]:
{7: 'an integer', 'a': 'some value', 'b': 'foo', 'c': 12}

In [28]:
mapping = {}
for key, value in zip(range(3), ['a', 'b', 'c']):
    mapping[key] = value
mapping


Out[28]:
{0: 'a', 1: 'b', 2: 'c'}

In [29]:
mapping = dict(zip(range(5), reversed(range(5))))
mapping


Out[29]:
{0: 4, 1: 3, 2: 2, 3: 1, 4: 0}

In [30]:
words = ['apple', 'bat', 'bar', 'atom', 'book']
by_letter = {}

for word in words:
    letter = word[0]
    if letter not in by_letter:
        by_letter[letter] = [word]
    else:
        by_letter[letter].append(word)
        
by_letter


Out[30]:
{'a': ['apple', 'atom'], 'b': ['bat', 'bar', 'book']}

In [33]:
by_letter = {}
by_letter.setdefault(letter, []).append(word)
by_letter


Out[33]:
{'b': ['book']}

In [34]:
from collections import defaultdict
by_letter = defaultdict(list)
for word in words:
    by_letter[word[0]].append(word)
by_letter


Out[34]:
defaultdict(list, {'a': ['apple', 'atom'], 'b': ['bat', 'bar', 'book']})

In [35]:
counts = defaultdict(lambda: 4)
counts


Out[35]:
defaultdict(<function __main__.<lambda>>, {})

In [36]:
hash('string')


Out[36]:
-1542666171

In [38]:
hash((1, 2, (2, 3)))


Out[38]:
1387206534

In [39]:
hash((1, 2, [2, 3]))


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-39-c7446717475e> in <module>()
----> 1 hash((1, 2, [2, 3]))

TypeError: unhashable type: 'list'

In [40]:
d = {}
d[tuple([1, 2, 3])] = 5
d


Out[40]:
{(1, 2, 3): 5}

In [41]:
set([2, 2, 2, 1, 3, 3])


Out[41]:
{1, 2, 3}

In [42]:
{2, 2, 2, 1, 3, 3}


Out[42]:
{1, 2, 3}

In [43]:
a = {1, 2, 3, 4, 5}
b = {3, 4, 5, 6, 7, 8}
a | b # 并(或)


Out[43]:
{1, 2, 3, 4, 5, 6, 7, 8}

In [44]:
a & b # 交(与)


Out[44]:
{3, 4, 5}

In [45]:
a - b # 差


Out[45]:
{1, 2}

In [46]:
a ^ b # 对称差(异或)


Out[46]:
{1, 2, 6, 7, 8}

In [47]:
a_set = {1, 2, 3, 4, 5}
{1, 2, 3}.issubset(a_set)


Out[47]:
True

In [48]:
a_set.issuperset({1, 2, 3})


Out[48]:
True

In [49]:
{1, 2, 3} == {3, 2, 1}


Out[49]:
True

In [51]:
{(1), 2, 3} == {2, 3, 1}


Out[51]:
True

In [52]:
1 == (1)


Out[52]:
True

In [53]:
strings = ['a', 'as', 'bat', 'car', 'dove', 'python']
[x.upper() for x in strings if len(x) > 2]


Out[53]:
['BAT', 'CAR', 'DOVE', 'PYTHON']

In [55]:
unique_lengths = {len(x) for x in strings}
unique_lengths


Out[55]:
{1, 2, 3, 4, 6}

In [56]:
loc_mapping = {val : index for index, val in enumerate(strings)}
loc_mapping


Out[56]:
{'a': 0, 'as': 1, 'bat': 2, 'car': 3, 'dove': 4, 'python': 5}

In [57]:
loc_mapping = dict((val, idx) for idx, val in enumerate(strings))
loc_mapping


Out[57]:
{'a': 0, 'as': 1, 'bat': 2, 'car': 3, 'dove': 4, 'python': 5}

In [59]:
all_data = [['Tom', 'Billy', 'Jefferson', 'Andrew', 'Wesley', 'Steven', 'Joe'],
           ['Susie', 'Casey', 'Jill', 'Ana', 'Eva', 'Jennifer', 'Stephanie']]
result = [name for names in all_data for name in names
         if name.count('e') >= 2]
result


Out[59]:
['Jefferson', 'Wesley', 'Steven', 'Jennifer', 'Stephanie']

In [60]:
some_tuples = [(1, 2, 3), (4, 5, 6), (7, 8, 9)]
flattened = [x for tup in some_tuples for x in tup]
flattened


Out[60]:
[1, 2, 3, 4, 5, 6, 7, 8, 9]

In [61]:
[[x for x in tup] for tup in some_tuples]


Out[61]:
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

In [62]:
def func():
    a = []
    for i in range(5):
        a.append(i)
    return a

In [63]:
func()


Out[63]:
[0, 1, 2, 3, 4]

In [65]:
a = []
def func():
    for i in range(5):
        a.append(i)
func()
a


Out[65]:
[0, 1, 2, 3, 4]

In [66]:
a = None

def bind_a_variable():
    global a
    a = []
bind_a_variable()
print a


[]

In [67]:
def outer_function(x, y, z):
    def inner_function(a, b, c):
        pass
    pass

In [68]:
def f():
    a = 5
    b = 6
    c = 7
    return a, b, c
a, b, c = f()
a, b, c


Out[68]:
(5, 6, 7)

In [69]:
def f():
    a = 5
    b = 6
    c = 7
    return {'a' : a, 'b' : b, 'c' : c}

f()


Out[69]:
{'a': 5, 'b': 6, 'c': 7}

In [73]:
import re # 正则表达式模块
states = [' Alabama ', 'Georial!', 'Georigia', 'georgia', 'FlOrIda',
         'south carolina##', 'West virgini?']
def remove_punctuation(value):
    return re.sub('[!#?]', '', value)

clean_ops = [str.strip, remove_punctuation, str.title]

def clean_strings(strings, ops):
    result = []
    for value in strings:
        for function in ops:
            value = function(value)
        result.append(value)
    return result

clean_strings(states, clean_ops)


Out[73]:
['Alabama',
 'Georial',
 'Georigia',
 'Georgia',
 'Florida',
 'South Carolina',
 'West Virgini']

In [74]:
map(remove_punctuation, states)


Out[74]:
[' Alabama ',
 'Georial',
 'Georigia',
 'georgia',
 'FlOrIda',
 'south carolina',
 'West virgini']

In [75]:
def apply_to_list(some_list, f):
    return [f(x) for x in some_list]

ints = [4, 0, 1, 5, 6]
apply_to_list(ints, lambda x: x * 2)


Out[75]:
[8, 0, 2, 10, 12]

In [76]:
[x * 2 for x in ints]


Out[76]:
[8, 0, 2, 10, 12]

In [77]:
strings = ['foo', 'card', 'bar', 'aaaa', 'abab']
strings.sort(key=lambda x: len(set(list(x))))
strings


Out[77]:
['aaaa', 'foo', 'abab', 'bar', 'card']

In [81]:
def make_closure(a):
    def closure():
        print('I know the secret: %d' % a)
    return closure

make_closure(5)()


I know the secret: 5

In [ ]:


In [ ]:


In [ ]: