Trying to increase item in a python list to get all options
Question:
I’m trying to take a list of numbers and increase them by a certain amount(1 in my example) up to a certain range(5 in my example) but for some reason it’s not doing it the way I was expecting:
Given this list:
[1, 5, 7, 9, 3]
I expect:
[1, 5, 7, 9, 3]
[2, 5, 7, 9, 3]
[3, 5, 7, 9, 3]
[4, 5, 7, 9, 3]
[5, 5, 7, 9, 3]
[1, 6, 7, 9, 3]
[2, 7, 7, 9, 3]
[3, 8, 7, 9, 3]
[4, 9, 7, 9, 3]
[5, 10, 7, 9, 3]
… and so on
Here’s my code:
# create list
list_of_nums = [1, 5, 7, 9, 3]
# variable of increase
increase_range = 5
range_per_increase = 1
for idx, i in enumerate(list_of_nums):
current_item = i
while current_item < i+increase_range:
copy_of_list = list_of_nums
current_item = current_item+range_per_increase
copy_of_list[idx] = current_item
print(copy_of_list)
break
Output:
[4, 7, 9, 11, 5]
[4, 8, 9, 11, 5]
[4, 8, 10, 11, 5]
[4, 8, 10, 12, 5]
[4, 8, 10, 12, 6]
What am I doing wrong or is there a easier way to get the output I want?
Update: I think my above example I get incrementals for each item then move on, but is it possible to get all combinations so the first item and the others item are incremented. I want all combinations for the lists within each numeric range while preserving the order.
Answers:
There is an easier way to get this. The built in itertools.product
method can do most of the work.
from itertools import product
list_of_nums = [1, 5, 7, 9, 3]
increase_range = 5
range_per_increase = 1
choices_list = []
for num in list_of_nums:
choices_list.append([])
currentAdder = num
while currentAdder < num + increase_range:
choices_list[-1].append(currentAdder)
currentAdder += range_per_increase
print(choices_list)
# Prints [[1, 2, 3, 4, 5], [5, 6, 7, 8, 9], ...] with all the options for each item.
print("nnn")
for nums in product(*choices_list): # Unpack choices_list into product
print(list(nums)) # Since each output should be a list, convert from tuple to list
I’m trying to take a list of numbers and increase them by a certain amount(1 in my example) up to a certain range(5 in my example) but for some reason it’s not doing it the way I was expecting:
Given this list:
[1, 5, 7, 9, 3]
I expect:
[1, 5, 7, 9, 3]
[2, 5, 7, 9, 3]
[3, 5, 7, 9, 3]
[4, 5, 7, 9, 3]
[5, 5, 7, 9, 3]
[1, 6, 7, 9, 3]
[2, 7, 7, 9, 3]
[3, 8, 7, 9, 3]
[4, 9, 7, 9, 3]
[5, 10, 7, 9, 3]
… and so on
Here’s my code:
# create list
list_of_nums = [1, 5, 7, 9, 3]
# variable of increase
increase_range = 5
range_per_increase = 1
for idx, i in enumerate(list_of_nums):
current_item = i
while current_item < i+increase_range:
copy_of_list = list_of_nums
current_item = current_item+range_per_increase
copy_of_list[idx] = current_item
print(copy_of_list)
break
Output:
[4, 7, 9, 11, 5]
[4, 8, 9, 11, 5]
[4, 8, 10, 11, 5]
[4, 8, 10, 12, 5]
[4, 8, 10, 12, 6]
What am I doing wrong or is there a easier way to get the output I want?
Update: I think my above example I get incrementals for each item then move on, but is it possible to get all combinations so the first item and the others item are incremented. I want all combinations for the lists within each numeric range while preserving the order.
There is an easier way to get this. The built in itertools.product
method can do most of the work.
from itertools import product
list_of_nums = [1, 5, 7, 9, 3]
increase_range = 5
range_per_increase = 1
choices_list = []
for num in list_of_nums:
choices_list.append([])
currentAdder = num
while currentAdder < num + increase_range:
choices_list[-1].append(currentAdder)
currentAdder += range_per_increase
print(choices_list)
# Prints [[1, 2, 3, 4, 5], [5, 6, 7, 8, 9], ...] with all the options for each item.
print("nnn")
for nums in product(*choices_list): # Unpack choices_list into product
print(list(nums)) # Since each output should be a list, convert from tuple to list