简体   繁体   English

Python Itertools排列只有字母和数字

[英]Python Itertools permutations only letters and numbers

I need to get only the permutations that have letters and numbers (The permutation can not be. "A, B, C, D" I need it like this: "A, B, C, 1") 我只需要得到有字母和数字的排列(排列不可能。“A,B,C,D”我需要它像这样:“A,B,C,1”)

In short, the permutations can not contain only letters, not just numbers. 简而言之,排列不能只包含字母,而不仅仅是数字。 Must be a combination of both. 必须是两者的结合。

My code: 我的代码:

import itertools
print list(itertools.combinations([0,1,2,3,4,'a','b','c','d'], 4))

Then I get: 然后我得到:

[(0, 1, 2, 3), (0, 1, 2, 4), (0, 1, 2, 'a'), (0, 1, 2, 'b'), (0, 1, 2, 'c'), (0, 1, 2, 'd'), (0, 1, 3, 4), (0, 1, 3, 'a'), (0, 1, 3, 'b'), (0, 1, 3, 'c'), (0, 1, 3, 'd'), (0, 1, 4, 'a'), (0, 1, 4, 'b'), (0, 1, 4, 'c'), (0, 1, 4, 'd'), (0, 1, 'a', 'b'), (0, 1, 'a', 'c'), (0, 1, 'a', 'd'), (0, 1, 'b', 'c'), (0, 1, 'b', 'd'), (0, 1, 'c', 'd'), (0, 2, 3, 4), (0, 2, 3, 'a'), (0, 2, 3, 'b'), (0, 2, 3, 'c'), (0, 2, 3, 'd'), (0, 2, 4, 'a'), (0, 2, 4, 'b'), (0, 2, 4, 'c'), (0, 2, 4, 'd'), (0, 2, 'a', 'b'), (0, 2, 'a', 'c'), (0, 2, 'a', 'd'), (0, 2, 'b', 'c'), (0, 2, 'b', 'd'), (0, 2, 'c', 'd'), (0, 3, 4, 'a'), (0, 3, 4, 'b'), (0, 3, 4, 'c'), (0, 3, 4, 'd'), (0, 3, 'a', 'b'), (0, 3, 'a', 'c'), (0, 3, 'a', 'd'), (0, 3, 'b', 'c'), (0, 3, 'b', 'd'), (0, 3, 'c', 'd'), (0, 4, 'a', 'b'), (0, 4, 'a', 'c'), (0, 4, 'a', 'd'), (0, 4, 'b', 'c'), (0, 4, 'b', 'd'), (0, 4, 'c', 'd'), (0, 'a', 'b', 'c'), (0, 'a', 'b', 'd'), (0, 'a', 'c', 'd'), (0, 'b', 'c', 'd'), (1, 2, 3, 4), (1, 2, 3, 'a'), (1, 2, 3, 'b'), (1, 2, 3, 'c'), (1, 2, 3, 'd'), (1, 2, 4, 'a'), (1, 2, 4, 'b'), (1, 2, 4, 'c'), (1, 2, 4, 'd'), (1, 2, 'a', 'b'), (1, 2, 'a', 'c'), (1, 2, 'a', 'd'), (1, 2, 'b', 'c'), (1, 2, 'b', 'd'), (1, 2, 'c', 'd'), (1, 3, 4, 'a'), (1, 3, 4, 'b'), (1, 3, 4, 'c'), (1, 3, 4, 'd'), (1, 3, 'a', 'b'), (1, 3, 'a', 'c'), (1, 3, 'a', 'd'), (1, 3, 'b', 'c'), (1, 3, 'b', 'd'), (1, 3, 'c', 'd'), (1, 4, 'a', 'b'), (1, 4, 'a', 'c'), (1, 4, 'a', 'd'), (1, 4, 'b', 'c'), (1, 4, 'b', 'd'), (1, 4, 'c', 'd'), (1, 'a', 'b', 'c'), (1, 'a', 'b', 'd'), (1, 'a', 'c', 'd'), (1, 'b', 'c', 'd'), (2, 3, 4, 'a'), (2, 3, 4, 'b'), (2, 3, 4, 'c'), (2, 3, 4, 'd'), (2, 3, 'a', 'b'), (2, 3, 'a', 'c'), (2, 3, 'a', 'd'), (2, 3, 'b', 'c'), (2, 3, 'b', 'd'), (2, 3, 'c', 'd'), (2, 4, 'a', 'b'), (2, 4, 'a', 'c'), (2, 4, 'a', 'd'), (2, 4, 'b', 'c'), (2, 4, 'b', 'd'), (2, 4, 'c', 'd'), (2, 'a', 'b', 'c'), (2, 'a', 'b', 'd'), (2, 'a', 'c', 'd'), (2, 'b', 'c', 'd'), (3, 4, 'a', 'b'), (3, 4, 'a', 'c'), (3, 4, 'a', 'd'), (3, 4, 'b', 'c'), (3, 4, 'b', 'd'), (3, 4, 'c', 'd'), (3, 'a', 'b', 'c'), (3, 'a', 'b', 'd'), (3, 'a', 'c', 'd'), (3, 'b', 'c', 'd'), (4, 'a', 'b', 'c'), (4, 'a', 'b', 'd'), (4, 'a', 'c', 'd'), (4, 'b', 'c', 'd'), ('a', 'b', 'c', 'd')]

