本文整理汇总了Python中more_itertools.pairwise方法的典型用法代码示例。如果您正苦于以下问题:Python more_itertools.pairwise方法的具体用法?Python more_itertools.pairwise怎么用?Python more_itertools.pairwise使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类more_itertools
的用法示例。
在下文中一共展示了more_itertools.pairwise方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: realign_history
# 需要导入模块: import more_itertools [as 别名]
# 或者: from more_itertools import pairwise [as 别名]
def realign_history(history):
""""Realigns history so as to be compatible with auditors.
Since the true applicants groups, unmanipulated test scores and true_eligible
are generated before the agent's action, they are in the previous state, so we
push them one step ahead in history and ignore the first step.
Args:
history: A list of tuples of state, action pairs.
Returns:
A realigned history with changed state, action pairs.
"""
realign_variables = [
'test_scores_x', 'applicant_groups', 'true_eligible', 'params'
]
realigned_history = []
for (state, _), (next_state,
next_action) in more_itertools.pairwise(history):
new_history_point = core.HistoryItem(
state=copy.deepcopy(next_state), action=copy.deepcopy(next_action))
for variable in realign_variables:
setattr(new_history_point.state, variable, getattr(state, variable))
realigned_history.append(new_history_point)
return realigned_history
示例2: test_base_case
# 需要导入模块: import more_itertools [as 别名]
# 或者: from more_itertools import pairwise [as 别名]
def test_base_case(self):
"""ensure an iterable will return pairwise"""
p = mi.pairwise([1, 2, 3])
self.assertEqual([(1, 2), (2, 3)], list(p))
示例3: test_short_case
# 需要导入模块: import more_itertools [as 别名]
# 或者: from more_itertools import pairwise [as 别名]
def test_short_case(self):
"""ensure an empty iterator if there's not enough values to pair"""
p = mi.pairwise("a")
self.assertRaises(StopIteration, lambda: next(p))
示例4: add_waiting_gates
# 需要导入模块: import more_itertools [as 别名]
# 或者: from more_itertools import pairwise [as 别名]
def add_waiting_gates(self, circuit):
"""Insert missing waiting placeholders.
Parameters
----------
circuit : quantumsim.circuits.TimeAware
Returns
-------
quantumsim.circuits.Circuit
"""
gates_dict = defaultdict(list)
for gate in circuit.gates:
for qubit in gate.qubits:
gates_dict[qubit].append(gate)
time_start = circuit.time_start
time_end = circuit.time_end
margin = 1e-1
waiting_gates = []
for qubit, gates in gates_dict.items():
duration = gates[0].time_start - time_start
if duration > margin:
waiting_gates.append(
self.waiting_gate(qubit, duration)
.shift(time_start=time_start))
duration = time_end - gates[-1].time_end
if duration > margin:
waiting_gates.append(
self.waiting_gate(qubit, duration)
.shift(time_end=time_end))
for gate1, gate2 in pairwise(gates):
duration = gate2.time_start - gate1.time_end
if duration > margin:
waiting_gates.append(self.waiting_gate(qubit, duration)
.shift(time_start=gate1.time_end))
gates = sorted(circuit.gates + waiting_gates,
key=lambda g: g.time_start)
return Circuit(circuit.qubits, gates)
示例5: _validate_history
# 需要导入模块: import more_itertools [as 别名]
# 或者: from more_itertools import pairwise [as 别名]
def _validate_history(self, history):
"""Checks that a history can be replayed using the metric's simulation.
Args:
history: an iterable of (state, action) pairs.
Raises:
ValueError if the metric's simulation and the history do not match.
"""
history = copy.deepcopy(history)
for idx, (step, next_step) in enumerate(more_itertools.pairwise(history)):
simulated_state = self._simulate(step.state, step.action)
if simulated_state != next_step.state:
raise ValueError('Invalid history at step %d %s != %s' %
(idx, step, next_step))
示例6: realign_history
# 需要导入模块: import more_itertools [as 别名]
# 或者: from more_itertools import pairwise [as 别名]
def realign_history(self, history):
""""Realigns history so as to be compatible with auditors.
Since the true applicants groups, unmanipulated test scores and
true_eligible
are generated before the agent's action, they are in the previous state, so
we
push them one step ahead in history and ignore the first step.
Args:
history: A list of tuples of state, action pairs.
Returns:
A realigned history with changed state, action pairs.
"""
realign_variables = [
'test_scores_x', 'applicant_groups', 'true_eligible', 'params'
]
realigned_history = []
for (state, _), (next_state,
next_action) in more_itertools.pairwise(history):
new_history_point = core.HistoryItem(
state=copy.deepcopy(next_state), action=copy.deepcopy(next_action))
for variable in realign_variables:
setattr(new_history_point.state, variable, getattr(state, variable))
realigned_history.append(new_history_point)
return realigned_history