def test_count(): slt = SortedList(load=7) assert slt.count(0) == 0 for iii in range(100): for jjj in range(iii): slt.add(iii) slt._check() for iii in range(100): assert slt.count(iii) == iii
def test_count(): slt = SortedList(load=7) assert slt.count(0) == 0 for iii in range(100): for jjj in range(iii): slt.add(iii) slt._check() for iii in range(100): assert slt.count(iii) == iii
def save_remove_n_edges(edges: pandas.DataFrame, n): """ removes n edges from 'edges' so that no node is removed in the process, i.e. the total number of nodes in 'edges' stays the same :param edges: original edges :param n: number of how many edges should be removed :return: """ if n < 1: return edges all_edges = SortedList( list(edges[globConst.NODE1_ID_COL_NAME].append( edges[globConst.NODE2_ID_COL_NAME]))) edges_list = set(all_edges) edges_count_dict = {x: all_edges.count(x) for x in edges_list} i = 0 for _ in range(1000): drop_indices_candidates = random.sample( edges.index.values.tolist(), n) drop_indices = [] for drop_index in tqdm(drop_indices_candidates): if i == n: break drop_edge_candidate = edges.loc[drop_index] if edges_count_dict[drop_edge_candidate[globConst.NODE1_ID_COL_NAME]]>1 \ and edges_count_dict[drop_edge_candidate[globConst.NODE2_ID_COL_NAME]]>1: drop_indices.append(drop_index) i += 1 edges.drop(inplace=True, index=drop_indices) if i == n: break edges.reset_index(drop=True, inplace=True) return edges
def _test5(): """ 网址:http://www.grantjenks.com/docs/sortedcontainers/sortedlist.html """ from sortedcontainers import SortedList # 定义 sl = SortedList(key=lambda x: -x) # 降序 sl = SortedList([3, 1, 2, 1, 5, 4]) # 升序 print(sl) # SortedList([1, 1, 2, 3, 4, 5]) # 插入、删除元素 sl.add(3) sl.add(3) sl.discard(2) # SortedList([1, 1, 3, 3, 3, 4, 5]) print(sl) # 统计某个元素出现的次数 print(sl.count(3)) # 3 # 返回第一个和最后一个元素 print(sl[0]) # 1 print(sl[-1]) # 5 # 遍历 set for e in sl: print(e, end=", ") # 1, 1, 3, 3, 3, 4, 5, print() # 判断某元素是否存在 print(2 in sl) # False # bisect_left() / bisect_right() print(sl.bisect_left(3)) # 返回大于等于3的最小元素对应的下标 2 print(sl.bisect_right(3)) # 返回大于3的最小元素对应的下标 5 # 清空 sl.clear() print(len(sl)) # 0 print(len(sl) == 0) # True
def save_remove_n_edges(edges: pandas.DataFrame, n): """ removes n edges from 'edges' so that no node is removed in the process, i.e. the total number of nodes in 'edges' stays the same :param edges: original edges :param n: number of how many edges should be removed :return: """ if n < 1: return edges print("Ensuring same size (Positive/Negative)...") all_edges = SortedList( list(edges[globConst.NODE1_ID_COL_NAME].append( edges[globConst.NODE2_ID_COL_NAME]))) edges_list = set(all_edges) edges_count_dict = {x: all_edges.count(x) for x in edges_list} i = 0 for _ in range(1000): drop_indices_candidates = random.sample( edges.index.values.tolist(), n) drop_indices = [] tqdmbuffer = TqdmBuffer() if globConst.GUI_MODE else None for drop_index in tqdm(drop_indices_candidates, file=tqdmbuffer): if i == n: break drop_edge_candidate = edges.loc[drop_index] if (edges_count_dict[drop_edge_candidate[ globConst.NODE1_ID_COL_NAME]] > 1 and edges_count_dict[drop_edge_candidate[ globConst.NODE2_ID_COL_NAME]] > 1): edges_count_dict[drop_edge_candidate[ globConst.NODE1_ID_COL_NAME]] = edges_count_dict[ drop_edge_candidate[ globConst.NODE1_ID_COL_NAME]] - 1 edges_count_dict[drop_edge_candidate[ globConst.NODE2_ID_COL_NAME]] = edges_count_dict[ drop_edge_candidate[ globConst.NODE2_ID_COL_NAME]] - 1 drop_indices.append(drop_index) i += 1 edges.drop(inplace=True, index=drop_indices) if i == n: break edges.reset_index(drop=True, inplace=True) return edges
################################################ l1 = [(1, 10), (2, 100), (2, -5), (1, -100)] l1.sort(key=lambda x: (x[0], x[1])) print(l1) ################################################ l2 = [i for i in range(10)] bisect.bisect(l2, 5) # binary search from sortedcontainers import SortedList # treemap sl = SortedList(['e', 'a', 'c', 'd', 'b']) sl SortedList(['a', 'b', 'c', 'd', 'e']) sl *= 10_000_000 sl.count('c') 10000000 sl[-3:] ['e', 'e', 'e'] from sortedcontainers import SortedDict sd = SortedDict({'c': 3, 'a': 1, 'b': 2}) sd SortedDict({'a': 1, 'b': 2, 'c': 3}) sd.popitem(index=-1) ('c', 3) from sortedcontainers import SortedSet ss = SortedSet('abracadabra') ss SortedSet(['a', 'b', 'c', 'd', 'r'])
# http://www.grantjenks.com/docs/sortedcontainers/ from sortedcontainers import SortedList sl = SortedList(['e', 'a', 'c', 'd', 'b']) print(sl) sl *= 10000000 print(sl.count('c')) print(sl[-3:]) ['e', 'e', 'e'] from sortedcontainers import SortedDict sd = SortedDict({'c': 3, 'a': 1, 'b': 2}) print(sd) SortedDict({'a': 1, 'b': 2, 'c': 3}) print(sd.popitem()) from sortedcontainers import SortedSet ss = SortedSet('abracadabra') print(ss) print(ss.bisect_left('c'))
一款纯python写的对列表、字典、集合排序的模块 下面显示的所有操作都比线性时间快 >>> from sortedcontainers import SortedList >>> sl = SortedList(['e', 'a', 'c', 'd', 'b']) >>> sl SortedList(['a', 'b', 'c', 'd', 'e']) >>> sl *= 10_000_000 >>> sl.count('c') 10000000 >>> sl[-3:] ['e', 'e', 'e'] >>> from sortedcontainers import SortedDict >>> sd = SortedDict({'c': 3, 'a': 1, 'b': 2}) >>> sd SortedDict({'a': 1, 'b': 2, 'c': 3}) >>> sd.popitem(index=-1) ('c', 3) >>> from sortedcontainers import SortedSet >>> ss = SortedSet('abracadabra') >>> ss SortedSet(['a', 'b', 'c', 'd', 'r']) >>> ss.bisect_left('c') 2
# pip install sortedcontainers from sortedcontainers import SortedList sl = SortedList(['e', 'a', 'c', 'd', 'b']) print(sl) SortedList(['a', 'b', 'c', 'd', 'e']) sl *= 10_000_000 print(sl.count('c')) print(sl[-3:]) from sortedcontainers import SortedDict sd = SortedDict({'c': 3, 'a': 1, 'b': 2}) print(sd) SortedDict({'a': 1, 'b': 2, 'c': 3}) print(sd.popitem(index=-1)) from sortedcontainers import SortedSet ss = SortedSet('abracadabra') print(ss) SortedSet(['a', 'b', 'c', 'd', 'r']) print(ss.bisect_left('c'))