本文整理汇总了Python中more_itertools.consecutive_groups方法的典型用法代码示例。如果您正苦于以下问题:Python more_itertools.consecutive_groups方法的具体用法?Python more_itertools.consecutive_groups怎么用?Python more_itertools.consecutive_groups使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类more_itertools
的用法示例。
在下文中一共展示了more_itertools.consecutive_groups方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_exotic_ordering
# 需要导入模块: import more_itertools [as 别名]
# 或者: from more_itertools import consecutive_groups [as 别名]
def test_exotic_ordering(self):
iterable = [
('a', 'b', 'c', 'd'),
('a', 'c', 'b', 'd'),
('a', 'c', 'd', 'b'),
('a', 'd', 'b', 'c'),
('d', 'b', 'c', 'a'),
('d', 'c', 'a', 'b'),
]
ordering = list(permutations('abcd')).index
actual = [list(g) for g in mi.consecutive_groups(iterable, ordering)]
expected = [
[('a', 'b', 'c', 'd')],
[('a', 'c', 'b', 'd'), ('a', 'c', 'd', 'b'), ('a', 'd', 'b', 'c')],
[('d', 'b', 'c', 'a'), ('d', 'c', 'a', 'b')],
]
self.assertEqual(actual, expected)
示例2: test_numbers
# 需要导入模块: import more_itertools [as 别名]
# 或者: from more_itertools import consecutive_groups [as 别名]
def test_numbers(self):
iterable = [-10, -8, -7, -6, 1, 2, 4, 5, -1, 7]
actual = [list(g) for g in mi.consecutive_groups(iterable)]
expected = [[-10], [-8, -7, -6], [1, 2], [4, 5], [-1], [7]]
self.assertEqual(actual, expected)
示例3: test_custom_ordering
# 需要导入模块: import more_itertools [as 别名]
# 或者: from more_itertools import consecutive_groups [as 别名]
def test_custom_ordering(self):
iterable = ['1', '10', '11', '20', '21', '22', '30', '31']
ordering = lambda x: int(x)
actual = [list(g) for g in mi.consecutive_groups(iterable, ordering)]
expected = [['1'], ['10', '11'], ['20', '21', '22'], ['30', '31']]
self.assertEqual(actual, expected)
示例4: label_sequential_regions
# 需要导入模块: import more_itertools [as 别名]
# 或者: from more_itertools import consecutive_groups [as 别名]
def label_sequential_regions(inlist):
"""Input a list of labeled tuples and return a dictionary of sequentially labeled regions.
Args:
inlist (list): A list of tuples with the first number representing the index and the second the index label.
Returns:
dict: Dictionary of labeled regions.
Examples:
>>> label_sequential_regions([(1, 'O'), (2, 'O'), (3, 'O'), (4, 'M'), (5, 'M'), (6, 'I'), (7, 'M'), (8, 'O'), (9, 'O')])
{'O1': [1, 2, 3], 'M1': [4, 5], 'I1': [6], 'M2': [7], 'O2': [8, 9]}
"""
import more_itertools as mit
df = pd.DataFrame(inlist).set_index(0)
labeled = {}
for label in df[1].unique():
iterable = df[df[1] == label].index.tolist()
labeled.update({'{}{}'.format(label, i + 1): items for i, items in
enumerate([list(group) for group in mit.consecutive_groups(iterable)])})
return labeled
示例5: update_utxos
# 需要导入模块: import more_itertools [as 别名]
# 或者: from more_itertools import consecutive_groups [as 别名]
def update_utxos(self, utxos_to_add: List[UtxoType], utxos_to_update: List[UtxoType], utxos_to_delete: List[Tuple[int, int]]):
if utxos_to_delete:
row_indexes_to_remove = []
for utxo_id in utxos_to_delete:
utxo = self.utxo_by_id.get(utxo_id)
if utxo:
utxo_index = self.utxos.index(utxo)
if utxo_index not in row_indexes_to_remove:
row_indexes_to_remove.append(utxo_index)
del self.utxo_by_id[utxo_id]
row_indexes_to_remove.sort(reverse=True)
for group in consecutive_groups(row_indexes_to_remove, ordering=lambda x: -x):
l = list(group)
self.beginRemoveRows(QModelIndex(), l[-1], l[0]) # items are sorted in reversed order
del self.utxos[l[-1]: l[0]+1]
self.endRemoveRows()
if utxos_to_add:
# in the model, the rows are sorted by the number of confirmations in the descending order, so put
# the new ones in the right place
# filter out the already existing utxos
utxos_to_add_verified = []
for utxo in utxos_to_add:
if utxo.id not in self.utxo_by_id:
utxos_to_add_verified.append(utxo)
utxos_to_add_verified.sort(key=lambda x: x.block_height, reverse=True)
row_idx = 0
self.beginInsertRows(QModelIndex(), row_idx, row_idx + len(utxos_to_add_verified) - 1)
try:
for index, utxo in enumerate(utxos_to_add_verified):
if utxo.id not in self.utxo_by_id:
self.add_utxo(utxo, index)
finally:
self.endInsertRows()
if utxos_to_update:
for utxo_new in utxos_to_update:
utxo = self.utxo_by_id.get(utxo_new.id)
if utxo:
utxo.block_height = utxo_new.block_height # block_height is the only field that can be updated
utxo_index = self.utxos.index(utxo)
ui_index = self.index(utxo_index, 0)
self.dataChanged.emit(ui_index, ui_index)