I pushing this question, let me know if there is a possible way of knowing the size of the file obtained, if I want to save to a text file. 我推问这个问题,让我知道是否有可能知道所获文件的大小,如果我想保存到文本文件。 I also want to know if there is any way to calculate how long it would take to get all permutations'm asking. 我还想知道是否有任何方法可以计算出所有排列要求的时间。

Thank you very much in advance. 非常感谢你提前。

Using set intersection: 使用集合交集:

import itertools
import string
numbers = set(range(10))
letters = set(string.ascii_letters)
print([x for x in itertools.combinations([0,1,2,3,4,'a','b','c','d'], 4)
       if set(x) & letters and set(x) & numbers])

Quite a naive solution.. 相当天真的解决方案..

(x
    for x in itertools.combinations([0,1,2,3,4,'a','b','c','d'], 4)
    if not all(c.isalpha() for c in x) and not all(c.isdigit() for c in x))
mylist=[]
for x in permutations([0,1,2,"a","b","c"],4):
    print (x)
    mylist.append(x)
for t in permutations([3,4,"c","d"]):
    print (t)
    mylist.append(t)

I split them 3 digit-3letter, 2digit-2letter. 我将它们拆分为3位数字3通道,2位数字2通道。 So compile t and x is your answer.First for loop cant contain only numbers or only letters because its permutations 4. Second one cant also same reason.So compile the results in a list, means your answer. 所以编译tx是你的答案。第一个循环不能只包含数字或只包含字母因为它的排列4.第二个也不能同样的原因。所以在列表中编译结果,意味着你的答案。

How to calculate the time; 如何计算时间;

import time
mylist=[]
start=time.time()
for x in permutations([0,1,2,"a","b","c"],4):
    print (x)
    mylist.append(x)
for t in permutations([3,4,"c","d"]):
    print (t)
    mylist.append(t)
end=time.time()
diff=end-start
print ("It took",diff,"seconds")

Output: 输出:

...
...
...
('c', 4, 'd', 3)
('c', 'd', 3, 4)
('c', 'd', 4, 3)
('d', 3, 4, 'c')
('d', 3, 'c', 4)
('d', 4, 3, 'c')
('d', 4, 'c', 3)
('d', 'c', 3, 4)
('d', 'c', 4, 3)
It took 0.5800008773803711 seconds
>>> 

Edit for how much permutations are there : 编辑有多少排列

from itertools import permutations
import time
mylist=[]
start=time.time()
for x in permutations([0,1,2,"a","b","c"],4):
    print (x)
    mylist.append(x)
for t in permutations([3,4,"c","d"]):
    print (t)
    mylist.append(t)
end=time.time()
diff=end-start
print ("There is {} permutations.".format(len(mylist)))
print ("It took",diff,"seconds")

Output: 输出:

...
...
...
('d', 3, 4, 'c')
('d', 3, 'c', 4)
('d', 4, 3, 'c')
('d', 4, 'c', 3)
('d', 'c', 3, 4)
('d', 'c', 4, 3)
There is 384 permutations.
It took 0.5120010375976562 seconds
>>> 

