Slice list of lists at regular interval then merge
How would you "columnize" a list of lists at every x
th index?
I was thinking of doing this by taking the starting list, then creating two new lists, merging them, then adding the remainder not divisible by x
.
For example with an interval of 2:
start = [
[1, 'one'],
[2, 'two'],
[3, 'three'],
[4, 'four'],
[5, 'five'],
[6, 'six'],
[7, 'seven'],
[8, 'eight'],
[9, 'nine'],
]
expected = [
[1, 'one', 3, 'three'],
[2, 'two', 4, 'four'],
# page break
[5, 'five', 7, 'seven'],
[6, 'six', 8, 'eight'],
# page break
[9, 'nine'],
]
Just wondering if there's a quick way of doing this?
python
add a comment |
How would you "columnize" a list of lists at every x
th index?
I was thinking of doing this by taking the starting list, then creating two new lists, merging them, then adding the remainder not divisible by x
.
For example with an interval of 2:
start = [
[1, 'one'],
[2, 'two'],
[3, 'three'],
[4, 'four'],
[5, 'five'],
[6, 'six'],
[7, 'seven'],
[8, 'eight'],
[9, 'nine'],
]
expected = [
[1, 'one', 3, 'three'],
[2, 'two', 4, 'four'],
# page break
[5, 'five', 7, 'seven'],
[6, 'six', 8, 'eight'],
# page break
[9, 'nine'],
]
Just wondering if there's a quick way of doing this?
python
Could you check your testcase, and if it is right this way, explain how you get it? I would think of1-3-5-7-9 - 2-4-6-8
and not1-3 - 2-4 - 5-7 - 6-8 - 9
as a "columnisation" of a list
– Black Owl Kai
Nov 13 '18 at 16:14
This makes more sense for 'columnise':C=2; [[y for x in start[C*z:C*z+C] for y in x] for z in range(len(start)//C+1)]
– gustavovelascoh
Nov 13 '18 at 16:30
add a comment |
How would you "columnize" a list of lists at every x
th index?
I was thinking of doing this by taking the starting list, then creating two new lists, merging them, then adding the remainder not divisible by x
.
For example with an interval of 2:
start = [
[1, 'one'],
[2, 'two'],
[3, 'three'],
[4, 'four'],
[5, 'five'],
[6, 'six'],
[7, 'seven'],
[8, 'eight'],
[9, 'nine'],
]
expected = [
[1, 'one', 3, 'three'],
[2, 'two', 4, 'four'],
# page break
[5, 'five', 7, 'seven'],
[6, 'six', 8, 'eight'],
# page break
[9, 'nine'],
]
Just wondering if there's a quick way of doing this?
python
How would you "columnize" a list of lists at every x
th index?
I was thinking of doing this by taking the starting list, then creating two new lists, merging them, then adding the remainder not divisible by x
.
For example with an interval of 2:
start = [
[1, 'one'],
[2, 'two'],
[3, 'three'],
[4, 'four'],
[5, 'five'],
[6, 'six'],
[7, 'seven'],
[8, 'eight'],
[9, 'nine'],
]
expected = [
[1, 'one', 3, 'three'],
[2, 'two', 4, 'four'],
# page break
[5, 'five', 7, 'seven'],
[6, 'six', 8, 'eight'],
# page break
[9, 'nine'],
]
Just wondering if there's a quick way of doing this?
python
python
edited Nov 13 '18 at 16:17
bdoubleu
asked Nov 13 '18 at 16:06
bdoubleubdoubleu
617112
617112
Could you check your testcase, and if it is right this way, explain how you get it? I would think of1-3-5-7-9 - 2-4-6-8
and not1-3 - 2-4 - 5-7 - 6-8 - 9
as a "columnisation" of a list
– Black Owl Kai
Nov 13 '18 at 16:14
This makes more sense for 'columnise':C=2; [[y for x in start[C*z:C*z+C] for y in x] for z in range(len(start)//C+1)]
– gustavovelascoh
Nov 13 '18 at 16:30
add a comment |
Could you check your testcase, and if it is right this way, explain how you get it? I would think of1-3-5-7-9 - 2-4-6-8
and not1-3 - 2-4 - 5-7 - 6-8 - 9
as a "columnisation" of a list
– Black Owl Kai
Nov 13 '18 at 16:14
This makes more sense for 'columnise':C=2; [[y for x in start[C*z:C*z+C] for y in x] for z in range(len(start)//C+1)]
– gustavovelascoh
Nov 13 '18 at 16:30
Could you check your testcase, and if it is right this way, explain how you get it? I would think of
1-3-5-7-9 - 2-4-6-8
and not 1-3 - 2-4 - 5-7 - 6-8 - 9
as a "columnisation" of a list– Black Owl Kai
Nov 13 '18 at 16:14
Could you check your testcase, and if it is right this way, explain how you get it? I would think of
1-3-5-7-9 - 2-4-6-8
and not 1-3 - 2-4 - 5-7 - 6-8 - 9
as a "columnisation" of a list– Black Owl Kai
Nov 13 '18 at 16:14
This makes more sense for 'columnise':
C=2; [[y for x in start[C*z:C*z+C] for y in x] for z in range(len(start)//C+1)]
– gustavovelascoh
Nov 13 '18 at 16:30
This makes more sense for 'columnise':
C=2; [[y for x in start[C*z:C*z+C] for y in x] for z in range(len(start)//C+1)]
– gustavovelascoh
Nov 13 '18 at 16:30
add a comment |
2 Answers
2
active
oldest
votes
I agree with the comments about this being a strange way to 'collumize'. However, here is a function that does what you described:
def columnize(A, interval=2):
ans =
for i in range(0,len(A), interval*2):
for j in range(min(interval, len(A)-i)):
ans.append(A[i+j] + (A[i+j+interval] if i+j+interval < len(A) else ))
return ans
add a comment |
Are you looking for something like this? Columnizing squared matrix?
start = [
[1, 'one'],
[2, 'two'],
[3, 'three'],
[4, 'four'],
[5, 'five'],
[6, 'six'],
[7, 'seven'],
[8, 'eight'],
[9, 'nine'],
]
expected = [
[1, 'one', 3, 'three'],
[2, 'two', 4, 'four'],
# page break
[5, 'five', 7, 'seven'],
[6, 'six', 8, 'eight'],
# page break
[9, 'nine'],
]
a = 2
r = a*a
ans =
for i in range(0, len(start), r):
l_tmp = start[i:i+r]
if l_tmp[::a]:
ans.append([item for sublist in l_tmp[::a] for item in sublist])
if l_tmp[1::a]:
ans.append([item for sublist in l_tmp[1::a] for item in sublist])
# You can easily add page break here
print(ans)
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%2f53284995%2fslice-list-of-lists-at-regular-interval-then-merge%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
I agree with the comments about this being a strange way to 'collumize'. However, here is a function that does what you described:
def columnize(A, interval=2):
ans =
for i in range(0,len(A), interval*2):
for j in range(min(interval, len(A)-i)):
ans.append(A[i+j] + (A[i+j+interval] if i+j+interval < len(A) else ))
return ans
add a comment |
I agree with the comments about this being a strange way to 'collumize'. However, here is a function that does what you described:
def columnize(A, interval=2):
ans =
for i in range(0,len(A), interval*2):
for j in range(min(interval, len(A)-i)):
ans.append(A[i+j] + (A[i+j+interval] if i+j+interval < len(A) else ))
return ans
add a comment |
I agree with the comments about this being a strange way to 'collumize'. However, here is a function that does what you described:
def columnize(A, interval=2):
ans =
for i in range(0,len(A), interval*2):
for j in range(min(interval, len(A)-i)):
ans.append(A[i+j] + (A[i+j+interval] if i+j+interval < len(A) else ))
return ans
I agree with the comments about this being a strange way to 'collumize'. However, here is a function that does what you described:
def columnize(A, interval=2):
ans =
for i in range(0,len(A), interval*2):
for j in range(min(interval, len(A)-i)):
ans.append(A[i+j] + (A[i+j+interval] if i+j+interval < len(A) else ))
return ans
answered Nov 13 '18 at 16:37
wowserxwowserx
42018
42018
add a comment |
add a comment |
Are you looking for something like this? Columnizing squared matrix?
start = [
[1, 'one'],
[2, 'two'],
[3, 'three'],
[4, 'four'],
[5, 'five'],
[6, 'six'],
[7, 'seven'],
[8, 'eight'],
[9, 'nine'],
]
expected = [
[1, 'one', 3, 'three'],
[2, 'two', 4, 'four'],
# page break
[5, 'five', 7, 'seven'],
[6, 'six', 8, 'eight'],
# page break
[9, 'nine'],
]
a = 2
r = a*a
ans =
for i in range(0, len(start), r):
l_tmp = start[i:i+r]
if l_tmp[::a]:
ans.append([item for sublist in l_tmp[::a] for item in sublist])
if l_tmp[1::a]:
ans.append([item for sublist in l_tmp[1::a] for item in sublist])
# You can easily add page break here
print(ans)
add a comment |
Are you looking for something like this? Columnizing squared matrix?
start = [
[1, 'one'],
[2, 'two'],
[3, 'three'],
[4, 'four'],
[5, 'five'],
[6, 'six'],
[7, 'seven'],
[8, 'eight'],
[9, 'nine'],
]
expected = [
[1, 'one', 3, 'three'],
[2, 'two', 4, 'four'],
# page break
[5, 'five', 7, 'seven'],
[6, 'six', 8, 'eight'],
# page break
[9, 'nine'],
]
a = 2
r = a*a
ans =
for i in range(0, len(start), r):
l_tmp = start[i:i+r]
if l_tmp[::a]:
ans.append([item for sublist in l_tmp[::a] for item in sublist])
if l_tmp[1::a]:
ans.append([item for sublist in l_tmp[1::a] for item in sublist])
# You can easily add page break here
print(ans)
add a comment |
Are you looking for something like this? Columnizing squared matrix?
start = [
[1, 'one'],
[2, 'two'],
[3, 'three'],
[4, 'four'],
[5, 'five'],
[6, 'six'],
[7, 'seven'],
[8, 'eight'],
[9, 'nine'],
]
expected = [
[1, 'one', 3, 'three'],
[2, 'two', 4, 'four'],
# page break
[5, 'five', 7, 'seven'],
[6, 'six', 8, 'eight'],
# page break
[9, 'nine'],
]
a = 2
r = a*a
ans =
for i in range(0, len(start), r):
l_tmp = start[i:i+r]
if l_tmp[::a]:
ans.append([item for sublist in l_tmp[::a] for item in sublist])
if l_tmp[1::a]:
ans.append([item for sublist in l_tmp[1::a] for item in sublist])
# You can easily add page break here
print(ans)
Are you looking for something like this? Columnizing squared matrix?
start = [
[1, 'one'],
[2, 'two'],
[3, 'three'],
[4, 'four'],
[5, 'five'],
[6, 'six'],
[7, 'seven'],
[8, 'eight'],
[9, 'nine'],
]
expected = [
[1, 'one', 3, 'three'],
[2, 'two', 4, 'four'],
# page break
[5, 'five', 7, 'seven'],
[6, 'six', 8, 'eight'],
# page break
[9, 'nine'],
]
a = 2
r = a*a
ans =
for i in range(0, len(start), r):
l_tmp = start[i:i+r]
if l_tmp[::a]:
ans.append([item for sublist in l_tmp[::a] for item in sublist])
if l_tmp[1::a]:
ans.append([item for sublist in l_tmp[1::a] for item in sublist])
# You can easily add page break here
print(ans)
answered Nov 13 '18 at 16:37
BlueSheepTokenBlueSheepToken
1,169415
1,169415
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%2f53284995%2fslice-list-of-lists-at-regular-interval-then-merge%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
Could you check your testcase, and if it is right this way, explain how you get it? I would think of
1-3-5-7-9 - 2-4-6-8
and not1-3 - 2-4 - 5-7 - 6-8 - 9
as a "columnisation" of a list– Black Owl Kai
Nov 13 '18 at 16:14
This makes more sense for 'columnise':
C=2; [[y for x in start[C*z:C*z+C] for y in x] for z in range(len(start)//C+1)]
– gustavovelascoh
Nov 13 '18 at 16:30