Finding index of highest element from a list of list
This is the list i am dealing with.
Value:
[[0.25, 0.0, 0.0, 0.0, 0.0, 0.0, 0.94, 0.0, 0.0, 0.63, 0.0],
[0.51, 0.51],
[0.54, 0.54, 0.0, 0.0,0.63, 0.0, 0.51, 0.54, 0.51, 1.0, 0.51],
[0.81,0.05, 0.13, 0.7, 0.02]]
I have tried to find the highest value belongs to which list. Here the highest value is 1 and its in third list. ie, index 2.
How to get this?
maxval=
maxval1=
for i in range(0,len(Value)):
maxval1.append(max(Value[i]))
maxval.append(max(maxval1))
maxval1
Out[220]: [0.94, 0.51, 1.0, 0.81]
maxval
Out[221]: [1.0]
index=
index1=
for i in range(0,len(Value)):
index1.append(Value[i].index(maxval))
gives error: ValueError: [1.0] is not in list
python-3.x list
add a comment |
This is the list i am dealing with.
Value:
[[0.25, 0.0, 0.0, 0.0, 0.0, 0.0, 0.94, 0.0, 0.0, 0.63, 0.0],
[0.51, 0.51],
[0.54, 0.54, 0.0, 0.0,0.63, 0.0, 0.51, 0.54, 0.51, 1.0, 0.51],
[0.81,0.05, 0.13, 0.7, 0.02]]
I have tried to find the highest value belongs to which list. Here the highest value is 1 and its in third list. ie, index 2.
How to get this?
maxval=
maxval1=
for i in range(0,len(Value)):
maxval1.append(max(Value[i]))
maxval.append(max(maxval1))
maxval1
Out[220]: [0.94, 0.51, 1.0, 0.81]
maxval
Out[221]: [1.0]
index=
index1=
for i in range(0,len(Value)):
index1.append(Value[i].index(maxval))
gives error: ValueError: [1.0] is not in list
python-3.x list
add a comment |
This is the list i am dealing with.
Value:
[[0.25, 0.0, 0.0, 0.0, 0.0, 0.0, 0.94, 0.0, 0.0, 0.63, 0.0],
[0.51, 0.51],
[0.54, 0.54, 0.0, 0.0,0.63, 0.0, 0.51, 0.54, 0.51, 1.0, 0.51],
[0.81,0.05, 0.13, 0.7, 0.02]]
I have tried to find the highest value belongs to which list. Here the highest value is 1 and its in third list. ie, index 2.
How to get this?
maxval=
maxval1=
for i in range(0,len(Value)):
maxval1.append(max(Value[i]))
maxval.append(max(maxval1))
maxval1
Out[220]: [0.94, 0.51, 1.0, 0.81]
maxval
Out[221]: [1.0]
index=
index1=
for i in range(0,len(Value)):
index1.append(Value[i].index(maxval))
gives error: ValueError: [1.0] is not in list
python-3.x list
This is the list i am dealing with.
Value:
[[0.25, 0.0, 0.0, 0.0, 0.0, 0.0, 0.94, 0.0, 0.0, 0.63, 0.0],
[0.51, 0.51],
[0.54, 0.54, 0.0, 0.0,0.63, 0.0, 0.51, 0.54, 0.51, 1.0, 0.51],
[0.81,0.05, 0.13, 0.7, 0.02]]
I have tried to find the highest value belongs to which list. Here the highest value is 1 and its in third list. ie, index 2.
How to get this?
maxval=
maxval1=
for i in range(0,len(Value)):
maxval1.append(max(Value[i]))
maxval.append(max(maxval1))
maxval1
Out[220]: [0.94, 0.51, 1.0, 0.81]
maxval
Out[221]: [1.0]
index=
index1=
for i in range(0,len(Value)):
index1.append(Value[i].index(maxval))
gives error: ValueError: [1.0] is not in list
python-3.x list
python-3.x list
asked Nov 15 '18 at 6:19
91133039113303
335112
335112
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
You are searching for max value in each list, but it is present in third list only, this will throw ValueError
for all other lists unless max value is present in first list itself.
It can be done simply like this
max_list = list()
for i, sub_list in enumerate(Value):
max_list.append((max(sub_list), i))
index_of_max = max(max_list)[1]
index_of_max
provides index of list containing max value.
add a comment |
You can do this as kindof one-liner as well:
data = [[0.25, 0.0, 0.0, 0.0, 0.0, 0.0, 0.94, 0.0, 0.0, 0.63, 0.0],
[0.51, 0.51],
[0.54, 0.54, 0.0, 0.0,0.63, 0.0, 0.51, 0.54, 0.51, 1.0, 0.51],
[0.81,0.05, 0.13, 0.7, 0.02]]
# iL === innerList, abbreviated for 79 chars line width
max_idx, max_value = max( enumerate(max(iL) for iL in data), key = lambda x:x[1])
print(max_idx)
Output:
2 # max_value == 1, but not printed
The trick is to get the max(..)
of each inner list, enumerate()
those and use another max(iterable, key=...)
that has a key
that selects the highest value (not position) of the enumerate tuple (position,value)
.
Advantage - its using generators:
you do not create any intermediary lists but soley work with generators - for big lists this is far more memory friendly then then creating a list like infiQuanta does.
add a comment |
You can solve this using the Python's library pandas
. Solution becomes really simple:
Create a dataframe
(df) from your list:
In [2040]: df = pd.DataFrame(Value)
In [2041]: df
Out[2041]:
0 1 2 3 4 5 6 7 8 9 10
0 0.25 0.00 0.00 0.0 0.00 0.0 0.94 0.00 0.00 0.63 0.00
1 0.51 0.51 NaN NaN NaN NaN NaN NaN NaN NaN NaN
2 0.54 0.54 0.00 0.0 0.63 0.0 0.51 0.54 0.51 1.00 0.51
3 0.81 0.05 0.13 0.7 0.02 NaN NaN NaN NaN NaN NaN
Now, just find max among each row:
In [2047]: df.max(axis = 1)
Out[2047]:
0 0.94
1 0.51
2 1.00
3 0.81
Above, you can see max
of all rows.
Now, find the max
value from the above. That will be the max of the whole Dataframe.
In [2048]: df.max(axis = 1).max()
Out[2048]: 1.0
To, find the index of this value:
1-line solution:
In [2082]: pd.DataFrame(Value).max(axis = 1).idxmax()
Out[2082]: 2
Let me know if this helps.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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%2f53313533%2ffinding-index-of-highest-element-from-a-list-of-list%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You are searching for max value in each list, but it is present in third list only, this will throw ValueError
for all other lists unless max value is present in first list itself.
It can be done simply like this
max_list = list()
for i, sub_list in enumerate(Value):
max_list.append((max(sub_list), i))
index_of_max = max(max_list)[1]
index_of_max
provides index of list containing max value.
add a comment |
You are searching for max value in each list, but it is present in third list only, this will throw ValueError
for all other lists unless max value is present in first list itself.
It can be done simply like this
max_list = list()
for i, sub_list in enumerate(Value):
max_list.append((max(sub_list), i))
index_of_max = max(max_list)[1]
index_of_max
provides index of list containing max value.
add a comment |
You are searching for max value in each list, but it is present in third list only, this will throw ValueError
for all other lists unless max value is present in first list itself.
It can be done simply like this
max_list = list()
for i, sub_list in enumerate(Value):
max_list.append((max(sub_list), i))
index_of_max = max(max_list)[1]
index_of_max
provides index of list containing max value.
You are searching for max value in each list, but it is present in third list only, this will throw ValueError
for all other lists unless max value is present in first list itself.
It can be done simply like this
max_list = list()
for i, sub_list in enumerate(Value):
max_list.append((max(sub_list), i))
index_of_max = max(max_list)[1]
index_of_max
provides index of list containing max value.
edited Nov 15 '18 at 6:36
answered Nov 15 '18 at 6:31
infiQuantainfiQuanta
868
868
add a comment |
add a comment |
You can do this as kindof one-liner as well:
data = [[0.25, 0.0, 0.0, 0.0, 0.0, 0.0, 0.94, 0.0, 0.0, 0.63, 0.0],
[0.51, 0.51],
[0.54, 0.54, 0.0, 0.0,0.63, 0.0, 0.51, 0.54, 0.51, 1.0, 0.51],
[0.81,0.05, 0.13, 0.7, 0.02]]
# iL === innerList, abbreviated for 79 chars line width
max_idx, max_value = max( enumerate(max(iL) for iL in data), key = lambda x:x[1])
print(max_idx)
Output:
2 # max_value == 1, but not printed
The trick is to get the max(..)
of each inner list, enumerate()
those and use another max(iterable, key=...)
that has a key
that selects the highest value (not position) of the enumerate tuple (position,value)
.
Advantage - its using generators:
you do not create any intermediary lists but soley work with generators - for big lists this is far more memory friendly then then creating a list like infiQuanta does.
add a comment |
You can do this as kindof one-liner as well:
data = [[0.25, 0.0, 0.0, 0.0, 0.0, 0.0, 0.94, 0.0, 0.0, 0.63, 0.0],
[0.51, 0.51],
[0.54, 0.54, 0.0, 0.0,0.63, 0.0, 0.51, 0.54, 0.51, 1.0, 0.51],
[0.81,0.05, 0.13, 0.7, 0.02]]
# iL === innerList, abbreviated for 79 chars line width
max_idx, max_value = max( enumerate(max(iL) for iL in data), key = lambda x:x[1])
print(max_idx)
Output:
2 # max_value == 1, but not printed
The trick is to get the max(..)
of each inner list, enumerate()
those and use another max(iterable, key=...)
that has a key
that selects the highest value (not position) of the enumerate tuple (position,value)
.
Advantage - its using generators:
you do not create any intermediary lists but soley work with generators - for big lists this is far more memory friendly then then creating a list like infiQuanta does.
add a comment |
You can do this as kindof one-liner as well:
data = [[0.25, 0.0, 0.0, 0.0, 0.0, 0.0, 0.94, 0.0, 0.0, 0.63, 0.0],
[0.51, 0.51],
[0.54, 0.54, 0.0, 0.0,0.63, 0.0, 0.51, 0.54, 0.51, 1.0, 0.51],
[0.81,0.05, 0.13, 0.7, 0.02]]
# iL === innerList, abbreviated for 79 chars line width
max_idx, max_value = max( enumerate(max(iL) for iL in data), key = lambda x:x[1])
print(max_idx)
Output:
2 # max_value == 1, but not printed
The trick is to get the max(..)
of each inner list, enumerate()
those and use another max(iterable, key=...)
that has a key
that selects the highest value (not position) of the enumerate tuple (position,value)
.
Advantage - its using generators:
you do not create any intermediary lists but soley work with generators - for big lists this is far more memory friendly then then creating a list like infiQuanta does.
You can do this as kindof one-liner as well:
data = [[0.25, 0.0, 0.0, 0.0, 0.0, 0.0, 0.94, 0.0, 0.0, 0.63, 0.0],
[0.51, 0.51],
[0.54, 0.54, 0.0, 0.0,0.63, 0.0, 0.51, 0.54, 0.51, 1.0, 0.51],
[0.81,0.05, 0.13, 0.7, 0.02]]
# iL === innerList, abbreviated for 79 chars line width
max_idx, max_value = max( enumerate(max(iL) for iL in data), key = lambda x:x[1])
print(max_idx)
Output:
2 # max_value == 1, but not printed
The trick is to get the max(..)
of each inner list, enumerate()
those and use another max(iterable, key=...)
that has a key
that selects the highest value (not position) of the enumerate tuple (position,value)
.
Advantage - its using generators:
you do not create any intermediary lists but soley work with generators - for big lists this is far more memory friendly then then creating a list like infiQuanta does.
edited Nov 15 '18 at 8:58
answered Nov 15 '18 at 8:10
Patrick ArtnerPatrick Artner
25.1k62444
25.1k62444
add a comment |
add a comment |
You can solve this using the Python's library pandas
. Solution becomes really simple:
Create a dataframe
(df) from your list:
In [2040]: df = pd.DataFrame(Value)
In [2041]: df
Out[2041]:
0 1 2 3 4 5 6 7 8 9 10
0 0.25 0.00 0.00 0.0 0.00 0.0 0.94 0.00 0.00 0.63 0.00
1 0.51 0.51 NaN NaN NaN NaN NaN NaN NaN NaN NaN
2 0.54 0.54 0.00 0.0 0.63 0.0 0.51 0.54 0.51 1.00 0.51
3 0.81 0.05 0.13 0.7 0.02 NaN NaN NaN NaN NaN NaN
Now, just find max among each row:
In [2047]: df.max(axis = 1)
Out[2047]:
0 0.94
1 0.51
2 1.00
3 0.81
Above, you can see max
of all rows.
Now, find the max
value from the above. That will be the max of the whole Dataframe.
In [2048]: df.max(axis = 1).max()
Out[2048]: 1.0
To, find the index of this value:
1-line solution:
In [2082]: pd.DataFrame(Value).max(axis = 1).idxmax()
Out[2082]: 2
Let me know if this helps.
add a comment |
You can solve this using the Python's library pandas
. Solution becomes really simple:
Create a dataframe
(df) from your list:
In [2040]: df = pd.DataFrame(Value)
In [2041]: df
Out[2041]:
0 1 2 3 4 5 6 7 8 9 10
0 0.25 0.00 0.00 0.0 0.00 0.0 0.94 0.00 0.00 0.63 0.00
1 0.51 0.51 NaN NaN NaN NaN NaN NaN NaN NaN NaN
2 0.54 0.54 0.00 0.0 0.63 0.0 0.51 0.54 0.51 1.00 0.51
3 0.81 0.05 0.13 0.7 0.02 NaN NaN NaN NaN NaN NaN
Now, just find max among each row:
In [2047]: df.max(axis = 1)
Out[2047]:
0 0.94
1 0.51
2 1.00
3 0.81
Above, you can see max
of all rows.
Now, find the max
value from the above. That will be the max of the whole Dataframe.
In [2048]: df.max(axis = 1).max()
Out[2048]: 1.0
To, find the index of this value:
1-line solution:
In [2082]: pd.DataFrame(Value).max(axis = 1).idxmax()
Out[2082]: 2
Let me know if this helps.
add a comment |
You can solve this using the Python's library pandas
. Solution becomes really simple:
Create a dataframe
(df) from your list:
In [2040]: df = pd.DataFrame(Value)
In [2041]: df
Out[2041]:
0 1 2 3 4 5 6 7 8 9 10
0 0.25 0.00 0.00 0.0 0.00 0.0 0.94 0.00 0.00 0.63 0.00
1 0.51 0.51 NaN NaN NaN NaN NaN NaN NaN NaN NaN
2 0.54 0.54 0.00 0.0 0.63 0.0 0.51 0.54 0.51 1.00 0.51
3 0.81 0.05 0.13 0.7 0.02 NaN NaN NaN NaN NaN NaN
Now, just find max among each row:
In [2047]: df.max(axis = 1)
Out[2047]:
0 0.94
1 0.51
2 1.00
3 0.81
Above, you can see max
of all rows.
Now, find the max
value from the above. That will be the max of the whole Dataframe.
In [2048]: df.max(axis = 1).max()
Out[2048]: 1.0
To, find the index of this value:
1-line solution:
In [2082]: pd.DataFrame(Value).max(axis = 1).idxmax()
Out[2082]: 2
Let me know if this helps.
You can solve this using the Python's library pandas
. Solution becomes really simple:
Create a dataframe
(df) from your list:
In [2040]: df = pd.DataFrame(Value)
In [2041]: df
Out[2041]:
0 1 2 3 4 5 6 7 8 9 10
0 0.25 0.00 0.00 0.0 0.00 0.0 0.94 0.00 0.00 0.63 0.00
1 0.51 0.51 NaN NaN NaN NaN NaN NaN NaN NaN NaN
2 0.54 0.54 0.00 0.0 0.63 0.0 0.51 0.54 0.51 1.00 0.51
3 0.81 0.05 0.13 0.7 0.02 NaN NaN NaN NaN NaN NaN
Now, just find max among each row:
In [2047]: df.max(axis = 1)
Out[2047]:
0 0.94
1 0.51
2 1.00
3 0.81
Above, you can see max
of all rows.
Now, find the max
value from the above. That will be the max of the whole Dataframe.
In [2048]: df.max(axis = 1).max()
Out[2048]: 1.0
To, find the index of this value:
1-line solution:
In [2082]: pd.DataFrame(Value).max(axis = 1).idxmax()
Out[2082]: 2
Let me know if this helps.
edited Nov 15 '18 at 6:59
answered Nov 15 '18 at 6:34
Mayank PorwalMayank Porwal
4,9702724
4,9702724
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.
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%2f53313533%2ffinding-index-of-highest-element-from-a-list-of-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