Binary to decimal in python

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
0
down vote

favorite












binaryToDecimal = lambda binary: sum([[int(2**index) for index in range(len(binary))][-index-1] for index, char in enumerate(binary) if int(char)])


I wrote a simple function that converts binary strings into decimal integers. How can this be improved/simplified?



I know about the int function, I wanted to implement the conversion myself.







share|improve this question





















  • what about using python: binaryToDecimal = lambda x: str(int(x,2))
    – Jean-François Fabre
    May 9 at 18:41










  • I'm aware that you can do that :) I just wanted to implement it myself
    – Hum4n01d
    May 9 at 18:41










  • Well, readability could be improved a lot by not using too much list comprehensions together :)
    – IEatBagels
    May 9 at 18:42






  • 1




    Definitely, I did that on purpose though to make it as compact as possible
    – Hum4n01d
    May 9 at 18:43






  • 2




    I added reinventing-the-wheel, because that's what you're doing here: inventing this even though a standard implementation exists. In the future, you may want to add this tag yourself, to signify to other users that you're knowingly not using the standard implementation.
    – Daniel
    May 9 at 19:28
















up vote
0
down vote

favorite












binaryToDecimal = lambda binary: sum([[int(2**index) for index in range(len(binary))][-index-1] for index, char in enumerate(binary) if int(char)])


I wrote a simple function that converts binary strings into decimal integers. How can this be improved/simplified?



I know about the int function, I wanted to implement the conversion myself.







share|improve this question





















  • what about using python: binaryToDecimal = lambda x: str(int(x,2))
    – Jean-François Fabre
    May 9 at 18:41










  • I'm aware that you can do that :) I just wanted to implement it myself
    – Hum4n01d
    May 9 at 18:41










  • Well, readability could be improved a lot by not using too much list comprehensions together :)
    – IEatBagels
    May 9 at 18:42






  • 1




    Definitely, I did that on purpose though to make it as compact as possible
    – Hum4n01d
    May 9 at 18:43






  • 2




    I added reinventing-the-wheel, because that's what you're doing here: inventing this even though a standard implementation exists. In the future, you may want to add this tag yourself, to signify to other users that you're knowingly not using the standard implementation.
    – Daniel
    May 9 at 19:28












up vote
0
down vote

favorite









up vote
0
down vote

favorite











binaryToDecimal = lambda binary: sum([[int(2**index) for index in range(len(binary))][-index-1] for index, char in enumerate(binary) if int(char)])


I wrote a simple function that converts binary strings into decimal integers. How can this be improved/simplified?



I know about the int function, I wanted to implement the conversion myself.







share|improve this question













binaryToDecimal = lambda binary: sum([[int(2**index) for index in range(len(binary))][-index-1] for index, char in enumerate(binary) if int(char)])


I wrote a simple function that converts binary strings into decimal integers. How can this be improved/simplified?



I know about the int function, I wanted to implement the conversion myself.









share|improve this question












share|improve this question




share|improve this question








edited May 9 at 19:24









Daniel

4,1132836




4,1132836









asked May 9 at 18:33









Hum4n01d

1065




1065











  • what about using python: binaryToDecimal = lambda x: str(int(x,2))
    – Jean-François Fabre
    May 9 at 18:41










  • I'm aware that you can do that :) I just wanted to implement it myself
    – Hum4n01d
    May 9 at 18:41










  • Well, readability could be improved a lot by not using too much list comprehensions together :)
    – IEatBagels
    May 9 at 18:42






  • 1




    Definitely, I did that on purpose though to make it as compact as possible
    – Hum4n01d
    May 9 at 18:43






  • 2




    I added reinventing-the-wheel, because that's what you're doing here: inventing this even though a standard implementation exists. In the future, you may want to add this tag yourself, to signify to other users that you're knowingly not using the standard implementation.
    – Daniel
    May 9 at 19:28
















  • what about using python: binaryToDecimal = lambda x: str(int(x,2))
    – Jean-François Fabre
    May 9 at 18:41










  • I'm aware that you can do that :) I just wanted to implement it myself
    – Hum4n01d
    May 9 at 18:41










  • Well, readability could be improved a lot by not using too much list comprehensions together :)
    – IEatBagels
    May 9 at 18:42






  • 1




    Definitely, I did that on purpose though to make it as compact as possible
    – Hum4n01d
    May 9 at 18:43






  • 2




    I added reinventing-the-wheel, because that's what you're doing here: inventing this even though a standard implementation exists. In the future, you may want to add this tag yourself, to signify to other users that you're knowingly not using the standard implementation.
    – Daniel
    May 9 at 19:28















