【问题标题】:Arrange itertools product in numerical order按数字顺序排列 itertools 产品
【发布时间】:2021-12-02 12:12:44
【问题描述】:

我有一个由 itertools 通过以下方式完成的产品:

CHs=list(itertools.product(at_idx_map['H'], at_idx_map['C']))

获取 CH 的以下输出:

[(0, 1), (0, 6), (0, 11), (5, 1), (5, 6), (5, 11), (10, 1), (10, 6), (10, 11)]

但是我需要按数字顺序排列对,我想要的输出是:

[(0, 1), (0, 6), (0, 11), (1,5), (5, 6), (5, 11), (1, 10), (6, 10), (10, 11)]

有人对此有什么建议吗:

【问题讨论】:

  • 内置的sorted()怎么样?

标签: python tuples itertools


【解决方案1】:

在收到的每件商品上应用sorted

CHs=list(tuple(sorted(pair)) for pair in itertools.product(at_idx_map['H'], at_idx_map['C']))

替代方案:

CHs=list(map(tuple, map(sorted, itertools.product(at_idx_map['H'], at_idx_map['C']))))

【讨论】:

  • 这回答了你的问题吗?
猜你喜欢
  • 1970-01-01
  • 2015-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-18
  • 1970-01-01
相关资源
最近更新 更多