当前位置: 首页>>代码示例>>Python>>正文


Python more_itertools.pairwise方法代码示例

本文整理汇总了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 
开发者ID:google,项目名称:ml-fairness-gym,代码行数:27,代码来源:college_admission_util.py

示例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)) 
开发者ID:sofia-netsurv,项目名称:python-netsurv,代码行数:6,代码来源:test_recipes.py

示例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)) 
开发者ID:sofia-netsurv,项目名称:python-netsurv,代码行数:6,代码来源:test_recipes.py

示例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) 
开发者ID:quantumsim,项目名称:quantumsim,代码行数:41,代码来源:model.py

示例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)) 
开发者ID:google,项目名称:ml-fairness-gym,代码行数:17,代码来源:core.py

示例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 
开发者ID:google,项目名称:ml-fairness-gym,代码行数:29,代码来源:college_admission.py


注:本文中的more_itertools.pairwise方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。