推荐学习书目
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 · Dec 8, 2018 via iPhone · 2034 views
    This topic created in 2733 days ago, the information mentioned may be changed or developed.

    比如 abcd

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

    好像没了吧

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

    8 replies    2018-12-11 20:07:09 +08:00
    westoy
        1
    westoy  
       Dec 8, 2018
    itertools.permutations
    crab
        2
    crab  
       Dec 8, 2018
    from itertools import permutations
    iPhonePKAndroid
        3
    iPhonePKAndroid  
    OP
       Dec 8, 2018
    @crab
    @westoy 谢谢谢谢,就是他
    mainlong
        4
    mainlong  
       Dec 8, 2018 via Android
    这应该是高中数学上排列组合。可以搜得到相关库。

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

    如果有重复的就比较麻烦了,花时间算了。
    iPhonePKAndroid
        5
    iPhonePKAndroid  
    OP
       Dec 10, 2018
    @crab
    @westoy 有什么方法能够计算他能够生成多少个数量?
    necomancer
        6
    necomancer  
       Dec 11, 2018
    @iPhonePKAndroid 全排列:
    def p(n,m):
    necomancer
        7
    necomancer  
       Dec 11, 2018
    @iPhonePKAndroid
    全排列:
    from math import gamma
    def p(n,m):
    return gamma(n)/gamma(m)
    necomancer
        8
    necomancer  
       Dec 11, 2018
    from scipy.special import loggamma
    import numpy as np
    def p(n, m):
    return np.exp(loggamma(n+1)-loggamma(m+1))
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5618 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 47ms · UTC 07:38 · PVG 15:38 · LAX 00:38 · JFK 03:38
    ♥ Do have faith in what you're doing.