python - MemoryError while trying to using itertools.permutations, how use less memory? -


i'm loading text document containing random strings , i'm trying print every possible permutation of characters in string.

if notepad contains example:

123 abc 

i want output be

123,132,213,231,312,321 abc,acb,bac,bca,cab,cba 

the text file contains pretty large strings can see why getting memoryerror.

my first attempt used this:

import sys import itertools import math  def organize(to_print):     number_list = []     upper_list = []     lower_list = []     x in range(0,len(to_print)):         if str(to_print[x]).isdigit() true:             number_list.append(to_print[x])         elif to_print[x].isupper() true:             upper_list.append(to_print[x])         else:             lower_list.append(to_print[x])     master_list = number_list + upper_list + lower_list     return master_list  number = open(*file_dir*, 'r').readlines()  factorial = math.factorial(len(number)) complete_series = ''  x in range(0,factorial):     complete_string = ''.join((list(itertools.permutations(organize(number)))[x]))      complete_series += complete_string+',' edit_series = complete_series[:-1] print(edit_series) 

the reason def organize if have string 1ab need preorder number,uppercase,lowercase before start permutations.

i got memory error here: complete_string = ''.join((list(itertools.permutations(organize(number)))[x])) initial attempt bring out of for-loop.


my second attempt this:

import sys import itertools import math  def organize(to_print):     number_list = []     upper_list = []     lower_list = []     x in range(0,len(to_print)):         if str(to_print[x]).isdigit() true:             number_list.append(to_print[x])         elif to_print[x].isupper() true:             upper_list.append(to_print[x])         else:             lower_list.append(to_print[x])     master_list = number_list + upper_list + lower_list     return master_list  number = open(*file_dir*, 'r').readlines()  factorial = math.factorial(len(number)) complete_series = '' the_permutation = list(itertools.permutations(organize(number)))  x in range(0,factorial):     complete_string = ''.join((the_permutation[x]))      complete_series += complete_string+',' edit_series = complete_series[:-1] print(edit_series) 

but still getting memory error. don't need or want answer directly learning practice reduce inefficiencies, hints in right direction nice.


added 3rd attempt:

import sys import itertools import math  def organize(to_print):     number_list = []     upper_list = []     lower_list = []     x in range(0,len(to_print)):         if str(to_print[x]).isdigit() true:             number_list.append(to_print[x])         elif to_print[x].isupper() true:             upper_list.append(to_print[x])         else:             lower_list.append(to_print[x])     master_list = number_list + upper_list + lower_list     return master_list  number = open(*file_dir*, 'r').readlines()  factorial = math.factorial(len(number)) complete_series = '' the_permutation = itertools.permutations(organize(number)) x in itertools.islice(the_permutation,factorial):     complete_string = ''.join(next(the_permutation))     complete_series += complete_string+',' edit_series = complete_series[:-1] print(edit_series) 

don't call list, iterate on permutations:

the_permutation = itertools.permutations(organize(number))  x in the_permutation:     complete_string = ''.join(the_permutation) 

list(itertools.permutations(organize(number))) stores permutations in memory store permutations in string in loop, there no guarantee able store data using approach depending on how data in the_permutation

if want amount of permutations can call next om permutations object:

the_permutation = itertools.permutations(organize(number)) x in range(factorial):     complete_string = ''.join(next(the_permutation)) 

or use itertools.islice:

for x in itertools.islice(the_permutation,factorial):     complete_string = ''.join(next(the_permutation)) 

Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -