python显示单词长度_Python:迷你字母中所有固定长度的可能单词(排列)

Let's say I have a string like so:

abcdefghijklmnopqrstuvwxyz1234567890!@#$%^&*()-_+={}[]\:;"'?/>.

This is basicly a list of all the characters on my keyboard. How could I get all possible combinations for, let's say, a "word" made up of 8 of these chars? I know there are going to be millions of possibilities.

Cheers!

解决方案

Difference between permutations and combinations

You are either looking for a permutation or a combination.

'abc' and 'bac' are different permutations, but they are the same combination {a,b,c}.

Permutations of 'abc': '', 'a', 'b', 'c', 'ab', 'ba', 'ac', 'ca', 'bc', 'cb', 'abc', 'acb', 'bac', 'bca', 'cab', 'cba'

Combinations of 'abc': {}, {'a'}, {'b'}, {'c'}, {'a','b'}, {'b','c'}, {'a','c'}, {'a','b','c'}

In python

Use from itertools import * (since the functions there really should be in the default namespace), or import itertools if you'd like.

If you care about permutations:

permutations(yourString, 8)

If you care about combinations :

combinations(yourString, 8)

In other languages

In other languages, there are simple recursive or iterative algorithms to generate these. See wikipedia or stackoverflow. e.g. http://en.wikipedia.org/wiki/Permutation#Systematic_generation_of_all_permutations

Important note

Do note that the number of permutations is N!, so for example your string would have

(69 choose 8) = 8 billion combinations of length 8, and therefore...

(69 choose 8) * 8! ~= 3.37 × 10^14 permutations of length 8.

You'll run out of memory if you are storing every permutation. Even if you don't (because you're reducing them), it'll take a long time to run, maybe somewhere between 1-10 days on a modern computer.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值