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


Python chain.from_iterable方法代码示例

本文整理汇总了Python中itertools.chain.from_iterable方法的典型用法代码示例。如果您正苦于以下问题:Python chain.from_iterable方法的具体用法?Python chain.from_iterable怎么用?Python chain.from_iterable使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在itertools.chain的用法示例。


在下文中一共展示了chain.from_iterable方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: visit_graph_items

# 需要导入模块: from itertools import chain [as 别名]
# 或者: from itertools.chain import from_iterable [as 别名]
def visit_graph_items(self, items):
        invalid = [f for f in items
                   if not isinstance(f, self._graph_accept_types)]
        if invalid:
            self.errors.report(
                'Graph can not contain these types: {}'
                .format(self._format_types(invalid))
            )
            return

        root = Root(list(chain.from_iterable(e.fields for e in items
                                             if e.name is None)))
        self.visit(root)

        for item in items:
            if item.name is not None:
                self.visit(item)

        duplicates = self._get_duplicates(e.name for e in items
                                          if e.name is not None)
        if duplicates:
            self.errors.report('Duplicated nodes found in the graph: {}'
                               .format(self._format_names(duplicates))) 
开发者ID:vmagamedov,项目名称:hiku,代码行数:25,代码来源:graph.py

示例2: elements

# 需要导入模块: from itertools import chain [as 别名]
# 或者: from itertools.chain import from_iterable [as 别名]
def elements(self):
        '''Iterator over elements repeating each as many times as its count.

        >>> c = Counter('ABCABC')
        >>> sorted(c.elements())
        ['A', 'A', 'B', 'B', 'C', 'C']

        # Knuth's example for prime factors of 1836:  2**2 * 3**3 * 17**1
        >>> prime_factors = Counter({2: 2, 3: 3, 17: 1})
        >>> product = 1
        >>> for factor in prime_factors.elements():     # loop over factors
        ...     product *= factor                       # and multiply them
        >>> product
        1836

        Note, if an element's count has been set to zero or is a negative
        number, elements() will ignore it.

        '''
        # Emulate Bag.do from Smalltalk and Multiset.begin from C++.
        return _chain.from_iterable(_starmap(_repeat, self.items()))

    # Override dict methods where necessary 
开发者ID:war-and-code,项目名称:jawfish,代码行数:25,代码来源:__init__.py

示例3: link_result_to_ids

# 需要导入模块: from itertools import chain [as 别名]
# 或者: from itertools.chain import from_iterable [as 别名]
def link_result_to_ids(from_list, link_type, result):
    if from_list:
        if link_type is Maybe:
            return [i for i in result if i is not Nothing]
        elif link_type is One:
            if any(i is Nothing for i in result):
                raise TypeError('Non-optional link should not return Nothing: '
                                '{!r}'.format(result))
            return result
        elif link_type is Many:
            return list(chain.from_iterable(result))
    else:
        if link_type is Maybe:
            return [] if result is Nothing else [result]
        elif link_type is One:
            if result is Nothing:
                raise TypeError('Non-optional link should not return Nothing')
            return [result]
        elif link_type is Many:
            return result
    raise TypeError(repr([from_list, link_type])) 
开发者ID:vmagamedov,项目名称:hiku,代码行数:23,代码来源:engine.py

示例4: predict

# 需要导入模块: from itertools import chain [as 别名]
# 或者: from itertools.chain import from_iterable [as 别名]
def predict(self, query):
        cache_query_key = ''.join(str(list(chain.from_iterable(query[0]))))
        if cache_query_key in self.cached_res.keys():
            return self.cached_res[cache_query_key]

        input_ids, input_len = query
        input_ids = torch.tensor(input_ids).to(self.device).unsqueeze(0)
        input_len = torch.tensor(input_len).to(self.device).unsqueeze(0)
        labels = None
        _, pred_slot = self.sumbt_model(input_ids, input_len, labels)
        pred_slot_t = pred_slot[0][-1].tolist()
        predict_belief = []
        for idx,i in enumerate(pred_slot_t):
            predict_belief.append(f'{self.target_slot[idx]}-{self.label_map_inv[idx][i]}')
        self.cached_res[cache_query_key] = predict_belief
        return predict_belief 
开发者ID:ConvLab,项目名称:ConvLab,代码行数:18,代码来源:sumbt.py

示例5: apply

