Python Itertools permutation only letters and numbers

I need to get only permutations with letters and numbers (There can’t be a permutation. "A, B, C, D" I need it like this: "A, B, C, 1")

In short, permutations cannot contain only letters, not just numbers. There must be a combination of both.

My code is:

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 ask this question, let me know if there is a way to find out the size of the received file if I want to save it to a text file. I also want to know if there is a way to calculate how long it will take to get all permutation requests.

Thank you in advance.

+4
source share
6 answers

Using intersection sets:

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])
+7

.

(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))
+4
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)

3--3-, 2--2-. , t x . -, , 4. . , .

;

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")

:

...
...
...
('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
>>> 

:

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")

:

...
...
...
('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
>>> 
0

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 
0

ifilter, . , , True.

, , :

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

, , "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]

( lambdas, , . :

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

nums_that_end_in_6 = filter(predicate, range(100))

int, , ints. , True, ​​, . , :

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

/ , :

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))

, , :

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

, :

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

, , , , .

0

You can create the right combinations by combining non-empty combinations of 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 selected combinations (A + B, n) - select (A, n) - select (B, n), where A is the number of elements in a, B is the number of elements in b, and "select" is a binomial coefficient.

0
source

Source: https://habr.com/ru/post/1569648/


All Articles