In [1]:
import pandas as pd
left = pd.DataFrame({'key1': ['K0', 'K0', 'K1', 'K2'],
                     'key2': ['K0', 'K1', 'K0', 'K1'],
                     'A': ['A0', 'A0', 'A2', 'A2'],
                     'B': ['B0', 'B0', 'B2', 'B3']})

left


Out[1]:
A B key1 key2
0 A0 B0 K0 K0
1 A0 B0 K0 K1
2 A2 B2 K1 K0
3 A2 B3 K2 K1

In [2]:
left_l = left


Out[2]:
A B key1 key2
0 A0 B0 K0 K0
1 A0 B0 K0 K1
2 A2 B2 K1 K0
3 A2 B3 K2 K1

In [14]:
right = pd.DataFrame({
    'key1': ['K0', 'K1', 'K1', 'K2'],
    'key2': ['K0', 'K0', 'K0', 'K0'],
    'C': ['C0', 'C1', 'C2', 'C3'],
    'D': ['D0', 'D1', 'D2', 'D3']})
right


Out[14]:
C D key1 key2
0 C0 D0 K0 K0
1 C1 D1 K1 K0
2 C2 D2 K1 K0
3 C3 D3 K2 K0

In [2]:
result = pd.merge(left, right, on=['key1', 'key2'])
result


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-2-6d992c0ba5c0> in <module>()
----> 1 result = pd.merge(left, right, on=['key1', 'key2'])
      2 result

NameError: name 'pd' is not defined

In [84]:
import networkx as nx
import matplotlib.pyplot as plt

G = nx.DiGraph()
e = [('a', 'b', 0.3), ('b', 'c', 0.9), ('a', 'c', 0.5), ('c', 'd', 1.2)]
G.add_weighted_edges_from(e)
print(list(nx.all_simple_paths(G, 'a', 'd')))


[['a', 'b', 'c', 'd'], ['a', 'c', 'd']]

In [42]:
import matplotlib.pyplot as plt
import networkx as nx

G = nx.krackhardt_kite_graph()

print("Betweenness")
b = nx.betweenness_centrality(G)
for v in G.nodes():
    print("%0.2d %5.3f" % (v, b[v]))

print("Degree centrality")
d = nx.degree_centrality(G)
for v in G.nodes():
    print("%0.2d %5.3f" % (v, d[v]))

print("Closeness centrality")
c = nx.closeness_centrality(G)
for v in G.nodes():
    print("%0.2d %5.3f" % (v, c[v]))

# nx.draw(G)
# plt.show()


Betweenness
00 0.023
01 0.023
02 0.000
03 0.102
04 0.000
05 0.231
06 0.231
07 0.389
08 0.222
09 0.000
Degree centrality
00 0.444
01 0.444
02 0.333
03 0.667
04 0.333
05 0.556
06 0.556
07 0.333
08 0.222
09 0.111
Closeness centrality
00 0.529
01 0.529
02 0.500
03 0.600
04 0.500
05 0.643
06 0.643
07 0.600
08 0.429
09 0.310

In [25]:
import heapq
nums = [1, 8, 2, 23, 7, -4, 18, 23, 42, 37, 2]


portfolio = [
    {'name': 'IBM', 'shares': 100, 'price': 91.1},
    {'name': 'AAPL', 'shares': 50, 'price': 543.22},
    {'name': 'FB', 'shares': 200, 'price': 21.09},
    {'name': 'HPQ', 'shares': 35, 'price': 31.75},
    {'name': 'YHOO', 'shares': 45, 'price': 16.35},
    {'name': 'ACME', 'shares': 75, 'price': 115.65}
]
cheap = heapq.nsmallest(3, portfolio, key=lambda s: s['price'])
expensive = heapq.nlargest(3, portfolio, key=lambda s: s['price'])

he = list(nums)
heapq.heapify(he)

In [1]:
rows = [
    {'fname': 'Brian', 'lname': 'Jones', 'uid': 1003},
    {'fname': 'David', 'lname': 'Beazley', 'uid': 1002},
    {'fname': 'John', 'lname': 'Cleese', 'uid': 1001},
    {'fname': 'Big', 'lname': 'Jones', 'uid': 1004}
]

from operator import itemgetter

min(rows, key=itemgetter('uid', 'fname'))
rows.sort(key=itemgetter('uid'))
rows


Out[1]:
[{'fname': 'John', 'lname': 'Cleese', 'uid': 1001},
 {'fname': 'David', 'lname': 'Beazley', 'uid': 1002},
 {'fname': 'Brian', 'lname': 'Jones', 'uid': 1003},
 {'fname': 'Big', 'lname': 'Jones', 'uid': 1004}]

In [41]:
from itertools import groupby
rows_group = [
    {'address': '5412 N CLARK', 'date': '07/01/2012'},
    {'address': '5148 N CLARK', 'date': '07/04/2012'},
    {'address': '5800 E 58TH', 'date': '07/02/2012'},
    {'address': '2122 N CLARK', 'date': '07/03/2012'},
    {'address': '5645 N RAVENSWOOD', 'date': '07/02/2012'},
    {'address': '1060 W ADDISON', 'date': '07/02/2012'},
    {'address': '4801 N BROADWAY', 'date': '07/01/2012'},
    {'address': '1039 W GRANVILLE', 'date': '07/04/2012'},
]

In [47]:
rows_group.sort(key=itemgetter('date'))

for date, item in groupby(rows_group, key=itemgetter('date')):
    print(date)
    for i in item:
        print(i)


07/01/2012
{'address': '5412 N CLARK', 'date': '07/01/2012'}
{'address': '4801 N BROADWAY', 'date': '07/01/2012'}
07/02/2012
{'address': '5800 E 58TH', 'date': '07/02/2012'}
{'address': '5645 N RAVENSWOOD', 'date': '07/02/2012'}
{'address': '1060 W ADDISON', 'date': '07/02/2012'}
07/03/2012
{'address': '2122 N CLARK', 'date': '07/03/2012'}
07/04/2012
{'address': '5148 N CLARK', 'date': '07/04/2012'}
{'address': '1039 W GRANVILLE', 'date': '07/04/2012'}

In [50]:
from functools import reduce
small_dic = dict(c5_=["u1_", "u2_", "u3_", "u4_"],
                         c6_=["u1_", "u2_", "u3_", "u4_"],
                         c7_=["u1_", "u2_", "u3_", "u4_"],
                         c8_=["u1_", "u2_", "u3_", "u4_"],
                         c9_=["u5_", "u6_", "u7_", "u8_"],
                         c10=["u5_", "u6_", "u7_", "u8_"],
                         c11=["u5_", "u6_", "u7_", "u8_"],
                         c12=["u5_", "u6_", "u7_", "u8_"])
sorted(list(reduce(lambda x, y: set(x) | set(y), small_dic.values())))


Out[50]:
['u1_', 'u2_', 'u3_', 'u4_', 'u5_', 'u6_', 'u7_', 'u8_']

In [79]:
class A(object):
    def __init__(self):
        # 私有变量
        self._name = 'A'
    
    def print_self(self, name: str=None):
        try:
            if name is None:
                print("Don't Set Name Yet!!")
            else:
                print(name)
        except TypeError:
            pass

        
class B(A):
    def __init__(self):
        self.name = 'B'
        
    def print_self(self):
        super().print_self(self.name)
        print('B print func!')

In [80]:
a = B()
a.print_self()


B
B print func!