Find the min and max of each nested list in a list
up vote
0
down vote
favorite
I am trying to find the min and max of each list in a nested list, and the index of when the min or max occurred:
So that, for example:
l=[[5,6,7][6,10,9,6][2,3,1]]
Becomes:
maxl=[7,10,3]
indexl=[2,1,2]
I've tried this and it seems to get me the max list (no indexes yet) but not for min- does anyone know how best to do this?
maxHap=
for subL in happiness1:
maxHap.append(max(subL))
print(maxHap)
minHap=
for subL in happiness1:
minHap.append(min(subL))
print(minHap)
Thank you from a newbie
python
add a comment |
up vote
0
down vote
favorite
I am trying to find the min and max of each list in a nested list, and the index of when the min or max occurred:
So that, for example:
l=[[5,6,7][6,10,9,6][2,3,1]]
Becomes:
maxl=[7,10,3]
indexl=[2,1,2]
I've tried this and it seems to get me the max list (no indexes yet) but not for min- does anyone know how best to do this?
maxHap=
for subL in happiness1:
maxHap.append(max(subL))
print(maxHap)
minHap=
for subL in happiness1:
minHap.append(min(subL))
print(minHap)
Thank you from a newbie
python
Are you open to using an additional dependency like numpy?
– Mad Physicist
Nov 11 at 10:27
1
This should actually work. What results do you get?
– MrLeeh
Nov 11 at 10:28
The max list is filled but the min list just returns a list of spaces, which is incorrect
– I. Evans
Nov 11 at 10:29
Got it working! But thanks
– I. Evans
Nov 11 at 10:32
Why is the expected output for "l=[[5,6,7][6,10,9,6][2,3,1]]", "maxl=[7,10,3] and indexl=[2,1,2]"? Shouldn't it be (7, 10, 3) and (2, 1, 1) instead?
– quant
Nov 11 at 10:33
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am trying to find the min and max of each list in a nested list, and the index of when the min or max occurred:
So that, for example:
l=[[5,6,7][6,10,9,6][2,3,1]]
Becomes:
maxl=[7,10,3]
indexl=[2,1,2]
I've tried this and it seems to get me the max list (no indexes yet) but not for min- does anyone know how best to do this?
maxHap=
for subL in happiness1:
maxHap.append(max(subL))
print(maxHap)
minHap=
for subL in happiness1:
minHap.append(min(subL))
print(minHap)
Thank you from a newbie
python
I am trying to find the min and max of each list in a nested list, and the index of when the min or max occurred:
So that, for example:
l=[[5,6,7][6,10,9,6][2,3,1]]
Becomes:
maxl=[7,10,3]
indexl=[2,1,2]
I've tried this and it seems to get me the max list (no indexes yet) but not for min- does anyone know how best to do this?
maxHap=
for subL in happiness1:
maxHap.append(max(subL))
print(maxHap)
minHap=
for subL in happiness1:
minHap.append(min(subL))
print(minHap)
Thank you from a newbie
python
python
edited Nov 11 at 10:26
MrLeeh
2,56831528
2,56831528
asked Nov 11 at 10:24
I. Evans
483
483
Are you open to using an additional dependency like numpy?
– Mad Physicist
Nov 11 at 10:27
1
This should actually work. What results do you get?
– MrLeeh
Nov 11 at 10:28
The max list is filled but the min list just returns a list of spaces, which is incorrect
– I. Evans
Nov 11 at 10:29
Got it working! But thanks
– I. Evans
Nov 11 at 10:32
Why is the expected output for "l=[[5,6,7][6,10,9,6][2,3,1]]", "maxl=[7,10,3] and indexl=[2,1,2]"? Shouldn't it be (7, 10, 3) and (2, 1, 1) instead?
– quant
Nov 11 at 10:33
add a comment |
Are you open to using an additional dependency like numpy?
– Mad Physicist
Nov 11 at 10:27
1
This should actually work. What results do you get?
– MrLeeh
Nov 11 at 10:28
The max list is filled but the min list just returns a list of spaces, which is incorrect
– I. Evans
Nov 11 at 10:29
Got it working! But thanks
– I. Evans
Nov 11 at 10:32
Why is the expected output for "l=[[5,6,7][6,10,9,6][2,3,1]]", "maxl=[7,10,3] and indexl=[2,1,2]"? Shouldn't it be (7, 10, 3) and (2, 1, 1) instead?
– quant
Nov 11 at 10:33
Are you open to using an additional dependency like numpy?
– Mad Physicist
Nov 11 at 10:27
Are you open to using an additional dependency like numpy?
– Mad Physicist
Nov 11 at 10:27
1
1
This should actually work. What results do you get?
– MrLeeh
Nov 11 at 10:28
This should actually work. What results do you get?
– MrLeeh
Nov 11 at 10:28
The max list is filled but the min list just returns a list of spaces, which is incorrect
– I. Evans
Nov 11 at 10:29
The max list is filled but the min list just returns a list of spaces, which is incorrect
– I. Evans
Nov 11 at 10:29
Got it working! But thanks
– I. Evans
Nov 11 at 10:32
Got it working! But thanks
– I. Evans
Nov 11 at 10:32
Why is the expected output for "l=[[5,6,7][6,10,9,6][2,3,1]]", "maxl=[7,10,3] and indexl=[2,1,2]"? Shouldn't it be (7, 10, 3) and (2, 1, 1) instead?
– quant
Nov 11 at 10:33
Why is the expected output for "l=[[5,6,7][6,10,9,6][2,3,1]]", "maxl=[7,10,3] and indexl=[2,1,2]"? Shouldn't it be (7, 10, 3) and (2, 1, 1) instead?
– quant
Nov 11 at 10:33
add a comment |
3 Answers
3
active
oldest
votes
up vote
1
down vote
accepted
You can use the method index
to get the index of a value of a list:
https://docs.python.org/3/tutorial/datastructures.html
maxl =
indexl =
for inner_list in l:
max_value = max(inner_list)
maxl.append(max(max_value))
indexl.append(inner_list.index(max_value)
add a comment |
up vote
2
down vote
Other option:
l=[[5,6,7],[6,10,9,6],[2,3,1]]
maxs = [ max(s) for s in l ] #=> [7, 10, 3]
max_idxs = [ s.index(max(s)) for s in l ] #=> [2, 1, 1]
mins = [ min(s) for s in l ] #=> [5, 6, 1]
mins_idxs = [ s.index(min(s)) for s in l ] #=> [0, 0, 2]
Or you can store the results into an array of dict, one liner:
mapp = map(lambda x: {'max': max(x), 'max_idxs': x.index(max(x)), 'min': min(x), 'min_idxs': x.index(min(x)) }, l)
for k in mapp:
print(k)
#=> {'max': 7, 'max_idxs': 2, 'min': 5, 'min_idxs': 0}
#=> {'max': 10, 'max_idxs': 1, 'min': 6, 'min_idxs': 0}
#=> {'max': 3, 'max_idxs': 1, 'min': 1, 'min_idxs': 2}
add a comment |
up vote
1
down vote
You can use the following one-liner:
l=[[5,6,7],[6,10,9,6],[2,3,1]]
maxL, index = zip(*[(max(subList), subList.index(max(subList))) for subList in l])
print(maxL) # will output (7, 10, 3)
print(index) # will output (2, 1, 1)
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
You can use the method index
to get the index of a value of a list:
https://docs.python.org/3/tutorial/datastructures.html
maxl =
indexl =
for inner_list in l:
max_value = max(inner_list)
maxl.append(max(max_value))
indexl.append(inner_list.index(max_value)
add a comment |
up vote
1
down vote
accepted
You can use the method index
to get the index of a value of a list:
https://docs.python.org/3/tutorial/datastructures.html
maxl =
indexl =
for inner_list in l:
max_value = max(inner_list)
maxl.append(max(max_value))
indexl.append(inner_list.index(max_value)
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
You can use the method index
to get the index of a value of a list:
https://docs.python.org/3/tutorial/datastructures.html
maxl =
indexl =
for inner_list in l:
max_value = max(inner_list)
maxl.append(max(max_value))
indexl.append(inner_list.index(max_value)
You can use the method index
to get the index of a value of a list:
https://docs.python.org/3/tutorial/datastructures.html
maxl =
indexl =
for inner_list in l:
max_value = max(inner_list)
maxl.append(max(max_value))
indexl.append(inner_list.index(max_value)
answered Nov 11 at 10:35
ostcar
763
763
add a comment |
add a comment |
up vote
2
down vote
Other option:
l=[[5,6,7],[6,10,9,6],[2,3,1]]
maxs = [ max(s) for s in l ] #=> [7, 10, 3]
max_idxs = [ s.index(max(s)) for s in l ] #=> [2, 1, 1]
mins = [ min(s) for s in l ] #=> [5, 6, 1]
mins_idxs = [ s.index(min(s)) for s in l ] #=> [0, 0, 2]
Or you can store the results into an array of dict, one liner:
mapp = map(lambda x: {'max': max(x), 'max_idxs': x.index(max(x)), 'min': min(x), 'min_idxs': x.index(min(x)) }, l)
for k in mapp:
print(k)
#=> {'max': 7, 'max_idxs': 2, 'min': 5, 'min_idxs': 0}
#=> {'max': 10, 'max_idxs': 1, 'min': 6, 'min_idxs': 0}
#=> {'max': 3, 'max_idxs': 1, 'min': 1, 'min_idxs': 2}
add a comment |
up vote
2
down vote
Other option:
l=[[5,6,7],[6,10,9,6],[2,3,1]]
maxs = [ max(s) for s in l ] #=> [7, 10, 3]
max_idxs = [ s.index(max(s)) for s in l ] #=> [2, 1, 1]
mins = [ min(s) for s in l ] #=> [5, 6, 1]
mins_idxs = [ s.index(min(s)) for s in l ] #=> [0, 0, 2]
Or you can store the results into an array of dict, one liner:
mapp = map(lambda x: {'max': max(x), 'max_idxs': x.index(max(x)), 'min': min(x), 'min_idxs': x.index(min(x)) }, l)
for k in mapp:
print(k)
#=> {'max': 7, 'max_idxs': 2, 'min': 5, 'min_idxs': 0}
#=> {'max': 10, 'max_idxs': 1, 'min': 6, 'min_idxs': 0}
#=> {'max': 3, 'max_idxs': 1, 'min': 1, 'min_idxs': 2}
add a comment |
up vote
2
down vote
up vote
2
down vote
Other option:
l=[[5,6,7],[6,10,9,6],[2,3,1]]
maxs = [ max(s) for s in l ] #=> [7, 10, 3]
max_idxs = [ s.index(max(s)) for s in l ] #=> [2, 1, 1]
mins = [ min(s) for s in l ] #=> [5, 6, 1]
mins_idxs = [ s.index(min(s)) for s in l ] #=> [0, 0, 2]
Or you can store the results into an array of dict, one liner:
mapp = map(lambda x: {'max': max(x), 'max_idxs': x.index(max(x)), 'min': min(x), 'min_idxs': x.index(min(x)) }, l)
for k in mapp:
print(k)
#=> {'max': 7, 'max_idxs': 2, 'min': 5, 'min_idxs': 0}
#=> {'max': 10, 'max_idxs': 1, 'min': 6, 'min_idxs': 0}
#=> {'max': 3, 'max_idxs': 1, 'min': 1, 'min_idxs': 2}
Other option:
l=[[5,6,7],[6,10,9,6],[2,3,1]]
maxs = [ max(s) for s in l ] #=> [7, 10, 3]
max_idxs = [ s.index(max(s)) for s in l ] #=> [2, 1, 1]
mins = [ min(s) for s in l ] #=> [5, 6, 1]
mins_idxs = [ s.index(min(s)) for s in l ] #=> [0, 0, 2]
Or you can store the results into an array of dict, one liner:
mapp = map(lambda x: {'max': max(x), 'max_idxs': x.index(max(x)), 'min': min(x), 'min_idxs': x.index(min(x)) }, l)
for k in mapp:
print(k)
#=> {'max': 7, 'max_idxs': 2, 'min': 5, 'min_idxs': 0}
#=> {'max': 10, 'max_idxs': 1, 'min': 6, 'min_idxs': 0}
#=> {'max': 3, 'max_idxs': 1, 'min': 1, 'min_idxs': 2}
edited Nov 11 at 11:26
answered Nov 11 at 10:53
iGian
2,7242622
2,7242622
add a comment |
add a comment |
up vote
1
down vote
You can use the following one-liner:
l=[[5,6,7],[6,10,9,6],[2,3,1]]
maxL, index = zip(*[(max(subList), subList.index(max(subList))) for subList in l])
print(maxL) # will output (7, 10, 3)
print(index) # will output (2, 1, 1)
add a comment |
up vote
1
down vote
You can use the following one-liner:
l=[[5,6,7],[6,10,9,6],[2,3,1]]
maxL, index = zip(*[(max(subList), subList.index(max(subList))) for subList in l])
print(maxL) # will output (7, 10, 3)
print(index) # will output (2, 1, 1)
add a comment |
up vote
1
down vote
up vote
1
down vote
You can use the following one-liner:
l=[[5,6,7],[6,10,9,6],[2,3,1]]
maxL, index = zip(*[(max(subList), subList.index(max(subList))) for subList in l])
print(maxL) # will output (7, 10, 3)
print(index) # will output (2, 1, 1)
You can use the following one-liner:
l=[[5,6,7],[6,10,9,6],[2,3,1]]
maxL, index = zip(*[(max(subList), subList.index(max(subList))) for subList in l])
print(maxL) # will output (7, 10, 3)
print(index) # will output (2, 1, 1)
answered Nov 11 at 10:35
quant
1,58411526
1,58411526
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53247807%2ffind-the-min-and-max-of-each-nested-list-in-a-list%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Are you open to using an additional dependency like numpy?
– Mad Physicist
Nov 11 at 10:27
1
This should actually work. What results do you get?
– MrLeeh
Nov 11 at 10:28
The max list is filled but the min list just returns a list of spaces, which is incorrect
– I. Evans
Nov 11 at 10:29
Got it working! But thanks
– I. Evans
Nov 11 at 10:32
Why is the expected output for "l=[[5,6,7][6,10,9,6][2,3,1]]", "maxl=[7,10,3] and indexl=[2,1,2]"? Shouldn't it be (7, 10, 3) and (2, 1, 1) instead?
– quant
Nov 11 at 10:33