How to sort list of dictionaries by average of specified value
Write a function named sort_by_average_rating
that takes a list/array of key-value stores as a parameter where each key-value store has keys "ratings", "budget", and "box_office" where "budget" and "box_office" are integers and "ratings" is a list of integers.
Sort the input based on the average of the values in "ratings".
def sort_by_average_rating(lista):
lista.sort(sum(lista['ratings']) / int(len(lista['ratings'])))
return lista
I am getting error on input:
error on input ['ratings': [9, 10, 2, 3, 8, 10, 9], 'budget': 16219606.11, 'box_office': 13297812, 'ratings': [6, 3, 1, 7, 9, 2], 'budget': 12995254.35, 'box_office': 26409035] list indices must be integers or slices, not str.
Expected Output:
['ratings': [6, 3, 1, 7, 9, 2], 'budget': 12995254.35, 'box_office': 26409035, 'ratings': [9, 10, 2, 3, 8, 10, 9], 'budget': 16219606.11, 'box_office': 13297812]
What am I doing wrong?
python algorithm list sorting dictionary
add a comment |
Write a function named sort_by_average_rating
that takes a list/array of key-value stores as a parameter where each key-value store has keys "ratings", "budget", and "box_office" where "budget" and "box_office" are integers and "ratings" is a list of integers.
Sort the input based on the average of the values in "ratings".
def sort_by_average_rating(lista):
lista.sort(sum(lista['ratings']) / int(len(lista['ratings'])))
return lista
I am getting error on input:
error on input ['ratings': [9, 10, 2, 3, 8, 10, 9], 'budget': 16219606.11, 'box_office': 13297812, 'ratings': [6, 3, 1, 7, 9, 2], 'budget': 12995254.35, 'box_office': 26409035] list indices must be integers or slices, not str.
Expected Output:
['ratings': [6, 3, 1, 7, 9, 2], 'budget': 12995254.35, 'box_office': 26409035, 'ratings': [9, 10, 2, 3, 8, 10, 9], 'budget': 16219606.11, 'box_office': 13297812]
What am I doing wrong?
python algorithm list sorting dictionary
2
key, values are for dictionaries, not lists. Minimal, Complete, and Verifiable example please.
– Julien
Nov 13 '18 at 23:26
1
Possible duplicate of error list indices must be integers or slices, not str
– ljeabmreosn
Nov 14 '18 at 0:48
add a comment |
Write a function named sort_by_average_rating
that takes a list/array of key-value stores as a parameter where each key-value store has keys "ratings", "budget", and "box_office" where "budget" and "box_office" are integers and "ratings" is a list of integers.
Sort the input based on the average of the values in "ratings".
def sort_by_average_rating(lista):
lista.sort(sum(lista['ratings']) / int(len(lista['ratings'])))
return lista
I am getting error on input:
error on input ['ratings': [9, 10, 2, 3, 8, 10, 9], 'budget': 16219606.11, 'box_office': 13297812, 'ratings': [6, 3, 1, 7, 9, 2], 'budget': 12995254.35, 'box_office': 26409035] list indices must be integers or slices, not str.
Expected Output:
['ratings': [6, 3, 1, 7, 9, 2], 'budget': 12995254.35, 'box_office': 26409035, 'ratings': [9, 10, 2, 3, 8, 10, 9], 'budget': 16219606.11, 'box_office': 13297812]
What am I doing wrong?
python algorithm list sorting dictionary
Write a function named sort_by_average_rating
that takes a list/array of key-value stores as a parameter where each key-value store has keys "ratings", "budget", and "box_office" where "budget" and "box_office" are integers and "ratings" is a list of integers.
Sort the input based on the average of the values in "ratings".
def sort_by_average_rating(lista):
lista.sort(sum(lista['ratings']) / int(len(lista['ratings'])))
return lista
I am getting error on input:
error on input ['ratings': [9, 10, 2, 3, 8, 10, 9], 'budget': 16219606.11, 'box_office': 13297812, 'ratings': [6, 3, 1, 7, 9, 2], 'budget': 12995254.35, 'box_office': 26409035] list indices must be integers or slices, not str.
Expected Output:
['ratings': [6, 3, 1, 7, 9, 2], 'budget': 12995254.35, 'box_office': 26409035, 'ratings': [9, 10, 2, 3, 8, 10, 9], 'budget': 16219606.11, 'box_office': 13297812]
What am I doing wrong?
python algorithm list sorting dictionary
python algorithm list sorting dictionary
edited Nov 14 '18 at 16:33
jpp
99.2k2160110
99.2k2160110
asked Nov 13 '18 at 23:23
AnonymousAnonymous
61
61
2
key, values are for dictionaries, not lists. Minimal, Complete, and Verifiable example please.
– Julien
Nov 13 '18 at 23:26
1
Possible duplicate of error list indices must be integers or slices, not str
– ljeabmreosn
Nov 14 '18 at 0:48
add a comment |
2
key, values are for dictionaries, not lists. Minimal, Complete, and Verifiable example please.
– Julien
Nov 13 '18 at 23:26
1
Possible duplicate of error list indices must be integers or slices, not str
– ljeabmreosn
Nov 14 '18 at 0:48
2
2
key, values are for dictionaries, not lists. Minimal, Complete, and Verifiable example please.
– Julien
Nov 13 '18 at 23:26
key, values are for dictionaries, not lists. Minimal, Complete, and Verifiable example please.
– Julien
Nov 13 '18 at 23:26
1
1
Possible duplicate of error list indices must be integers or slices, not str
– ljeabmreosn
Nov 14 '18 at 0:48
Possible duplicate of error list indices must be integers or slices, not str
– ljeabmreosn
Nov 14 '18 at 0:48
add a comment |
1 Answer
1
active
oldest
votes
Your error message says you have this list of dictionaries:
L = ['ratings': [9, 10, 2, 3, 8, 10, 9], 'budget': 16219606.11, 'box_office': 13297812,
'ratings': [6, 3, 1, 7, 9, 2], 'budget': 12995254.35, 'box_office': 26409035]
You can just use sorted
with a custom key
function:
res = sorted(L, key=lambda x: sum(x['ratings']) / len(x['ratings']))
['box_office': 26409035, 'budget': 12995254.35, 'ratings': [6, 3, 1, 7, 9, 2],
'box_office': 13297812, 'budget': 16219606.11, 'ratings': [9, 10, 2, 3, 8, 10, 9]]
Alternatively, you can use statistics.mean
:
from statistics import mean
res = sorted(L, key=lambda x: mean(x['ratings']))
Notice the lambda
function takes each item in your iterable (L
) rather than the entire list of dictionaries (as in your current attempt).
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%2f53290963%2fhow-to-sort-list-of-dictionaries-by-average-of-specified-value%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Your error message says you have this list of dictionaries:
L = ['ratings': [9, 10, 2, 3, 8, 10, 9], 'budget': 16219606.11, 'box_office': 13297812,
'ratings': [6, 3, 1, 7, 9, 2], 'budget': 12995254.35, 'box_office': 26409035]
You can just use sorted
with a custom key
function:
res = sorted(L, key=lambda x: sum(x['ratings']) / len(x['ratings']))
['box_office': 26409035, 'budget': 12995254.35, 'ratings': [6, 3, 1, 7, 9, 2],
'box_office': 13297812, 'budget': 16219606.11, 'ratings': [9, 10, 2, 3, 8, 10, 9]]
Alternatively, you can use statistics.mean
:
from statistics import mean
res = sorted(L, key=lambda x: mean(x['ratings']))
Notice the lambda
function takes each item in your iterable (L
) rather than the entire list of dictionaries (as in your current attempt).
add a comment |
Your error message says you have this list of dictionaries:
L = ['ratings': [9, 10, 2, 3, 8, 10, 9], 'budget': 16219606.11, 'box_office': 13297812,
'ratings': [6, 3, 1, 7, 9, 2], 'budget': 12995254.35, 'box_office': 26409035]
You can just use sorted
with a custom key
function:
res = sorted(L, key=lambda x: sum(x['ratings']) / len(x['ratings']))
['box_office': 26409035, 'budget': 12995254.35, 'ratings': [6, 3, 1, 7, 9, 2],
'box_office': 13297812, 'budget': 16219606.11, 'ratings': [9, 10, 2, 3, 8, 10, 9]]
Alternatively, you can use statistics.mean
:
from statistics import mean
res = sorted(L, key=lambda x: mean(x['ratings']))
Notice the lambda
function takes each item in your iterable (L
) rather than the entire list of dictionaries (as in your current attempt).
add a comment |
Your error message says you have this list of dictionaries:
L = ['ratings': [9, 10, 2, 3, 8, 10, 9], 'budget': 16219606.11, 'box_office': 13297812,
'ratings': [6, 3, 1, 7, 9, 2], 'budget': 12995254.35, 'box_office': 26409035]
You can just use sorted
with a custom key
function:
res = sorted(L, key=lambda x: sum(x['ratings']) / len(x['ratings']))
['box_office': 26409035, 'budget': 12995254.35, 'ratings': [6, 3, 1, 7, 9, 2],
'box_office': 13297812, 'budget': 16219606.11, 'ratings': [9, 10, 2, 3, 8, 10, 9]]
Alternatively, you can use statistics.mean
:
from statistics import mean
res = sorted(L, key=lambda x: mean(x['ratings']))
Notice the lambda
function takes each item in your iterable (L
) rather than the entire list of dictionaries (as in your current attempt).
Your error message says you have this list of dictionaries:
L = ['ratings': [9, 10, 2, 3, 8, 10, 9], 'budget': 16219606.11, 'box_office': 13297812,
'ratings': [6, 3, 1, 7, 9, 2], 'budget': 12995254.35, 'box_office': 26409035]
You can just use sorted
with a custom key
function:
res = sorted(L, key=lambda x: sum(x['ratings']) / len(x['ratings']))
['box_office': 26409035, 'budget': 12995254.35, 'ratings': [6, 3, 1, 7, 9, 2],
'box_office': 13297812, 'budget': 16219606.11, 'ratings': [9, 10, 2, 3, 8, 10, 9]]
Alternatively, you can use statistics.mean
:
from statistics import mean
res = sorted(L, key=lambda x: mean(x['ratings']))
Notice the lambda
function takes each item in your iterable (L
) rather than the entire list of dictionaries (as in your current attempt).
answered Nov 14 '18 at 16:29
jppjpp
99.2k2160110
99.2k2160110
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%2f53290963%2fhow-to-sort-list-of-dictionaries-by-average-of-specified-value%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
2
key, values are for dictionaries, not lists. Minimal, Complete, and Verifiable example please.
– Julien
Nov 13 '18 at 23:26
1
Possible duplicate of error list indices must be integers or slices, not str
– ljeabmreosn
Nov 14 '18 at 0:48