获取长度为 n 的所有(n-选择-k)组合

2024-06-06

我怎样才能获得长度的所有组合(按顺序)n从数字列表中?例如,给定列表[1, 2, 3, 4],并设置n = 3,我怎样才能得到这些结果?

[1, 2, 3]
[1, 2, 4]
[1, 3, 4]
[2, 3, 4]

For combinations of all possible lengths, see Get all possible (2^N) combinations of a list’s elements, of any length https://stackoverflow.com/questions/464864 . Note that this is not simply a matter of iterating over the possible lengths and combining the results, as there are other reasonable approaches to the problem.

To avoid duplicate outputs when the input has duplicate elements, see Python combinations without repetitions https://stackoverflow.com/questions/36429507 .

Also related: Generate all binary strings of length n with k bits set https://stackoverflow.com/questions/1851134


itertools https://docs.python.org/3/library/itertools.html#itertools.combinations可以这样做:

import itertools

for comb in itertools.combinations([1, 2, 3, 4], 3):
    print(comb)

Outputs:

(1, 2, 3)
(1, 2, 4)
(1, 3, 4)
(2, 3, 4)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

获取长度为 n 的所有(n-选择-k)组合 的相关文章

随机推荐

  • 为什么没有参数的函数(与实际函数定义相比)可以编译?

    我刚刚看到某人的 C 代码 我很困惑为什么要编译它 有两点我不明白 The 函数原型与实际函数定义相比没有参数 中的参数函数定义没有类型 include
  • iOS HTTP 请求在后台运行

    当应用程序处于后台时 可以向 PHP 服务器发出 HTTP 异步请求吗 该应用程序是基于位置的应用程序 应收集当前位置并每 5 或其他值 分钟将坐标发送到服务器 即使应用程序处于后台 我也可以将 http 帖子发送到服务器吗 我读到了很多关
  • Thymeleaf Spring 安全集成 sec:授权不起作用

    我正在使用带有 Spring security 的 Thymeleaf 模板引擎 我还使用 Thymeleaf Spring Security 集成模块来使用 sec authorize 功能 但由于某种原因它不起作用 我没有收到任何错误
  • 在 pip 中为 Flask 应用程序构建 docker 映像失败

    from alpine latest RUN apk add no cache python3 dev pip3 install upgrade pip WORKDIR backend COPY backend RUN pip no cac
  • 类型铸造自我

    以下自我转换对 ClassA 有何作用 这种种姓可以让你进入ClassA吗 A h类 interface ClassA NSObject NSUInteger someNumber ClassB classB property nonato
  • Spring - 使用存储过程时使用 simplejdbccall 进行批量更新

    我正在使用 spring jdbc 模板 使用存储过程创建记录 public Long create City obj SimpleJdbcCall jdbcCall new SimpleJdbcCall getJdbcTemplate g
  • 用于高级搜索/过滤的.Net Web API URL 约定

    我对 Microsoft 的 REST 和 WebAPI 比较陌生 我们正在实现一个中心 REST 服务 它将容纳多种类型的对象获取和设置 作为该项目的领导者 我的任务是提出我们正在使用的正确的 Uri 设计 我想知道关于战争什么想法更好
  • 你遇到过哪些 git 陷阱?

    我遇到的最糟糕的情况是 git 子模块 我在 github 上有一个项目的子模块 该项目无人维护 我想提交补丁 但无法提交 所以我分叉了 现在子模块指向原始库 而我需要它指向 fork 因此 我删除了旧的子模块 并将其替换为同一提交中新项目
  • MsBuild 在 Visual Studio Online 上找不到恢复的 NuGet 包

    我尝试构建一个存储在 Visual Studio Online 上的外部 GIT 存储库中的解决方案 它有以下步骤 1 Git 恢复 有效 2 NuGet 恢复 有效 3 构建 不起作用 查看日志时我的第一个猜测是 MsBuild 没有查找
  • 将文件附加到 PHPMailer

    我目前正在开发一个项目 该项目将文件作为 blob 存储在数据库中 我需要将文件附加到电子邮件并通过 PHPMailer 发送出去 我熟悉 mail gt addAttachment 但是 这个函数似乎只接受文件路径 而我没有 我想知道是否
  • Gitflow错误无法初始化

    我已经将 gitflow 安装在我的 github 项目所在的目录中 但是 当我尝试使用命令 启动时git flow init 我收到以下错误消息 git flow init C cygwin64 usr local bin gitflow
  • 使用Python的timeit获取“全局名称'foo'未定义”

    我想知道执行一条Python语句需要多少时间 所以我上网查了一下 发现标准库提供了一个名为timeit http docs python org library timeit html旨在做到这一点 import timeit def fo
  • 从哪里开始阅读 SQLite 源代码? [关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 我想了解sqlite是如何实现的 并且 想阅读源代码 我已经下载了源代码 我应该开始查看代码的哪一部分 SQLite文档页 http
  • 重构后如何保留 terraform 资源以使用 for_each?

    目前我正在对我们的基础设施进行小型重构 我的项目的当前版本类似于以下内容 我正在尝试使用 for each 来重用变量 resource google cloud scheduler job job name Create All Doss
  • File_get_contents($url): 无法打开流

    我有一个脚本 我使用以下方法读取文件 file get contents urlencode url 我收到此错误 failed to open stream HTTP request failed HTTP 1 0 400 Bad req
  • 如何使用 winrar 提取与 zip 文件同名的文件夹中的所有 zip 文件?

    我需要迭代一个文件夹 对于每个 zip 文件 我需要使用它的名称来提取它 也就是说 如果它是 test zip 那么它应该解压到 test 文件夹 同样 它应该迭代我的文件夹及其子文件夹并提取内容 我编写了下面的代码 但它没有使用 zip
  • 获取css规则、chrome扩展

    我正在开发 Chrome 扩展程序 它需要访问document styleSheets cssRules 它在某些网站上运行良好 例如w3school 但其他人则不然 比如堆栈溢出 我收到错误 Failed to read the cssR
  • 当不存在循环时,terraform destroy 会产生循环错误

    地形版本 Terraform v0 12 1 地形配置文件 main tf在我的根提供程序中 provider google module organisation info source modules organisation info
  • 安装 RVM 时出现问题

    我已按照 rvm 网站上的说明执行了命令 但似乎不起作用 从 git 存储库获取代码运行顺利 但是当我尝试使用时 rvm notes Error usr local bin rvm line 73 home cody rvm scripts
  • 获取长度为 n 的所有(n-选择-k)组合

    我怎样才能获得长度的所有组合 按顺序 n从数字列表中 例如 给定列表 1 2 3 4 并设置n 3 我怎样才能得到这些结果 1 2 3 1 2 4 1 3 4 2 3 4 For combinations of all possible l