# 需要导入模块: from itertools import chain [as 别名]
# 或者: from itertools.chain import from_iterable [as 别名]
def apply(self, solver, players_dict):
        optimizer = self.optimizer
        positions, spacing = optimizer.spacing_positions, optimizer.spacing
        if not spacing or not positions:
            return
        players_by_roster_positions = defaultdict(list)  # type: Dict[int, List[Tuple[Player, Any]]]
        for player, variable in players_dict.items():
            if player.roster_order is None or not list_intersection(player.positions, positions):
                continue
            players_by_roster_positions[player.roster_order].append((player, variable))
        for roster_position, players in players_by_roster_positions.items():
            next_restricted_roster_position = roster_position + spacing
            restricted_players = chain.from_iterable(
                players for players_spacing, players in players_by_roster_positions.items()
                if players_spacing >= next_restricted_roster_position
            )
            for first_player, first_variable in restricted_players:
                for second_player, second_variable in players:
                    if first_player.team != second_player.team:
                        continue
                    solver.add_constraint([first_variable, second_variable], None, SolverSign.LTE, 1) 
开发者ID:DimaKudosh,项目名称:pydfs-lineup-optimizer,代码行数:23,代码来源:rules.py

示例6: build_stacks

# 需要导入模块: from itertools import chain [as 别名]
# 或者: from itertools.chain import from_iterable [as 别名]
def build_stacks(self, players: List[Player], optimizer: 'LineupOptimizer') -> List[OptimizerStack]:
        players_by_teams = get_players_grouped_by_teams(players, for_teams=self.for_teams)
        all_positions = tuple(set(chain.from_iterable(self.positions)))
        positions_for_optimizer = Counter(self.positions)
        positions_for_optimizer[all_positions] = len(self.positions)
        all_groups = []  # type: List[BaseGroup]
        for team_name, team_players in players_by_teams.items():
            groups = []
            for positions, total in positions_for_optimizer.items():
                groups.append(PlayersGroup(
                    players=[player for player in team_players if list_intersection(player.positions, positions)],
                    min_from_group=total,
                ))
            nested_group = NestedPlayersGroup(
                groups=groups,
                max_exposure=self.max_exposure_per_team.get(team_name, self.max_exposure),
            )
            all_groups.append(nested_group)
        return [OptimizerStack(groups=all_groups)] 
开发者ID:DimaKudosh,项目名称:pydfs-lineup-optimizer,代码行数:21,代码来源:stacks.py

示例7: test_multi_get_max_retry

# 需要导入模块: from itertools import chain [as 别名]
# 或者: from itertools.chain import from_iterable [as 别名]
def test_multi_get_max_retry(self):
        """Tests the case when the number of the maximum retries is reached, due to the unsuccessful responses.
        Request is repeated 3 times (based on `max_retry`), each time there is only one successful response.
        Eventually the call to `multi_get` returns the responses among which one is unsuccessful (`None`).
        """
        number_of_requests = 4
        query_params = [{'John Fitzgerald': 'Tom Hardy'}] * number_of_requests
        responses_to_calls = [
            self.mock_ok_responses(number_of_requests),
            self.mock_ok_responses(number_of_requests - 1),
            self.mock_ok_responses(number_of_requests - 2),
        ]
        # mock unsuccessful responses to the first call
        self.mock_unsuccessful_responses(responses_to_calls[0][0:3])
        # mock unsuccessful responses to the second call
        self.mock_unsuccessful_responses(responses_to_calls[1][1:3])
        # mock unsuccessful response to the third call
        self.mock_unsuccessful_response(responses_to_calls[2][1])
        get_mock = self.mock_request_futures(chain.from_iterable(responses_to_calls))

        actual_responses = MultiRequest(max_retry=3).multi_get('example.com', query_params)

        T.assert_equal(get_mock.call_count, 9)
        T.assert_is(actual_responses[2], None) 
开发者ID:Yelp,项目名称:threat_intel,代码行数:26,代码来源:http_test.py

示例8: test_migrate_cancer_interpretation_request

