Make all sublists same length by prepending nan
How can I make all of my sublists the same length (the length of the longest sublist) by prepending np.nan's on each sublist?
import random
[list(range(0,random.randint(1,5))) for x in range(n)]
So if the output is:
[[0, 1], [0], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3]]
It should look like:
[[nan,nan,nan,0, 1], [nan,nan,nan,nan,0], [0, 1, 2, 3, 4], [0, 1, 2,
3, 4], [nan,0, 1, 2, 3]]
python
add a comment |
How can I make all of my sublists the same length (the length of the longest sublist) by prepending np.nan's on each sublist?
import random
[list(range(0,random.randint(1,5))) for x in range(n)]
So if the output is:
[[0, 1], [0], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3]]
It should look like:
[[nan,nan,nan,0, 1], [nan,nan,nan,nan,0], [0, 1, 2, 3, 4], [0, 1, 2,
3, 4], [nan,0, 1, 2, 3]]
python
1
What have you tried so far?
– Idlehands
Nov 20 '18 at 19:18
@Idlehands I've tried reversing first and then .extend() and then reverse again. But this doesn't work if the list is empty
– jchaykow
Nov 20 '18 at 19:22
You might be overthinking it... how is the list of sublists supposed to be generated? Is it actual data or is it created with thenan
paddings from the beginning?
– Idlehands
Nov 20 '18 at 19:25
1
Saying 'everything' is not useful. Show us the actual code that you tried, which gives us either a starting point or a hint what we should not try.
– Bram Vanroy
Nov 20 '18 at 19:25
@BramVanroy it's a joke, I wrote two comments
– jchaykow
Nov 20 '18 at 19:26
add a comment |
How can I make all of my sublists the same length (the length of the longest sublist) by prepending np.nan's on each sublist?
import random
[list(range(0,random.randint(1,5))) for x in range(n)]
So if the output is:
[[0, 1], [0], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3]]
It should look like:
[[nan,nan,nan,0, 1], [nan,nan,nan,nan,0], [0, 1, 2, 3, 4], [0, 1, 2,
3, 4], [nan,0, 1, 2, 3]]
python
How can I make all of my sublists the same length (the length of the longest sublist) by prepending np.nan's on each sublist?
import random
[list(range(0,random.randint(1,5))) for x in range(n)]
So if the output is:
[[0, 1], [0], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3]]
It should look like:
[[nan,nan,nan,0, 1], [nan,nan,nan,nan,0], [0, 1, 2, 3, 4], [0, 1, 2,
3, 4], [nan,0, 1, 2, 3]]
python
python
asked Nov 20 '18 at 19:16
jchaykow
497318
497318
1
What have you tried so far?
– Idlehands
Nov 20 '18 at 19:18
@Idlehands I've tried reversing first and then .extend() and then reverse again. But this doesn't work if the list is empty
– jchaykow
Nov 20 '18 at 19:22
You might be overthinking it... how is the list of sublists supposed to be generated? Is it actual data or is it created with thenan
paddings from the beginning?
– Idlehands
Nov 20 '18 at 19:25
1
Saying 'everything' is not useful. Show us the actual code that you tried, which gives us either a starting point or a hint what we should not try.
– Bram Vanroy
Nov 20 '18 at 19:25
@BramVanroy it's a joke, I wrote two comments
– jchaykow
Nov 20 '18 at 19:26
add a comment |
1
What have you tried so far?
– Idlehands
Nov 20 '18 at 19:18
@Idlehands I've tried reversing first and then .extend() and then reverse again. But this doesn't work if the list is empty
– jchaykow
Nov 20 '18 at 19:22
You might be overthinking it... how is the list of sublists supposed to be generated? Is it actual data or is it created with thenan
paddings from the beginning?
– Idlehands
Nov 20 '18 at 19:25
1
Saying 'everything' is not useful. Show us the actual code that you tried, which gives us either a starting point or a hint what we should not try.
– Bram Vanroy
Nov 20 '18 at 19:25
@BramVanroy it's a joke, I wrote two comments
– jchaykow
Nov 20 '18 at 19:26
1
1
What have you tried so far?
– Idlehands
Nov 20 '18 at 19:18
What have you tried so far?
– Idlehands
Nov 20 '18 at 19:18
@Idlehands I've tried reversing first and then .extend() and then reverse again. But this doesn't work if the list is empty
– jchaykow
Nov 20 '18 at 19:22
@Idlehands I've tried reversing first and then .extend() and then reverse again. But this doesn't work if the list is empty
– jchaykow
Nov 20 '18 at 19:22
You might be overthinking it... how is the list of sublists supposed to be generated? Is it actual data or is it created with the
nan
paddings from the beginning?– Idlehands
Nov 20 '18 at 19:25
You might be overthinking it... how is the list of sublists supposed to be generated? Is it actual data or is it created with the
nan
paddings from the beginning?– Idlehands
Nov 20 '18 at 19:25
1
1
Saying 'everything' is not useful. Show us the actual code that you tried, which gives us either a starting point or a hint what we should not try.
– Bram Vanroy
Nov 20 '18 at 19:25
Saying 'everything' is not useful. Show us the actual code that you tried, which gives us either a starting point or a hint what we should not try.
– Bram Vanroy
Nov 20 '18 at 19:25
@BramVanroy it's a joke, I wrote two comments
– jchaykow
Nov 20 '18 at 19:26
@BramVanroy it's a joke, I wrote two comments
– jchaykow
Nov 20 '18 at 19:26
add a comment |
4 Answers
4
active
oldest
votes
First, find the length of the longest sublist using max()
. Then, for each sublist, use a slice assignment to replace the content of that list with the right number of NaN
s followed by the original list.
import random, math
n = 5
lists = [list(range(0,random.randint(1,5))) for x in range(n)]
# get the maximum length
maxlen = len(max(lists, key=len))
# pad left of each sublist with NaN to make it as long as the longest
for sublist in lists:
sublist[:] = [math.nan] * (maxlen - len(sublist)) + sublist
Accepting this one because it's just barely faster than @Torin May
– jchaykow
Nov 20 '18 at 19:38
It'll get a lot faster as yourn
goes up.
– kindall
Nov 20 '18 at 19:50
add a comment |
import random
import numpy as np
n = 5
a = [list(range(0,random.randint(1,5))) for x in range(n)]
for c in a:
while len(c) < n:
c.insert(0, np.nan)
print(a)
[[nan, nan, nan, nan, 0], [nan, nan, nan, nan, 0], [nan, nan, nan, 0, 1], [nan, nan, 0, 1, 2], [nan, nan, nan, 0, 1]]
This is an example of what I think you are looking for.
add a comment |
Not the most pythonic code but I think it does the trick:
longest_list_length = 0
for sublist in output:
if len(sublist) > longest_list_length:
longest_list_length = len(sublist)
for sublist in output:
nans_to_prepend = ['nan'] * (longest_list_length - len(sublist))
sublist = nans_to_prepend + sublist
replacing the value ofsublist
won't change the value in the original list.
– kindall
Nov 20 '18 at 19:28
add a comment |
If all you care about is generating the np.nan
padding during the random creation, this simple one liner will suffice:
[[np.nan]*(5-j) + list(range(j)) for j in (random.randint(1, 5) for x in range(5))]
# [[nan, 0, 1, 2, 3],
# [nan, nan, 0, 1, 2],
# [nan, nan, nan, 0, 1],
# [nan, 0, 1, 2, 3],
# [0, 1, 2, 3, 4]]
You can of course replace 5
with n
and determine your max length.
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%2f53400016%2fmake-all-sublists-same-length-by-prepending-nan%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
First, find the length of the longest sublist using max()
. Then, for each sublist, use a slice assignment to replace the content of that list with the right number of NaN
s followed by the original list.
import random, math
n = 5
lists = [list(range(0,random.randint(1,5))) for x in range(n)]
# get the maximum length
maxlen = len(max(lists, key=len))
# pad left of each sublist with NaN to make it as long as the longest
for sublist in lists:
sublist[:] = [math.nan] * (maxlen - len(sublist)) + sublist
Accepting this one because it's just barely faster than @Torin May
– jchaykow
Nov 20 '18 at 19:38
It'll get a lot faster as yourn
goes up.
– kindall
Nov 20 '18 at 19:50
add a comment |
First, find the length of the longest sublist using max()
. Then, for each sublist, use a slice assignment to replace the content of that list with the right number of NaN
s followed by the original list.
import random, math
n = 5
lists = [list(range(0,random.randint(1,5))) for x in range(n)]
# get the maximum length
maxlen = len(max(lists, key=len))
# pad left of each sublist with NaN to make it as long as the longest
for sublist in lists:
sublist[:] = [math.nan] * (maxlen - len(sublist)) + sublist
Accepting this one because it's just barely faster than @Torin May
– jchaykow
Nov 20 '18 at 19:38
It'll get a lot faster as yourn
goes up.
– kindall
Nov 20 '18 at 19:50
add a comment |
First, find the length of the longest sublist using max()
. Then, for each sublist, use a slice assignment to replace the content of that list with the right number of NaN
s followed by the original list.
import random, math
n = 5
lists = [list(range(0,random.randint(1,5))) for x in range(n)]
# get the maximum length
maxlen = len(max(lists, key=len))
# pad left of each sublist with NaN to make it as long as the longest
for sublist in lists:
sublist[:] = [math.nan] * (maxlen - len(sublist)) + sublist
First, find the length of the longest sublist using max()
. Then, for each sublist, use a slice assignment to replace the content of that list with the right number of NaN
s followed by the original list.
import random, math
n = 5
lists = [list(range(0,random.randint(1,5))) for x in range(n)]
# get the maximum length
maxlen = len(max(lists, key=len))
# pad left of each sublist with NaN to make it as long as the longest
for sublist in lists:
sublist[:] = [math.nan] * (maxlen - len(sublist)) + sublist
answered Nov 20 '18 at 19:27
kindall
127k17192241
127k17192241
Accepting this one because it's just barely faster than @Torin May
– jchaykow
Nov 20 '18 at 19:38
It'll get a lot faster as yourn
goes up.
– kindall
Nov 20 '18 at 19:50
add a comment |
Accepting this one because it's just barely faster than @Torin May
– jchaykow
Nov 20 '18 at 19:38
It'll get a lot faster as yourn
goes up.
– kindall
Nov 20 '18 at 19:50
Accepting this one because it's just barely faster than @Torin May
– jchaykow
Nov 20 '18 at 19:38
Accepting this one because it's just barely faster than @Torin May
– jchaykow
Nov 20 '18 at 19:38
It'll get a lot faster as your
n
goes up.– kindall
Nov 20 '18 at 19:50
It'll get a lot faster as your
n
goes up.– kindall
Nov 20 '18 at 19:50
add a comment |
import random
import numpy as np
n = 5
a = [list(range(0,random.randint(1,5))) for x in range(n)]
for c in a:
while len(c) < n:
c.insert(0, np.nan)
print(a)
[[nan, nan, nan, nan, 0], [nan, nan, nan, nan, 0], [nan, nan, nan, 0, 1], [nan, nan, 0, 1, 2], [nan, nan, nan, 0, 1]]
This is an example of what I think you are looking for.
add a comment |
import random
import numpy as np
n = 5
a = [list(range(0,random.randint(1,5))) for x in range(n)]
for c in a:
while len(c) < n:
c.insert(0, np.nan)
print(a)
[[nan, nan, nan, nan, 0], [nan, nan, nan, nan, 0], [nan, nan, nan, 0, 1], [nan, nan, 0, 1, 2], [nan, nan, nan, 0, 1]]
This is an example of what I think you are looking for.
add a comment |
import random
import numpy as np
n = 5
a = [list(range(0,random.randint(1,5))) for x in range(n)]
for c in a:
while len(c) < n:
c.insert(0, np.nan)
print(a)
[[nan, nan, nan, nan, 0], [nan, nan, nan, nan, 0], [nan, nan, nan, 0, 1], [nan, nan, 0, 1, 2], [nan, nan, nan, 0, 1]]
This is an example of what I think you are looking for.
import random
import numpy as np
n = 5
a = [list(range(0,random.randint(1,5))) for x in range(n)]
for c in a:
while len(c) < n:
c.insert(0, np.nan)
print(a)
[[nan, nan, nan, nan, 0], [nan, nan, nan, nan, 0], [nan, nan, nan, 0, 1], [nan, nan, 0, 1, 2], [nan, nan, nan, 0, 1]]
This is an example of what I think you are looking for.
answered Nov 20 '18 at 19:24
Torin May
303214
303214
add a comment |
add a comment |
Not the most pythonic code but I think it does the trick:
longest_list_length = 0
for sublist in output:
if len(sublist) > longest_list_length:
longest_list_length = len(sublist)
for sublist in output:
nans_to_prepend = ['nan'] * (longest_list_length - len(sublist))
sublist = nans_to_prepend + sublist
replacing the value ofsublist
won't change the value in the original list.
– kindall
Nov 20 '18 at 19:28
add a comment |
Not the most pythonic code but I think it does the trick:
longest_list_length = 0
for sublist in output:
if len(sublist) > longest_list_length:
longest_list_length = len(sublist)
for sublist in output:
nans_to_prepend = ['nan'] * (longest_list_length - len(sublist))
sublist = nans_to_prepend + sublist
replacing the value ofsublist
won't change the value in the original list.
– kindall
Nov 20 '18 at 19:28
add a comment |
Not the most pythonic code but I think it does the trick:
longest_list_length = 0
for sublist in output:
if len(sublist) > longest_list_length:
longest_list_length = len(sublist)
for sublist in output:
nans_to_prepend = ['nan'] * (longest_list_length - len(sublist))
sublist = nans_to_prepend + sublist
Not the most pythonic code but I think it does the trick:
longest_list_length = 0
for sublist in output:
if len(sublist) > longest_list_length:
longest_list_length = len(sublist)
for sublist in output:
nans_to_prepend = ['nan'] * (longest_list_length - len(sublist))
sublist = nans_to_prepend + sublist
answered Nov 20 '18 at 19:26
ritratt
74031328
74031328
replacing the value ofsublist
won't change the value in the original list.
– kindall
Nov 20 '18 at 19:28
add a comment |
replacing the value ofsublist
won't change the value in the original list.
– kindall
Nov 20 '18 at 19:28
replacing the value of
sublist
won't change the value in the original list.– kindall
Nov 20 '18 at 19:28
replacing the value of
sublist
won't change the value in the original list.– kindall
Nov 20 '18 at 19:28
add a comment |
If all you care about is generating the np.nan
padding during the random creation, this simple one liner will suffice:
[[np.nan]*(5-j) + list(range(j)) for j in (random.randint(1, 5) for x in range(5))]
# [[nan, 0, 1, 2, 3],
# [nan, nan, 0, 1, 2],
# [nan, nan, nan, 0, 1],
# [nan, 0, 1, 2, 3],
# [0, 1, 2, 3, 4]]
You can of course replace 5
with n
and determine your max length.
add a comment |
If all you care about is generating the np.nan
padding during the random creation, this simple one liner will suffice:
[[np.nan]*(5-j) + list(range(j)) for j in (random.randint(1, 5) for x in range(5))]
# [[nan, 0, 1, 2, 3],
# [nan, nan, 0, 1, 2],
# [nan, nan, nan, 0, 1],
# [nan, 0, 1, 2, 3],
# [0, 1, 2, 3, 4]]
You can of course replace 5
with n
and determine your max length.
add a comment |
If all you care about is generating the np.nan
padding during the random creation, this simple one liner will suffice:
[[np.nan]*(5-j) + list(range(j)) for j in (random.randint(1, 5) for x in range(5))]
# [[nan, 0, 1, 2, 3],
# [nan, nan, 0, 1, 2],
# [nan, nan, nan, 0, 1],
# [nan, 0, 1, 2, 3],
# [0, 1, 2, 3, 4]]
You can of course replace 5
with n
and determine your max length.
If all you care about is generating the np.nan
padding during the random creation, this simple one liner will suffice:
[[np.nan]*(5-j) + list(range(j)) for j in (random.randint(1, 5) for x in range(5))]
# [[nan, 0, 1, 2, 3],
# [nan, nan, 0, 1, 2],
# [nan, nan, nan, 0, 1],
# [nan, 0, 1, 2, 3],
# [0, 1, 2, 3, 4]]
You can of course replace 5
with n
and determine your max length.
answered Nov 20 '18 at 19:36
Idlehands
3,9871417
3,9871417
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%2f53400016%2fmake-all-sublists-same-length-by-prepending-nan%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
1
What have you tried so far?
– Idlehands
Nov 20 '18 at 19:18
@Idlehands I've tried reversing first and then .extend() and then reverse again. But this doesn't work if the list is empty
– jchaykow
Nov 20 '18 at 19:22
You might be overthinking it... how is the list of sublists supposed to be generated? Is it actual data or is it created with the
nan
paddings from the beginning?– Idlehands
Nov 20 '18 at 19:25
1
Saying 'everything' is not useful. Show us the actual code that you tried, which gives us either a starting point or a hint what we should not try.
– Bram Vanroy
Nov 20 '18 at 19:25
@BramVanroy it's a joke, I wrote two comments
– jchaykow
Nov 20 '18 at 19:26