what about using python: binaryToDecimal = lambda x: str(int(x,2))
– Jean-François Fabre
May 9 at 18:41




what about using python: binaryToDecimal = lambda x: str(int(x,2))
– Jean-François Fabre
May 9 at 18:41












I'm aware that you can do that :) I just wanted to implement it myself
– Hum4n01d
May 9 at 18:41




I'm aware that you can do that :) I just wanted to implement it myself
– Hum4n01d
May 9 at 18:41












Well, readability could be improved a lot by not using too much list comprehensions together :)
– IEatBagels
May 9 at 18:42




Well, readability could be improved a lot by not using too much list comprehensions together :)
– IEatBagels
May 9 at 18:42




1




1




Definitely, I did that on purpose though to make it as compact as possible
– Hum4n01d
May 9 at 18:43




Definitely, I did that on purpose though to make it as compact as possible
– Hum4n01d
May 9 at 18:43




2




2




I added reinventing-the-wheel, because that's what you're doing here: inventing this even though a standard implementation exists. In the future, you may want to add this tag yourself, to signify to other users that you're knowingly not using the standard implementation.
– Daniel
May 9 at 19:28




I added reinventing-the-wheel, because that's what you're doing here: inventing this even though a standard implementation exists. In the future, you may want to add this tag yourself, to signify to other users that you're knowingly not using the standard implementation.
– Daniel
May 9 at 19:28










1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










Of course, the most efficient solution would be to let python parse the binary string as integer ;)



binaryToDecimal = lambda x: int(x,2)


But you already know that, so what about simplifying your code like this:



binaryToDecimal = lambda binary: sum(1<<(len(binary)-index-1) for index, char in enumerate(binary) if char=='1')


  • removing the creation of the inner list comprehension just to pick one value

  • not using power but bitshift (integer computation, faster, more accurate)

  • don't create a list to pass to sum, pass a generator comprehension (it's faster, saves memory)

  • in your condition, don't convert your number to integer, just test against the char value (it's faster)

Keep in mind that your parsing has no error checking!






share|improve this answer





















  • Funny enough, I tested the character at first but moved to int to save characters (I was challenging myself to use the least characters)
    – Hum4n01d
    May 10 at 0:35











  • yes, it's good for code golfing, but not really for performance :)
    – Jean-François Fabre
    May 10 at 15:12










Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

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: "196"
;
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',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f194037%2fbinary-to-decimal-in-python%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote



accepted










Of course, the most efficient solution would be to let python parse the binary string as integer ;)



binaryToDecimal = lambda x: int(x,2)


But you already know that, so what about simplifying your code like this:



binaryToDecimal = lambda binary: sum(1<<(len(binary)-index-1) for index, char in enumerate(binary) if char=='1')


  • removing the creation of the inner list comprehension just to pick one value

  • not using power but bitshift (integer computation, faster, more accurate)

  • don't create a list to pass to sum, pass a generator comprehension (it's faster, saves memory)

  • in your condition, don't convert your number to integer, just test against the char value (it's faster)

Keep in mind that your parsing has no error checking!






