我有以下代码正常工作,然后我已更新到新版本的Python。我不明白的是为什么它最多只能工作10种组合。如果您将变量 combination_cap 和 range_to 设置为高于10,程序就会挂起。没错。什么都没有。
这是我的代码:
import itertools
import subprocess
import os
if os.name == 'nt':
def clear_console():
subprocess.call("cls", shell=True)
return
else:
def clear_console():
subprocess.call("clear", shell=True)
return
def generateCombinationRange():
combination_cap = 10
range_from = 1
range_to = 10
combination_list = list(itertools.combinations(range(1, int(combination_cap)+1), 6))
combination_list_lenght = len(combination_list)
output_file = open('F' + str(range_from) + 'T' + str(range_to) + 'OF' + str(combination_list_lenght) + '_comibnations.csv', 'w')
index = 0
for each_combination in combination_list:
index += 1
#print(''.join(str(each_combination)) + '\n')
if index >= range_from:
output_file.write(str(index) + ', '
+ str(each_combination[0]) + ', '
+ str(each_combination[1]) + ', '
+ str(each_combination[2]) + ', '
+ str(each_combination[3]) + ', '
+ str(each_combination[4]) + ', '
+ str(each_combination[5]) + '\n'
)
if index >= range_to:
output_file.close()
break
output_file.close()
generateCombinationRange()
答案 0 :(得分:1)
问题是行list(itertools.combinations(range(1, int(combination_cap)+1), 6))
。如果数字很高,那么你就会产生一个巨大的列表,它会扼杀你所有的RAM。你应该做的是将它用作生成器,因此它只生成你实际使用的数字。此版本适用于combination_cap
和range_to
的任何值集,因为它一次只能在内存中存储一个组合。
def generate_combination_range():
combination_cap = 100
range_from = 1
range_to = 100
output_file = open(
'F' + str(range_from) + 'T' + str(range_to) + 'OF' + '_comibnations.csv', 'w')
index = 0
for each_combination in itertools.combinations(range(1, int(combination_cap) + 1), 6):
index += 1
# print(''.join(str(each_combination)) + '\n')
if index >= range_from:
output_file.write(str(index) + ', '
+ str(each_combination[0]) + ', '
+ str(each_combination[1]) + ', '
+ str(each_combination[2]) + ', '
+ str(each_combination[3]) + ', '
+ str(each_combination[4]) + ', '
+ str(each_combination[5]) + '\n'
)
if index >= range_to:
output_file.close()
break
output_file.close()
另外,样式注释,它通常优先使用enumerate
而不是手动管理索引。 for index, each_combination in enumerate(itertools.combinations(range(1, int(combination_cap) + 1), 6)):
答案 1 :(得分:1)
您收到内存错误:
Traceback (most recent call last):
File "C:\zila\zilawww\t.py", line 54, in <module>
generateCombinationRange()
File "C:\zila\zilawww\t.py", line 25, in generateCombinationRange
combination_list = list(itertools.combinations((range(1, int(combination_cap)+1)), 6))
MemoryError
删除“列表”
combination_list = list(itertools.combinations(range(1, int(combination_cap)+1), 6))
使它成为一个发生器并保存内存(虽然你不能在len上使用它)
combination_gen = (itertools.combinations(range(1, int(combination_cap)+1), 6))
答案 2 :(得分:0)
我花时间将您的答案合并到我的代码中,现在我已经完成了产品。我想与所有可能感兴趣的人分享完整的代码。
import subprocess
import os
import itertools
import math
if os.name == 'nt':
def clear_console():
subprocess.call("cls", shell=True)
return
else:
def clear_console():
subprocess.call("clear", shell=True)
return
#----------------------------------------------------------------------
def main_menu():
while True:
main_menu_selected_option = 0
# MAIN MENU - PARENT
while main_menu_selected_option not in range(1, 4):
try:
clear_console()
print ('')
print (' Combination Generator v1.0 - Menu')
main_menu_selected_option = int(input('''
1. All indexed combinations for combination cap.
2. All Prime indexed combinations for combination cap.
3. Extract range of indexed combinations for combination cap.
4. Extract Prime range of indexed combinations for combination cap.
Enter Option: '''))
except ValueError:
#print ('Not an integer!')
#clear_console()
#print ('')
#print (' Lotery Systems - Menu')
continue
else:
if main_menu_selected_option == 1:
menu_1()
else:
if main_menu_selected_option == 2:
menu_2()
else:
if main_menu_selected_option == 3:
menu_3()
else:
if main_menu_selected_option == 4:
menu_4()
else:
clear_console()
main_menu()
#----------------------------------------------------------------------
def menu_1():
combination_cap = 0
# 1ST MENU = CHILD.
while int(combination_cap) not in range(1, 42+1):
combination_cap = int(input('''
Enter combination Cap: '''))
print ('')
print (' Generating combinations for combination cap.')
print (' Please wait...')
menu_1_execute(combination_cap)
#----------------------------------------------------------------------
def menu_2():
combination_cap = 0
# 2ND MENU = CHILD.
while int(combination_cap) not in range(1, 42+1):
combination_cap = int(input('''
Enter combination Cap: '''))
print ('')
print (' Generating combinations for combination cap.')
print (' Please wait...')
menu_2_execute(combination_cap)
#----------------------------------------------------------------------
def menu_3():
combination_cap = 0
range_from = 0
range_to = 0
# 3RD MENU = CHILD.
while int(combination_cap) not in range(1, 42+1):
try:
combination_cap = int(input('''
Enter combination Cap: '''))
range_from = int(input('''
Enter from: '''))
range_to = int(input('''
Enter to: '''))
except ValueError:
#print ('Not an integer!')
#clear_console()
#print ('')
#print (' Lotery Systems - Menu')
continue
else:
if range_from < range_to:
print ('')
print (' Generating range of combinations for combination cap.')
print (' Please wait...')
menu_3_execute(combination_cap, range_from, range_to)
else:
continue
#----------------------------------------------------------------------
def menu_4():
combination_cap = 0
range_from = 0
range_to = 0
# 3RD MENU = CHILD.
while int(combination_cap) not in range(1, 42+1):
try:
combination_cap = int(input('''
Enter combination Cap: '''))
range_from = int(input('''
Enter from: '''))
range_to = int(input('''
Enter to: '''))
except ValueError:
#print ('Not an integer!')
#clear_console()
#print ('')
#print (' Lotery Systems - Menu')
continue
else:
if range_from < range_to:
print ('')
print (' Generating Prime range of combinations for combination cap.')
print (' Please wait...')
menu_4_execute(combination_cap, range_from, range_to)
else:
continue
#----------------------------------------------------------------------
def menu_1_execute(combination_cap):
index = 0
output_file = open('all_indexed_combinations.csv', 'w')
combination_generator = (itertools.combinations(range(1, int(combination_cap) + 1), 6))
for each_combination in combination_generator:
index += 1
output_file.write(str(index) + ', '
+ str(each_combination[0]) + ', '
+ str(each_combination[1]) + ', '
+ str(each_combination[2]) + ', '
+ str(each_combination[3]) + ', '
+ str(each_combination[4]) + ', '
+ str(each_combination[5]) + '\n'
)
#Stamp Combination Cap
output_file.write('Cap: ' + ', ' + str(combination_cap) + '\n')
#Stamp Combinations Total
output_file.write('Total: ' + ', ' + str(index) + '\n')
output_file.close()
os.rename('all_indexed_combinations.csv',str(index) + '_combinations.csv')
#----------------------------------------------------------------------
def menu_2_execute(combination_cap):
index = 0
prime_index = 0
output_file = open('all_prime_indexed_combinations.csv', 'w')
combination_generator = (itertools.combinations(range(1, int(combination_cap) + 1), 6))
for each_combination in combination_generator:
index += 1
if isPrime(index):
prime_index += 1
output_file.write(str(index) + ', '
+ str(each_combination[0]) + ', '
+ str(each_combination[1]) + ', '
+ str(each_combination[2]) + ', '
+ str(each_combination[3]) + ', '
+ str(each_combination[4]) + ', '
+ str(each_combination[5]) + '\n'
)
#Stamp Combination Cap
output_file.write('Cap: ' + ', ' + str(combination_cap) + '\n')
#Stamp Prime Combinations Total
output_file.write('Total: ' + ', ' + str(prime_index) + '\n')
output_file.close()
os.rename('all_prime_indexed_combinations.csv',str(prime_index) + '_prime_combinations.csv')
#----------------------------------------------------------------------
def menu_3_execute(combination_cap, range_from, range_to):
index = 0
output_file = open('Range_F' + str(range_from) + 'T' + str(range_to) + '_combinations.csv', 'w')
combination_generator = (itertools.combinations(range(1, int(combination_cap) + 1), 6))
for each_combination in combination_generator:
index += 1
if index >= range_from:
output_file.write(str(index) + ', '
+ str(each_combination[0]) + ', '
+ str(each_combination[1]) + ', '
+ str(each_combination[2]) + ', '
+ str(each_combination[3]) + ', '
+ str(each_combination[4]) + ', '
+ str(each_combination[5]) + '\n'
)
if index >= range_to:
output_file.close()
break
output_file.close()
#----------------------------------------------------------------------
def menu_4_execute(combination_cap, range_from, range_to):
index = 0
output_file = open('Prime_Range_F' + str(range_from) + 'T' + str(range_to) + '_combinations.csv', 'w')
combination_generator = (itertools.combinations(range(1, int(combination_cap) + 1), 6))
for each_combination in combination_generator:
index += 1
if index >= range_from:
if isPrime(index):
output_file.write(str(index) + ', '
+ str(each_combination[0]) + ', '
+ str(each_combination[1]) + ', '
+ str(each_combination[2]) + ', '
+ str(each_combination[3]) + ', '
+ str(each_combination[4]) + ', '
+ str(each_combination[5]) + '\n'
)
if index >= range_to:
output_file.close()
break
output_file.close()
#----------------------------------------------------------------------
def isPrime(n):
if n < 2:
return False
i = 2
for i in range(2, int(math.sqrt(n) + 1)):
if (n % i == 0):
return False
return True
#----------------------------------------------------------------------
#Initialize Application
main_menu()