# 需要导入模块: from itertools import chain [as 别名]
# 或者: from itertools.chain import from_iterable [as 别名]
def test_migrate_cancer_interpretation_request(self, fill_nullables=True):
        # get original IR in version 4.0.0
        assembly = Assembly.GRCh38
        original_ir = self.get_valid_object(
            object_type=reports_4_0_0.CancerInterpretationRequest, version=self.version_4_0_0,
            fill_nullables=fill_nullables, genomeAssemblyVersion=assembly, structuralTieredVariants=[],
            versionControl=reports_4_0_0.ReportVersionControl(gitVersionControl='4.0.0')
        )
        valid_cancer_origins = ['germline_variant', 'somatic_variant']
        for tiered_variant in original_ir.tieredVariants:
            if tiered_variant.alleleOrigins[0] not in valid_cancer_origins:
                tiered_variant.alleleOrigins[0] = random.choice(valid_cancer_origins)
        # # migration requires there is exactly one tumour sample
        original_ir.cancerParticipant.tumourSamples = [original_ir.cancerParticipant.tumourSamples[0]]
        migrated, round_tripped = MigrationRunner().roundtrip_cancer_ir(original_ir, assembly)
        self.assertFalse(self.diff_round_tripped(original_ir, round_tripped, ignore_fields=[
            "TNMStageVersion", "TNMStageGrouping", "actions",
            "additionalTextualVariantAnnotations", "matchedSamples", "commonAf", "additionalInfo"]))
        # NOTE: not all fields in actions are kept and the order is not maintained, thus we ignore it in the
        # dictionary comparison and then here manually check them
        expected_report_events = chain.from_iterable(
            map(lambda v: [re for re in v.reportedVariantCancer.reportEvents], original_ir.tieredVariants))
        observed_report_events = chain.from_iterable(
            map(lambda v: [re for re in v.reportedVariantCancer.reportEvents], round_tripped.tieredVariants))
        self.assertFalse(self.diff_actions(chain(expected_report_events, observed_report_events))) 
开发者ID:genomicsengland,项目名称:GelReportModels,代码行数:27,代码来源:test_migration_reports_cancer_400_to_600_to_400.py

示例9: test_migrate_cancer_interpreted_genome

# 需要导入模块: from itertools import chain [as 别名]
# 或者: from itertools.chain import from_iterable [as 别名]
def test_migrate_cancer_interpreted_genome(self, fill_nullables=True):
        assembly = Assembly.GRCh38
        original_ig = self.get_valid_object(
            object_type=reports_4_0_0.CancerInterpretedGenome, version=self.version_4_0_0,
            fill_nullables=fill_nullables, genomeAssemblyVersion=assembly,
            interpretGenome=True, reportedStructuralVariants=[],
            versionControl=reports_4_0_0.ReportVersionControl(gitVersionControl='4.0.0')
        )
        valid_cancer_origins = ['germline_variant', 'somatic_variant']
        for reported_variant in original_ig.reportedVariants:
            if reported_variant.alleleOrigins[0] not in valid_cancer_origins:
                reported_variant.alleleOrigins[0] = random.choice(valid_cancer_origins)
        # migration requires there is exactly one tumour sample
        migrated, round_tripped = MigrationRunner().roundtrip_cancer_ig(original_ig, assembly)
        self.assertFalse(self.diff_round_tripped(original_ig, round_tripped, ignore_fields=[
            "analysisId", "actions", "additionalTextualVariantAnnotations", "commonAf"]))

        # NOTE: not all fields in actions are kept and the order is not maintained, thus we ignore it in the
        # dictionary comparison and then here manually check them
        expected_report_events = chain.from_iterable(
            map(lambda v: [re for re in v.reportedVariantCancer.reportEvents], original_ig.reportedVariants))
        observed_report_events = chain.from_iterable(
            map(lambda v: [re for re in v.reportedVariantCancer.reportEvents], round_tripped.reportedVariants))
        self.assertFalse(self.diff_actions(chain(expected_report_events, observed_report_events))) 
开发者ID:genomicsengland,项目名称:GelReportModels,代码行数:26,代码来源:test_migration_reports_cancer_400_to_600_to_400.py

示例10: _parallel_predict

# 需要导入模块: from itertools import chain [as 别名]
# 或者: from itertools.chain import from_iterable [as 别名]
def _parallel_predict(self, contexts: np.ndarray, is_predict: bool):

        # Total number of contexts to predict
        n_contexts = len(contexts)

        # Partition contexts by job
        n_jobs, n_contexts, starts = self._partition_contexts(n_contexts)
        total_contexts = sum(n_contexts)

        # Get seed value for each context
        seeds = self.rng.randint(np.iinfo(np.int32).max, size=total_contexts)

        # Perform parallel predictions
        predictions = Parallel(n_jobs=n_jobs, backend=self.backend)(
                          delayed(self._predict_contexts)(
                              contexts[starts[i]:starts[i + 1]],
                              is_predict,
                              seeds[starts[i]:starts[i + 1]],
                              starts[i])
                          for i in range(n_jobs))

        # Reduce
        predictions = list(chain.from_iterable(t for t in predictions))

        return predictions if len(predictions) > 1 else predictions[0] 
开发者ID:fidelity,项目名称:mabwiser,代码行数:27,代码来源:base_mab.py

示例11: build_vocab

