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]:
In [3]:
sorted('horse race')
Out[3]:
In [4]:
sorted(set('this is just some string'))
Out[4]:
In [5]:
seq1 = ['foo', 'bar', 'baz']
seq2 = ['one', 'two', 'three']
zip(seq1, seq2)
Out[5]:
In [6]:
seq3 = [False, True]
zip(seq1, seq2, seq3)
Out[6]:
In [7]:
for i, (a, b) in enumerate(zip(seq1, seq2)):
print( '%d: %s, %s' % (i, a, b))
In [8]:
pitchers = [('Nolan', 'Ryan'), ('Roger', 'Clemens'),
('Schilling', 'Curt')]
first_names, last_names = zip(*pitchers)
first_names
Out[8]:
In [9]:
last_names
Out[9]:
In [10]:
zip(pitchers[0], pitchers[1], pitchers[2])
Out[10]:
In [11]:
list(reversed(range(10)))
Out[11]:
In [12]:
empty_dict = {}
d1 = {'a' : 'some value', 'b' : [1, 2, 3, 4]}
d1
Out[12]:
In [14]:
d1[7] = 'an integer'
d1
Out[14]:
In [15]:
d1['b']
Out[15]:
In [16]:
'b' in d1
Out[16]:
In [18]:
d1[5] = 'some value'
d1
Out[18]:
In [19]:
del d1[5]
In [20]:
d1
Out[20]:
In [23]:
d1['dummy'] = 'another value'
ret = d1.pop('dummy')
ret
Out[23]:
In [24]:
d1.keys()
Out[24]:
In [25]:
d1.values()
Out[25]:
In [26]:
d1.update({'b' : 'foo', 'c' : 12})
d1
Out[26]:
In [28]:
mapping = {}
for key, value in zip(range(3), ['a', 'b', 'c']):
mapping[key] = value
mapping
Out[28]:
In [29]:
mapping = dict(zip(range(5), reversed(range(5))))
mapping
Out[29]:
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]:
In [33]:
by_letter = {}
by_letter.setdefault(letter, []).append(word)
by_letter
Out[33]:
In [34]:
from collections import defaultdict
by_letter = defaultdict(list)
for word in words:
by_letter[word[0]].append(word)
by_letter
Out[34]:
In [35]:
counts = defaultdict(lambda: 4)
counts
Out[35]:
In [36]:
hash('string')
Out[36]:
In [38]:
hash((1, 2, (2, 3)))
Out[38]:
In [39]:
hash((1, 2, [2, 3]))
In [40]:
d = {}
d[tuple([1, 2, 3])] = 5
d
Out[40]:
In [41]:
set([2, 2, 2, 1, 3, 3])
Out[41]:
In [42]:
{2, 2, 2, 1, 3, 3}
Out[42]:
In [43]:
a = {1, 2, 3, 4, 5}
b = {3, 4, 5, 6, 7, 8}
a | b # 并(或)
Out[43]:
In [44]:
a & b # 交(与)
Out[44]:
In [45]:
a - b # 差
Out[45]:
In [46]:
a ^ b # 对称差(异或)
Out[46]:
In [47]:
a_set = {1, 2, 3, 4, 5}
{1, 2, 3}.issubset(a_set)
Out[47]:
In [48]:
a_set.issuperset({1, 2, 3})
Out[48]:
In [49]:
{1, 2, 3} == {3, 2, 1}
Out[49]:
In [51]:
{(1), 2, 3} == {2, 3, 1}
Out[51]:
In [52]:
1 == (1)
Out[52]:
In [53]:
strings = ['a', 'as', 'bat', 'car', 'dove', 'python']
[x.upper() for x in strings if len(x) > 2]
Out[53]:
In [55]:
unique_lengths = {len(x) for x in strings}
unique_lengths
Out[55]:
In [56]:
loc_mapping = {val : index for index, val in enumerate(strings)}
loc_mapping
Out[56]:
In [57]:
loc_mapping = dict((val, idx) for idx, val in enumerate(strings))
loc_mapping
Out[57]:
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]:
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]:
In [61]:
[[x for x in tup] for tup in some_tuples]
Out[61]:
In [62]:
def func():
a = []
for i in range(5):
a.append(i)
return a
In [63]:
func()
Out[63]:
In [65]:
a = []
def func():
for i in range(5):
a.append(i)
func()
a
Out[65]:
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]:
In [69]:
def f():
a = 5
b = 6
c = 7
return {'a' : a, 'b' : b, 'c' : c}
f()
Out[69]:
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]:
In [74]:
map(remove_punctuation, states)
Out[74]:
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]:
In [76]:
[x * 2 for x in ints]
Out[76]:
In [77]:
strings = ['foo', 'card', 'bar', 'aaaa', 'abab']
strings.sort(key=lambda x: len(set(list(x))))
strings
Out[77]:
In [81]:
def make_closure(a):
def closure():
print('I know the secret: %d' % a)
return closure
make_closure(5)()
In [ ]:
In [ ]:
In [ ]: