Inserting the element-wise mean of nested list into the same listHow to remove an element from a list by index?Find intersection of two nested lists?Getting the last element of a listHow do I get the number of elements in a list?Is there a simple way to delete a list element by value?Proper way to make HTML nested list?How to test if variable exists in nested list and append it to that list in pythonpython: list of lists to ordered dict and group by first elementcompare 2 2D lists and delete items from one list based on anotherIterating through database with nested lists
Python web-scraper to download table of transistor counts from Wikipedia
What are these things that surround museum exhibits called?
How to give my students a straightedge instead of a ruler
What was the motivation for the invention of electric pianos?
Is there a tool to measure the "maturity" of a code in Git?
Asked to Not Use Transactions and to Use A Workaround to Simulate One
Can I see Harvest moon in India?
In what state are satellites left in when they are left in a graveyard orbit?
What is the name of this Allen-head furniture fastener?
Why is my fire extinguisher emptied after one use?
What was the ultimate objective of The Party in 1984?
Where is it? - The Google Earth Challenge Ep. 2
Make 2019 with single digits
Is a suit against a University Dorm for changing policies on a whim likely to succeed (USA)?
Are there any rules about taking damage whilst holding your breath in combat?
How do certain apps show new notifications when internet access is restricted to them?
Why is the year in this ISO timestamp not 2019?
Can a character with good/neutral alignment attune to a sentient object with evil alignment?
What is this gigantic dish at Ben Gurion airport?
How to write characters doing illogical things in a believable way?
Usage of blank space in trade banner and text-positioning
How would you control supersoldiers in a late iron-age society?
What officially disallows US presidents from driving?
Why don't Wizards use wrist straps to protect against disarming charms?
Inserting the element-wise mean of nested list into the same list
How to remove an element from a list by index?Find intersection of two nested lists?Getting the last element of a listHow do I get the number of elements in a list?Is there a simple way to delete a list element by value?Proper way to make HTML nested list?How to test if variable exists in nested list and append it to that list in pythonpython: list of lists to ordered dict and group by first elementcompare 2 2D lists and delete items from one list based on anotherIterating through database with nested lists
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
Suppose there is a list of nested lists of floats
L = [[a,b,c],[e,f,g],[h,i,j]]
What kind of function can I define to iterate through the list once and insert the mean of elements of every consecutive list into the same list? I.e. I want to get
L1 = [[a,b,c],[(a+e)/2,(b+f)/2,(c+g)/2],[e,f,g],[(e+h)/2,(f+i)/2,(g+j)/2],[h,i,j]]
I know the function to get the element wise mean of two lists:
from operator import add
new_list = list(map(add,list1,list2))
J = [j/2 for j in new_list]
However inserting this list of mean values back into the same list while maintaining the proper index iteration through the old list proved challenging.
python list nested-lists
add a comment
|
Suppose there is a list of nested lists of floats
L = [[a,b,c],[e,f,g],[h,i,j]]
What kind of function can I define to iterate through the list once and insert the mean of elements of every consecutive list into the same list? I.e. I want to get
L1 = [[a,b,c],[(a+e)/2,(b+f)/2,(c+g)/2],[e,f,g],[(e+h)/2,(f+i)/2,(g+j)/2],[h,i,j]]
I know the function to get the element wise mean of two lists:
from operator import add
new_list = list(map(add,list1,list2))
J = [j/2 for j in new_list]
However inserting this list of mean values back into the same list while maintaining the proper index iteration through the old list proved challenging.
python list nested-lists
Just construct a new list, and mutate back into the original list if necessary.
– Andras Deak
Mar 28 at 11:20
add a comment
|
Suppose there is a list of nested lists of floats
L = [[a,b,c],[e,f,g],[h,i,j]]
What kind of function can I define to iterate through the list once and insert the mean of elements of every consecutive list into the same list? I.e. I want to get
L1 = [[a,b,c],[(a+e)/2,(b+f)/2,(c+g)/2],[e,f,g],[(e+h)/2,(f+i)/2,(g+j)/2],[h,i,j]]
I know the function to get the element wise mean of two lists:
from operator import add
new_list = list(map(add,list1,list2))
J = [j/2 for j in new_list]
However inserting this list of mean values back into the same list while maintaining the proper index iteration through the old list proved challenging.
python list nested-lists
Suppose there is a list of nested lists of floats
L = [[a,b,c],[e,f,g],[h,i,j]]
What kind of function can I define to iterate through the list once and insert the mean of elements of every consecutive list into the same list? I.e. I want to get
L1 = [[a,b,c],[(a+e)/2,(b+f)/2,(c+g)/2],[e,f,g],[(e+h)/2,(f+i)/2,(g+j)/2],[h,i,j]]
I know the function to get the element wise mean of two lists:
from operator import add
new_list = list(map(add,list1,list2))
J = [j/2 for j in new_list]
However inserting this list of mean values back into the same list while maintaining the proper index iteration through the old list proved challenging.
python list nested-lists
python list nested-lists
asked Mar 28 at 11:08
NetUser5y62NetUser5y62
1065 bronze badges
1065 bronze badges
Just construct a new list, and mutate back into the original list if necessary.
– Andras Deak
Mar 28 at 11:20
add a comment
|
Just construct a new list, and mutate back into the original list if necessary.
– Andras Deak
Mar 28 at 11:20
Just construct a new list, and mutate back into the original list if necessary.
– Andras Deak
Mar 28 at 11:20
Just construct a new list, and mutate back into the original list if necessary.
– Andras Deak
Mar 28 at 11:20
add a comment
|
1 Answer
1
active
oldest
votes
There are two cases:
- You don't care if the resulting list is the same list:
new_list = []
for i in range(len(L)-1):
new_list.append(L[i])
new_list.append(list(map(lambda x: sum(x)/len(x), zip(L[i],L[i+1]))))
new_list.append(L[-1])
- You want the changes to be done in-place:
i=0
while i < len(L)-1:
new_elem = list(map(lambda x: sum(x)/len(x), zip(L[i],L[i+1])))
L.insert(i+1, new_elem)
i += 2
EDIT: If you're using python 3.4 or above, instead of lambda x: sum(x)/len(x)
you can use mean(x)
(from the package statistics
).
1
thank you, for number 2 there is a small typo: i < len(L) - 1
– NetUser5y62
Mar 28 at 11:34
avoid usinglambda
and just use the builtinmean
from the stdlib in thestatistics
library
– aws_apprentice
Mar 28 at 11:42
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/4.0/"u003ecc by-sa 4.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%2f55396066%2finserting-the-element-wise-mean-of-nested-list-into-the-same-list%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
There are two cases:
- You don't care if the resulting list is the same list:
new_list = []
for i in range(len(L)-1):
new_list.append(L[i])
new_list.append(list(map(lambda x: sum(x)/len(x), zip(L[i],L[i+1]))))
new_list.append(L[-1])
- You want the changes to be done in-place:
i=0
while i < len(L)-1:
new_elem = list(map(lambda x: sum(x)/len(x), zip(L[i],L[i+1])))
L.insert(i+1, new_elem)
i += 2
EDIT: If you're using python 3.4 or above, instead of lambda x: sum(x)/len(x)
you can use mean(x)
(from the package statistics
).
1
thank you, for number 2 there is a small typo: i < len(L) - 1
– NetUser5y62
Mar 28 at 11:34
avoid usinglambda
and just use the builtinmean
from the stdlib in thestatistics
library
– aws_apprentice
Mar 28 at 11:42
add a comment
|
There are two cases:
- You don't care if the resulting list is the same list:
new_list = []
for i in range(len(L)-1):
new_list.append(L[i])
new_list.append(list(map(lambda x: sum(x)/len(x), zip(L[i],L[i+1]))))
new_list.append(L[-1])
- You want the changes to be done in-place:
i=0
while i < len(L)-1:
new_elem = list(map(lambda x: sum(x)/len(x), zip(L[i],L[i+1])))
L.insert(i+1, new_elem)
i += 2
EDIT: If you're using python 3.4 or above, instead of lambda x: sum(x)/len(x)
you can use mean(x)
(from the package statistics
).
1
thank you, for number 2 there is a small typo: i < len(L) - 1
– NetUser5y62
Mar 28 at 11:34
avoid usinglambda
and just use the builtinmean
from the stdlib in thestatistics
library
– aws_apprentice
Mar 28 at 11:42
add a comment
|
There are two cases:
- You don't care if the resulting list is the same list:
new_list = []
for i in range(len(L)-1):
new_list.append(L[i])
new_list.append(list(map(lambda x: sum(x)/len(x), zip(L[i],L[i+1]))))
new_list.append(L[-1])
- You want the changes to be done in-place:
i=0
while i < len(L)-1:
new_elem = list(map(lambda x: sum(x)/len(x), zip(L[i],L[i+1])))
L.insert(i+1, new_elem)
i += 2
EDIT: If you're using python 3.4 or above, instead of lambda x: sum(x)/len(x)
you can use mean(x)
(from the package statistics
).
There are two cases:
- You don't care if the resulting list is the same list:
new_list = []
for i in range(len(L)-1):
new_list.append(L[i])
new_list.append(list(map(lambda x: sum(x)/len(x), zip(L[i],L[i+1]))))
new_list.append(L[-1])
- You want the changes to be done in-place:
i=0
while i < len(L)-1:
new_elem = list(map(lambda x: sum(x)/len(x), zip(L[i],L[i+1])))
L.insert(i+1, new_elem)
i += 2
EDIT: If you're using python 3.4 or above, instead of lambda x: sum(x)/len(x)
you can use mean(x)
(from the package statistics
).
edited Mar 28 at 11:50
answered Mar 28 at 11:23
BogsanBogsan
3612 silver badges7 bronze badges
3612 silver badges7 bronze badges
1
thank you, for number 2 there is a small typo: i < len(L) - 1
– NetUser5y62
Mar 28 at 11:34
avoid usinglambda
and just use the builtinmean
from the stdlib in thestatistics
library
– aws_apprentice
Mar 28 at 11:42
add a comment
|
1
thank you, for number 2 there is a small typo: i < len(L) - 1
– NetUser5y62
Mar 28 at 11:34
avoid usinglambda
and just use the builtinmean
from the stdlib in thestatistics
library
– aws_apprentice
Mar 28 at 11:42
1
1
thank you, for number 2 there is a small typo: i < len(L) - 1
– NetUser5y62
Mar 28 at 11:34
thank you, for number 2 there is a small typo: i < len(L) - 1
– NetUser5y62
Mar 28 at 11:34
avoid using
lambda
and just use the builtin mean
from the stdlib in the statistics
library– aws_apprentice
Mar 28 at 11:42
avoid using
lambda
and just use the builtin mean
from the stdlib in the statistics
library– aws_apprentice
Mar 28 at 11:42
add a comment
|
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
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%2f55396066%2finserting-the-element-wise-mean-of-nested-list-into-the-same-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
Just construct a new list, and mutate back into the original list if necessary.
– Andras Deak
Mar 28 at 11:20