# 需要导入模块: from itertools import chain [as 别名]
# 或者: from itertools.chain import from_iterable [as 别名]
def build_vocab(self, *args, **kwargs):
        counter = Counter()
        sources = []
        for arg in args:
            if isinstance(arg, Dataset):
                sources += [getattr(arg, name) for name, field in arg.fields.items() if field is self]
            else:
                sources.append(arg)

        for data in sources:
            for x in data:
                x = self.preprocess(x)
                try:
                    counter.update(x)
                except TypeError:
                    counter.update(chain.from_iterable(x))

        specials = list(OrderedDict.fromkeys([
            tok for tok in [self.unk_token, self.pad_token, self.init_token,
                            self.eos_token]
            if tok is not None]))
        self.vocab = self.vocab_cls(counter, specials=specials, **kwargs) 
开发者ID:aimagelab,项目名称:speaksee,代码行数:24,代码来源:field.py

示例12: _get_all_related_objects

# 需要导入模块: from itertools import chain [as 别名]
# 或者: from itertools.chain import from_iterable [as 别名]
def _get_all_related_objects(self, local_only=False, include_hidden=False,
                                 include_proxy_eq=False):
        """
        Returns a list of related fields (also many to many)
        :param local_only:
        :param include_hidden:
        :return: list
        """
        include_parents = True if local_only is False else PROXY_PARENTS
        fields = self.opts._get_fields(
            forward=False, reverse=True,
            include_parents=include_parents,
            include_hidden=include_hidden
        )
        if include_proxy_eq:
            children = chain.from_iterable(c._relation_tree
                                           for c in self.opts.concrete_model._meta.proxied_children
                                           if c is not self.opts)
            relations = (f.remote_field for f in children
                         if include_hidden or not f.remote_field.field.remote_field.is_hidden())
            fields = chain(fields, relations)
        return list(fields) 
开发者ID:stormsha,项目名称:StormOnline,代码行数:24,代码来源:relate.py

示例13: unpack_list

# 需要导入模块: from itertools import chain [as 别名]
# 或者: from itertools.chain import from_iterable [as 别名]
def unpack_list(items):
    return chain.from_iterable(item if isinstance(item, list) else [item] for item in items)

# this transforms a particular pattern into a generator for statements 
开发者ID:adlnet,项目名称:xapi-profiles,代码行数:6,代码来源:test_matching.py

示例14: train_step

# 需要导入模块: from itertools import chain [as 别名]
# 或者: from itertools.chain import from_iterable [as 别名]
def train_step(self, env_xs, env_as, env_rs, env_vs):
        # NOTE(reed): Reshape to set the data shape.
        self.model.reshape([('data', (len(env_xs), self.input_size))])

        xs = mx.nd.array(env_xs, ctx=self.ctx)
        as_ = np.array(list(chain.from_iterable(env_as)))

        # Compute discounted rewards and advantages.
        advs = []
        gamma, lambda_ = self.config.gamma, self.config.lambda_
        for i in range(len(env_vs)):
            # Compute advantages using Generalized Advantage Estimation;
            # see eqn. (16) of [Schulman 2016].
            delta_t = (env_rs[i] + gamma*np.array(env_vs[i][1:]) -
                       np.array(env_vs[i][:-1]))
            advs.extend(self._discount(delta_t, gamma * lambda_))

        # Negative generalized advantage estimations.
        neg_advs_v = -np.asarray(advs)

        # NOTE(reed): Only keeping the grads for selected actions.
        neg_advs_np = np.zeros((len(advs), self.act_space), dtype=np.float32)
        neg_advs_np[np.arange(neg_advs_np.shape[0]), as_] = neg_advs_v
        neg_advs = mx.nd.array(neg_advs_np, ctx=self.ctx)

        # NOTE(reed): The grads of values is actually negative advantages.
        v_grads = mx.nd.array(self.config.vf_wt * neg_advs_v[:, np.newaxis],
                              ctx=self.ctx)

        data_batch = mx.io.DataBatch(data=[xs], label=None)
        self._forward_backward(data_batch=data_batch,
                               out_grads=[neg_advs, v_grads])

        self._update_params() 
开发者ID:awslabs,项目名称:dynamic-training-with-apache-mxnet-on-aws,代码行数:36,代码来源:model.py

示例15: depth

# 需要导入模块: from itertools import chain [as 别名]
# 或者: from itertools.chain import from_iterable [as 别名]
def depth(self) -> int:
        """Return the farthest depth of the CodeBlock."""
        i = iter(self)
        level = 0
        try:
            for level in count():
                i = chain([next(i)], i)
                i = chain.from_iterable(s for s in i if isinstance(s, Sequence) and not isinstance(s, str))
        except StopIteration:
            return level
        return 0 
开发者ID:erp12,项目名称:pyshgp,代码行数:13,代码来源:atoms.py


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