I have a few related questions.
If I have a list l =[1,2,3,4,5,6,7,8]. I want to find max/min values of every two, four, six, and eight elements.
ex, max([1,2]); max([1,2,3,4])...
How can I write a for loop to print all those sub lists? Then put the all max values into a new list.
Thank you so much for the answers. They really help.
update:
New situation-- How about max/min values of max([1,2]), max([3,4,5,6])...every other 2, 4, 6 values.
Thanks ahead.
CodePudding user response:
I don't use comprehension to be simple to follow:
d = {}
l =[1, 2, 3, 4, 5, 6, 7, 8]
for j in [2, 4, 6, 8]:
print(f"[Max for {j} elements")
d[j] = []
for i in range(0, len(l), j):
r = max(l[i:i j])
print(f"{l[i:i j]}: {r}")
d[j].append(r)
print()
print(d)
Output:
[Max for 2 elements
[1, 2]: 2
[3, 4]: 4
[5, 6]: 6
[7, 8]: 8
[Max for 4 elements
[1, 2, 3, 4]: 4
[5, 6, 7, 8]: 8
[Max for 6 elements
[1, 2, 3, 4, 5, 6]: 6
[7, 8]: 8
[Max for 8 elements
[1, 2, 3, 4, 5, 6, 7, 8]: 8
{2: [2, 4, 6, 8], 4: [4, 8], 6: [6, 8], 8: [8]}
CodePudding user response:
you can use the following iteration
l = [1, 2, 3, 4, 5, 6, 7, 8]
gap=2
maximums = []
sublists = []
index_of_last_element = gap
while index_of_last_element < len(l) gap:
maximums.append(max(l[0:index_of_last_element]))
sublists.append(l[0:index_of_last_element])
index_of_last_element = gap
print(f"maximum values:{maximums}\nsublists:{sublists}")
this gives the following output:
maximum values:[2, 4, 6, 8]
sublists:[[1, 2], [1, 2, 3, 4], [1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6, 7, 8]]
CodePudding user response:
Not absolutely certain what you need but let's try to do it using a step-by-step approach:
lst = [1, 2, 3, 4, 5, 6, 7, 8]
maxima = []
for e in range(2, len(lst) 1, 2):
sub = lst[:e]
min_ = min(sub)
max_ = max(sub)
print(f'min for {sub} = {min_}')
print(f'max for {sub} = {max_}')
maxima.append(max_)
print(f'maxima = {maxima}')
Output:
min for [1, 2] = 1
max for [1, 2] = 2
min for [1, 2, 3, 4] = 1
max for [1, 2, 3, 4] = 4
min for [1, 2, 3, 4, 5, 6] = 1
max for [1, 2, 3, 4, 5, 6] = 6
min for [1, 2, 3, 4, 5, 6, 7, 8] = 1
max for [1, 2, 3, 4, 5, 6, 7, 8] = 8
maxima = [2, 4, 6, 8]
CodePudding user response:
It is not clear whether you want to get the cumulated min/max.
If this is the case, you only need to compute the min/max on the new elements and the last result:
MIN = [min(l[:2])]
MAX = [max(l[:2])]
for i in range(2,len(l),2):
MIN.append(min(MIN[-1],*l[i:i 2]))
MAX.append(max(MAX[-1],*l[i:i 2]))
MIN, MAX
output:
# cumul. MIN cumul. MAX
([1, 1, 1, 1], [2, 4, 6, 8])
with debug info
MIN = [float('inf')]
MAX = [float('-inf')]
for i in range(0,len(l),2):
MIN.append(min(MIN[-1],*l[i:i 2]))
MAX.append(max(MAX[-1],*l[i:i 2]))
print(f'l = {l[:i 2]}: min {MIN[-1]}, max{MAX[-1]}')
MIN.pop(0); MAX.pop(0);
output:
l = [1, 2]: min 1, max2
l = [1, 2, 3, 4]: min 1, max4
l = [1, 2, 3, 4, 5, 6]: min 1, max6
l = [1, 2, 3, 4, 5, 6, 7, 8]: min 1, max8
CodePudding user response:
You can create a loop with help from the range function.
l = list(range(10)) # input list
max_list = []
size = 2
# range(start, stop, step)
for i in range(size, len(l) size, size):
print(l[:i])
max_list.append(max(l[:i]))
print(max_list)
output:
[0, 1]
[0, 1, 2, 3]
[0, 1, 2, 3, 4, 5]
[0, 1, 2, 3, 4, 5, 6, 7]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 3, 5, 7, 9]
