當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。