【Python】列表元素排序:itertools.permutations()

一、题目

This tool returns successive r length permutations of elements in an iterable.

If r is not specified or is None, then r defaults to the length of the iterable, and all possible full length permutations are generated.

Permutations are printed in a lexicographic sorted order. So, if the input iterable is sorted, the permutation tuples will be produced in a sorted order.

Sample Code

from itertools import permutations

print(permutations(['1', '2', '3']))

print(list(permutations(['1', '2', '3'])))

print(list(permutations(['1', '2', '3'], 2)))

print(list(permutations('abc', 3)))

>>>  <itertools.permutations object at 0x00000201DA6478E0>
>>> [('1', '2', '3'), ('1', '3', '2'), ('2', '1', '3'), ('2', '3', '1'), ('3', '1', '2'), ('3', '2', '1')]
>>> [('1', '2'), ('1', '3'), ('2', '1'), ('2', '3'), ('3', '1'), ('3', '2')]
>>> [('a', 'b', 'c'), ('a', 'c', 'b'), ('b', 'a', 'c'), ('b', 'c', 'a'), ('c', 'a', 'b'), ('c', 'b', 'a')]

Input Format

A single line containing the space separated string S and the integer value k.

Output Format

Print the permutations of the string S on separate lines.

Sample Input

HACK 2

Sample Output

 AC
AH
AK
CA
CH
CK
HA
HC
HK
KA
KC
KH

二、代码

from itertools import permutations

S, k = input().split()
k = int(k)

perms = list(permutations(S, k))

print(perms)
for perm in sorted(perms):
    print(''.join(perm))
from itertools import permutations

S, k = input().split()
k = int(k)

perms = list(permutations(S, k))

print(sorted(perms))
sorted_perms = sorted(perms, key=lambda p: ''.join(p))
for perm in sorted_perms:
    print(''.join(perm))

三、解读

S, k = input().split()
k = int(k)
  • 读取用户输入的字符串,假设格式为 "string k",其中 "string" 是由空格分隔的字符组成的字符串,k 是一个整数,表示排列的长度。
  • 使用 split() 方法将输入的字符串分割成两部分:字符串 S 和整数 k。
  • 将分割得到的 k 转换为整数类型,因为 split() 方法返回的是字符串。
perms = list(permutations(S, k))
  • 使用 permutations(S, k) 生成字符串 S 的所有长度为 k 的排列。
  • 将生成的排列转换为列表并存储在变量 perms 中。
for perm in sorted(perms):
    print(''.join(perm))
  • 遍历 sorted(perms),即排序后的排列列表。
  • 对于每个排列 perm,使用 ''.join(perm) 将其从元组转换为字符串,并打印。
sorted_perms = sorted(perms, key=lambda p: ''.join(p))
  • 使用 sorted() 函数和一个 lambda 表达式再次对 perms 列表进行排序。
  • sorted():是Python 的内置函数,用于对可迭代对象的元素进行排序,并返回一个新的排好序的列表。
  • key=lambda p: ''.join(p) :定义排序的依据:
    • lambda:匿名函数,用于创建一个简单的函数对象。
    • p:是 lambda 函数的参数,代表列表 perms 中的每个元素,每个元素都是一个排列的元组。
    • ''.join(p):将元组 p 中的字符连接成一个字符串。因为元组中的字符需要按顺序拼接成字符串才能进行比较。
for perm in sorted_perms:
    print(''.join(perm))
  • key=lambda p: ''.join(p) 指定了排序的关键字,使用排列转换为字符串的结果进行排序。
  • 这步操作同样是多余的,因为 permutations() 已经生成了有序的排列。
  • 遍历 sorted_perms,即使用 lambda 表达式排序后的排列列表。
  • 对于每个排列 perm,将其转换为字符串并打印。

四、itertools.permutations() 通常用在什么地方?

itertools.permutations() 函数用于生成输入可迭代对象的所有可能排列。每个排列都是一个元素序列,其中输入序列的元素以不同的顺序出现。

itertools.permutations() 函数在需要探索所有可能的元素排列的场景中非常有用。由于它返回一个迭代器,因此在处理大量数据时也非常高效。需要注意的是,当输入序列较长时,排列的数量会阶乘增长,可能会导致性能问题。在使用时需要考虑输入序列的长度和实际需求。


1、数学和算法问题:

        解决需要找出所有可能元素顺序的问题,例如排列组合问题。

2、密码学:

        生成密码或秘钥的所有可能排序,用于暴力破解或密码分析。

3、数据加密:

        在某些加密技术中,可能需要对数据进行重新排列以增加安全性。

4、机器学习和数据科学:

        在特征工程或模型训练中,尝试不同的特征组合排列。

5、游戏开发:

        用于生成游戏中所有可能的关卡布局或敌人配置。

6、调度和优化问题:

        在解决调度问题时,找出所有可能的任务序列,例如作业调度、课程表安排等。

7、文本处理:

        对文本中的单词或字符进行重新排列,用于生成文本的变体或进行语言模型训练。

8、艺术和设计:

        在艺术作品或设计中生成图案或颜色的所有可能组合。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值