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]:
In [2]:
left_l = left
Out[2]:
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]:
In [2]:
result = pd.merge(left, right, on=['key1', 'key2'])
result
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')))
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()
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]:
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)
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]:
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()