Make every four elements into a single number (element) - python
So how to the title description?
So the list:
l=[1,2,3,4,5,6,7,8]
Desired output:
[1234,5678]
So merge the every four packs of elements!
I am thinking about a list comprehension, but it's not a working well (it is, it's below my question)
python list merge element
add a comment |
So how to the title description?
So the list:
l=[1,2,3,4,5,6,7,8]
Desired output:
[1234,5678]
So merge the every four packs of elements!
I am thinking about a list comprehension, but it's not a working well (it is, it's below my question)
python list merge element
add a comment |
So how to the title description?
So the list:
l=[1,2,3,4,5,6,7,8]
Desired output:
[1234,5678]
So merge the every four packs of elements!
I am thinking about a list comprehension, but it's not a working well (it is, it's below my question)
python list merge element
So how to the title description?
So the list:
l=[1,2,3,4,5,6,7,8]
Desired output:
[1234,5678]
So merge the every four packs of elements!
I am thinking about a list comprehension, but it's not a working well (it is, it's below my question)
python list merge element
python list merge element
asked Nov 13 '18 at 0:02
U9-ForwardU9-Forward
16.6k51543
16.6k51543
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
You can get the elements from slicing index of the range, we can do that because of the step
argument (last one), that make that sequence into an integer (number).
Note: i am ordering them by rank, so (best on top, and worst at bottom)
Option 1:
list comprehension
:
>>> [int(''.join(map(str,l[i:i+4]))) for i in range(0,len(l),4)]
[1234, 5678]
>>>
Option 2:
map
:
>>> list(map(lambda i: int(''.join(map(str,l[i:i+4]))),range(0,len(l),4)))
[1234, 5678]
>>>
1
Note: Option 1 is the better option, both on readability and performance; any time amap
would require the use oflambda
that the equivalent listcomp/genexpr can inline, themap
will be slower. The use ofmap
in option 1 is fine (becausestr
is an existing function implemented in C which couldn't be avoided in an equivalent listcomp anyway).
– ShadowRanger
Nov 13 '18 at 0:13
@ShadowRanger Yes sir, That's how i order them :-), i know that :D
– U9-Forward
Nov 13 '18 at 0:16
add a comment |
Assume the length of l
is the multiple of 4 and each element of l
is an integer (1 to 9)
Here is another option without using string.
[1000*l[4*i]+100*l[4*i+1]+10*l[4*i+2]+l[4*i+3] for i in range(len(l)//4)]
Ouch, Little unreadable, and inefficient..., but thanks for it.
– U9-Forward
Nov 13 '18 at 0:19
Note:range(len(l)/4)
should berange(len(l)//4)
, use//
not/
.
– U9-Forward
Nov 13 '18 at 0:20
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%2f53271871%2fmake-every-four-elements-into-a-single-number-element-python%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
You can get the elements from slicing index of the range, we can do that because of the step
argument (last one), that make that sequence into an integer (number).
Note: i am ordering them by rank, so (best on top, and worst at bottom)
Option 1:
list comprehension
:
>>> [int(''.join(map(str,l[i:i+4]))) for i in range(0,len(l),4)]
[1234, 5678]
>>>
Option 2:
map
:
>>> list(map(lambda i: int(''.join(map(str,l[i:i+4]))),range(0,len(l),4)))
[1234, 5678]
>>>
1
Note: Option 1 is the better option, both on readability and performance; any time amap
would require the use oflambda
that the equivalent listcomp/genexpr can inline, themap
will be slower. The use ofmap
in option 1 is fine (becausestr
is an existing function implemented in C which couldn't be avoided in an equivalent listcomp anyway).
– ShadowRanger
Nov 13 '18 at 0:13
@ShadowRanger Yes sir, That's how i order them :-), i know that :D
– U9-Forward
Nov 13 '18 at 0:16
add a comment |
You can get the elements from slicing index of the range, we can do that because of the step
argument (last one), that make that sequence into an integer (number).
Note: i am ordering them by rank, so (best on top, and worst at bottom)
Option 1:
list comprehension
:
>>> [int(''.join(map(str,l[i:i+4]))) for i in range(0,len(l),4)]
[1234, 5678]
>>>
Option 2:
map
:
>>> list(map(lambda i: int(''.join(map(str,l[i:i+4]))),range(0,len(l),4)))
[1234, 5678]
>>>
1
Note: Option 1 is the better option, both on readability and performance; any time amap
would require the use oflambda
that the equivalent listcomp/genexpr can inline, themap
will be slower. The use ofmap
in option 1 is fine (becausestr
is an existing function implemented in C which couldn't be avoided in an equivalent listcomp anyway).
– ShadowRanger
Nov 13 '18 at 0:13
@ShadowRanger Yes sir, That's how i order them :-), i know that :D
– U9-Forward
Nov 13 '18 at 0:16
add a comment |
You can get the elements from slicing index of the range, we can do that because of the step
argument (last one), that make that sequence into an integer (number).
Note: i am ordering them by rank, so (best on top, and worst at bottom)
Option 1:
list comprehension
:
>>> [int(''.join(map(str,l[i:i+4]))) for i in range(0,len(l),4)]
[1234, 5678]
>>>
Option 2:
map
:
>>> list(map(lambda i: int(''.join(map(str,l[i:i+4]))),range(0,len(l),4)))
[1234, 5678]
>>>
You can get the elements from slicing index of the range, we can do that because of the step
argument (last one), that make that sequence into an integer (number).
Note: i am ordering them by rank, so (best on top, and worst at bottom)
Option 1:
list comprehension
:
>>> [int(''.join(map(str,l[i:i+4]))) for i in range(0,len(l),4)]
[1234, 5678]
>>>
Option 2:
map
:
>>> list(map(lambda i: int(''.join(map(str,l[i:i+4]))),range(0,len(l),4)))
[1234, 5678]
>>>
edited Nov 13 '18 at 0:15
answered Nov 13 '18 at 0:02
U9-ForwardU9-Forward
16.6k51543
16.6k51543
1
Note: Option 1 is the better option, both on readability and performance; any time amap
would require the use oflambda
that the equivalent listcomp/genexpr can inline, themap
will be slower. The use ofmap
in option 1 is fine (becausestr
is an existing function implemented in C which couldn't be avoided in an equivalent listcomp anyway).
– ShadowRanger
Nov 13 '18 at 0:13
@ShadowRanger Yes sir, That's how i order them :-), i know that :D
– U9-Forward
Nov 13 '18 at 0:16
add a comment |
1
Note: Option 1 is the better option, both on readability and performance; any time amap
would require the use oflambda
that the equivalent listcomp/genexpr can inline, themap
will be slower. The use ofmap
in option 1 is fine (becausestr
is an existing function implemented in C which couldn't be avoided in an equivalent listcomp anyway).
– ShadowRanger
Nov 13 '18 at 0:13
@ShadowRanger Yes sir, That's how i order them :-), i know that :D
– U9-Forward
Nov 13 '18 at 0:16
1
1
Note: Option 1 is the better option, both on readability and performance; any time a
map
would require the use of lambda
that the equivalent listcomp/genexpr can inline, the map
will be slower. The use of map
in option 1 is fine (because str
is an existing function implemented in C which couldn't be avoided in an equivalent listcomp anyway).– ShadowRanger
Nov 13 '18 at 0:13
Note: Option 1 is the better option, both on readability and performance; any time a
map
would require the use of lambda
that the equivalent listcomp/genexpr can inline, the map
will be slower. The use of map
in option 1 is fine (because str
is an existing function implemented in C which couldn't be avoided in an equivalent listcomp anyway).– ShadowRanger
Nov 13 '18 at 0:13
@ShadowRanger Yes sir, That's how i order them :-), i know that :D
– U9-Forward
Nov 13 '18 at 0:16
@ShadowRanger Yes sir, That's how i order them :-), i know that :D
– U9-Forward
Nov 13 '18 at 0:16
add a comment |
Assume the length of l
is the multiple of 4 and each element of l
is an integer (1 to 9)
Here is another option without using string.
[1000*l[4*i]+100*l[4*i+1]+10*l[4*i+2]+l[4*i+3] for i in range(len(l)//4)]
Ouch, Little unreadable, and inefficient..., but thanks for it.
– U9-Forward
Nov 13 '18 at 0:19
Note:range(len(l)/4)
should berange(len(l)//4)
, use//
not/
.
– U9-Forward
Nov 13 '18 at 0:20
add a comment |
Assume the length of l
is the multiple of 4 and each element of l
is an integer (1 to 9)
Here is another option without using string.
[1000*l[4*i]+100*l[4*i+1]+10*l[4*i+2]+l[4*i+3] for i in range(len(l)//4)]
Ouch, Little unreadable, and inefficient..., but thanks for it.
– U9-Forward
Nov 13 '18 at 0:19
Note:range(len(l)/4)
should berange(len(l)//4)
, use//
not/
.
– U9-Forward
Nov 13 '18 at 0:20
add a comment |
Assume the length of l
is the multiple of 4 and each element of l
is an integer (1 to 9)
Here is another option without using string.
[1000*l[4*i]+100*l[4*i+1]+10*l[4*i+2]+l[4*i+3] for i in range(len(l)//4)]
Assume the length of l
is the multiple of 4 and each element of l
is an integer (1 to 9)
Here is another option without using string.
[1000*l[4*i]+100*l[4*i+1]+10*l[4*i+2]+l[4*i+3] for i in range(len(l)//4)]
edited Nov 13 '18 at 0:24
answered Nov 13 '18 at 0:17
Banghua ZhaoBanghua Zhao
1,2871721
1,2871721
Ouch, Little unreadable, and inefficient..., but thanks for it.
– U9-Forward
Nov 13 '18 at 0:19
Note:range(len(l)/4)
should berange(len(l)//4)
, use//
not/
.
– U9-Forward
Nov 13 '18 at 0:20
add a comment |
Ouch, Little unreadable, and inefficient..., but thanks for it.
– U9-Forward
Nov 13 '18 at 0:19
Note:range(len(l)/4)
should berange(len(l)//4)
, use//
not/
.
– U9-Forward
Nov 13 '18 at 0:20
Ouch, Little unreadable, and inefficient..., but thanks for it.
– U9-Forward
Nov 13 '18 at 0:19
Ouch, Little unreadable, and inefficient..., but thanks for it.
– U9-Forward
Nov 13 '18 at 0:19
Note:
range(len(l)/4)
should be range(len(l)//4)
, use //
not /
.– U9-Forward
Nov 13 '18 at 0:20
Note:
range(len(l)/4)
should be range(len(l)//4)
, use //
not /
.– U9-Forward
Nov 13 '18 at 0:20
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%2f53271871%2fmake-every-four-elements-into-a-single-number-element-python%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