如何编辑排列

发布于 2025-01-17 17:22:03 字数 236 浏览 4 评论 0原文

每个数字都是端口。 排列为我提供了所有可能的路线,但是我的起始端口始终是1号,所以在所有组合中,我想要以1开始的组合。

就是我到目前为止的全部

from itertools import permutations

routes = permutations([1 , 2, 3 ,4 , 5])
for i in list(routes):
      print(i)

这 ?

every number is a port.
and the permutation gives me all possible routes but my starting port is always number 1,so out of all combinations i want the ones that start with 1.

this is all i got so far

from itertools import permutations

routes = permutations([1 , 2, 3 ,4 , 5])
for i in list(routes):
      print(i)

is there a way to enable me to pick those specific combinations?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瞄了个咪的 2025-01-24 17:22:03

您可以简单地将1添加到2,3,4,5的排列开始。例如,

from itertools import permutations

routes = permutations([2, 3 ,4 , 5])
for i in routes:
      print([1,*i])

如果您真的想生成所有置换量并将其滤除到以1开始的置换,则可以执行以下操作。

from itertools import permutations

routes = permutations([1,2, 3 ,4 , 5])
for i in routes:
    if i[0] == 1:
        print(i)

You could simply add a 1 to the beginning of the permutations of 2,3,4,5. For example,

from itertools import permutations

routes = permutations([2, 3 ,4 , 5])
for i in routes:
      print([1,*i])

If you really want to generate every permutation and filter down to those starting with 1, you could do the following.

from itertools import permutations

routes = permutations([1,2, 3 ,4 , 5])
for i in routes:
    if i[0] == 1:
        print(i)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文