share|improve this answer





















  • Funny enough, I tested the character at first but moved to int to save characters (I was challenging myself to use the least characters)
    – Hum4n01d
    May 10 at 0:35











  • yes, it's good for code golfing, but not really for performance :)
    – Jean-François Fabre
    May 10 at 15:12














up vote
2
down vote



accepted










Of course, the most efficient solution would be to let python parse the binary string as integer ;)



binaryToDecimal = lambda x: int(x,2)


But you already know that, so what about simplifying your code like this:



binaryToDecimal = lambda binary: sum(1<<(len(binary)-index-1) for index, char in enumerate(binary) if char=='1')


  • removing the creation of the inner list comprehension just to pick one value

  • not using power but bitshift (integer computation, faster, more accurate)

  • don't create a list to pass to sum, pass a generator comprehension (it's faster, saves memory)

  • in your condition, don't convert your number to integer, just test against the char value (it's faster)

Keep in mind that your parsing has no error checking!






share|improve this answer





















  • Funny enough, I tested the character at first but moved to int to save characters (I was challenging myself to use the least characters)
    – Hum4n01d
    May 10 at 0:35











  • yes, it's good for code golfing, but not really for performance :)
    – Jean-François Fabre
    May 10 at 15:12












up vote
2
down vote



accepted







up vote
2
down vote



accepted






Of course, the most efficient solution would be to let python parse the binary string as integer ;)



binaryToDecimal = lambda x: int(x,2)


But you already know that, so what about simplifying your code like this:



binaryToDecimal = lambda binary: sum(1<<(len(binary)-index-1) for index, char in enumerate(binary) if char=='1')


  • removing the creation of the inner list comprehension just to pick one value

  • not using power but bitshift (integer computation, faster, more accurate)

  • don't create a list to pass to sum, pass a generator comprehension (it's faster, saves memory)

  • in your condition, don't convert your number to integer, just test against the char value (it's faster)

Keep in mind that your parsing has no error checking!






share|improve this answer













Of course, the most efficient solution would be to let python parse the binary string as integer ;)



binaryToDecimal = lambda x: int(x,2)


But you already know that, so what about simplifying your code like this:



binaryToDecimal = lambda binary: sum(1<<(len(binary)-index-1) for index, char in enumerate(binary) if char=='1')


  • removing the creation of the inner list comprehension just to pick one value

  • not using power but bitshift (integer computation, faster, more accurate)

  • don't create a list to pass to sum, pass a generator comprehension (it's faster, saves memory)

  • in your condition, don't convert your number to integer, just test against the char value (it's faster)

Keep in mind that your parsing has no error checking!







share|improve this answer













share|improve this answer



share|improve this answer











answered May 9 at 18:46









Jean-François Fabre

783210




783210











  • Funny enough, I tested the character at first but moved to int to save characters (I was challenging myself to use the least characters)
    – Hum4n01d
    May 10 at 0:35











  • yes, it's good for code golfing, but not really for performance :)
    – Jean-François Fabre
    May 10 at 15:12
















  • Funny enough, I tested the character at first but moved to int to save characters (I was challenging myself to use the least characters)
    – Hum4n01d
    May 10 at 0:35











  • yes, it's good for code golfing, but not really for performance :)
    – Jean-François Fabre
    May 10 at 15:12















Funny enough, I tested the character at first but moved to int to save characters (I was challenging myself to use the least characters)
– Hum4n01d
May 10 at 0:35





Funny enough, I tested the character at first but moved to int to save characters (I was challenging myself to use the least characters)
– Hum4n01d
May 10 at 0:35













yes, it's good for code golfing, but not really for performance :)
– Jean-François Fabre
May 10 at 15:12




yes, it's good for code golfing, but not really for performance :)
– Jean-François Fabre
May 10 at 15:12












 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f194037%2fbinary-to-decimal-in-python%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Chat program with C++ and SFML

Function to Return a JSON Like Objects Using VBA Collections and Arrays

Will my employers contract hold up in court?