V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
iPhonePKAndroid
V2EX  ›  Python

Python 如何对一组字符串按照不同位置生成

  •  
  •   iPhonePKAndroid · 2018-12-08 11:43:55 +08:00 via iPhone · 1533 次点击
    这是一个创建于 1937 天前的主题,其中的信息可能已经有所发展或是发生改变。

    比如 abcd

    我要生成 abcd dcab bcda adcb cdab badc dabc chad

    好像没了吧

    这样子用 python 有什么好的算法吗

    8 条回复    2018-12-11 20:07:09 +08:00
    westoy
        1
    westoy  
       2018-12-08 11:47:55 +08:00
    itertools.permutations
    crab
        2
    crab  
       2018-12-08 11:49:04 +08:00
    from itertools import permutations
    iPhonePKAndroid
        3
    iPhonePKAndroid  
    OP
       2018-12-08 11:59:26 +08:00
    @crab
    @westoy 谢谢谢谢,就是他
    mainlong
        4
    mainlong  
       2018-12-08 14:21:02 +08:00 via Android
    这应该是高中数学上排列组合。可以搜得到相关库。

    如果字符串里面字符不重复,那应该是 n!的种,你这个应该是 24 种。

    如果有重复的就比较麻烦了,花时间算了。
    iPhonePKAndroid
        5
    iPhonePKAndroid  
    OP
       2018-12-10 12:00:12 +08:00
    @crab
    @westoy 有什么方法能够计算他能够生成多少个数量?
    necomancer
        6
    necomancer  
       2018-12-11 20:02:04 +08:00
    @iPhonePKAndroid 全排列:
    def p(n,m):
    necomancer
        7
    necomancer  
       2018-12-11 20:04:47 +08:00
    @iPhonePKAndroid
    全排列:
    from math import gamma
    def p(n,m):
    return gamma(n)/gamma(m)
    necomancer
        8
    necomancer  
       2018-12-11 20:07:09 +08:00
    from scipy.special import loggamma
    import numpy as np
    def p(n, m):
    return np.exp(loggamma(n+1)-loggamma(m+1))
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1441 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 23:48 · PVG 07:48 · LAX 16:48 · JFK 19:48
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.