Python Itertools.Permutations()

前端 未结 4 1692
离开以前
离开以前 2020-12-09 07:47

Why does itertools.permutations() return a list of characters or digits for each permutation, instead of just returning a string?

For example:

>&g         


        
4条回答
  •  时光说笑
    2020-12-09 08:42

    itertools.permutations() simply works this way. It takes an arbitrary iterable as an argument, and always returns an iterator yielding tuples. It doesn't (and shouldn't) special-case strings. To get a list of strings, you can always join the tuples yourself:

    list(map("".join, itertools.permutations('1234')))
    

提交回复
热议问题