Update the valid_data_types_in_list() function to add additional constraints on your list. 更新valid_data_types_in_list()函数以在列表中添加其他约束。

def valid_data_types_in_list(input_list):
    str_type = False
    int_type = False
    for element in input_list:
        if type(element) == str:
            str_type = True
        if type(element) == int:
            int_type = True
    if str_type == int_type == True:
        return True
    return False

import itertools
output = [x for x in list(itertools.combinations([0,1,2,3,4,'a','b','c','d'], 4)) if valid_data_types_in_list(x)]
print output 

To use filter or ifilter, you must pass a predicate function and a sequence. 要使用filter或ifilter,必须传递谓词函数和序列。 The predicate function is evaluated once for each element in the sequence, and filter will only forward those elements which evaluate the predicate as True. 对序列中的每个元素计算谓词函数一次,过滤器只会将那些将谓词评估为True的元素转发。

For instance, let's say you wanted only the uppercase letters in a string: 例如,假设您只想要字符串中的大写字母:

>>> def is_upper(c):
...     return c.upper() == c
...
>>> uppers = filter(is_upper, "lsjdfLSKJDFLljsdlfkjLSFLDJ")
>>> print uppers
LSKJDFLLSFLDJ

Or if you only wanted the numbers that end in "6" in some list of numbers: 或者,如果您只想在某些数字列表中以“6”结尾的数字:

>>> nums_that_end_in_6 = filter(lambda n: n % 10 == 6, range(100))
>>> print nums_that_end_in_6
[6, 16, 26, 36, 46, 56, 66, 76, 86, 96]

(If you are not used to using lambdas, they are useful with filter when the logic is this simple. The lambda above is the same as: (如果您不习惯使用lambdas,当逻辑很简单时,它们对过滤器很有用。上面的lambda与下面的相同:

def predicate(n):
    return n % 10 == 6

nums_that_end_in_6 = filter(predicate, range(100))

In your situation, you are getting a sequence of combinations of letter and int values, and you only want those that are a mixture of letters and ints. 在你的情况下,你得到一系列字母和整数值的组合,你只需要那些字母和整数的混合。 So you will need to write a predicate function that returns True when given a sequence that is to your liking. 因此,您需要编写一个谓词函数,该函数在给定您喜欢的序列时返回True。 Using the set-based solution, your predicate might look like: 使用基于集合的解决方案,您的谓词可能如下所示:

ints = set(range(10))
letters = set(string.letters)
def predicate(seq):
    seqset = set(seq)
    return seqset & letters and seqset & ints

Using the any/all builtins, your predicate might look like: 使用any / all内置函数,您的谓词可能如下所示:

is_int = lambda x : isinstance(x, int)
is_str = lambda x : isinstance(x, str)
def predicate(seq):
    return not(all(is_int(item) for item in seq) or all(is_str(item) for item in seq))

Or if you just want to see if your sequence contains items of more than 1 type, you could write: 或者,如果您只想查看序列是否包含多于1种类型的项目,您可以编写:

def predicate(seq):
    return len(set(type(item) for item in seq))) > 1

To use any of these predicates, the form is the same: 要使用这些谓词中的任何一个,表单都是相同的:

values = list(string.letters) + range(10)
mixed_letter_int_combinations = filter(predicate, combinations(values, 4))

Then you can choose which predicate you prefer, based on performance, or readability, or whatever other criteria you like. 然后,您可以根据性能,可读性或您喜欢的任何其他条件选择您喜欢的谓词。

You can generate the proper combinations by combining non-empty combinations from the two sequences. 您可以通过组合两个序列中的非空组合来生成正确的组合。

import itertools

def combinations(a, b, n):
    for i in xrange(1, n):
        for ca in itertools.combinations(a, i):
            for cb in itertools.combinations(b, n-i):
                yield ca + cb

for r in combinations(list('abcd'), [1, 2, 3, 4], 4):
    print r

The number of combinations you get is choose(A+B, n) - choose(A, n) - choose(B, n), where A is the number of elements in a, B the number of elements in b, and "choose" is the binomial coefficient. 你得到的组合数量是选择(A + B,n) - 选择(A,n) - 选择(B,n),其中A是a中元素的数量,B是b中元素的数量,“选择“是二项式系数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM