Python Forum
list digit into number
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
list digit into number
#1
Hi, how can i change this code to work for two-digit numbers or three-digit number?

For now only work for single digit numbers, tried to change algorthm but didnt work.

def to_number(digits):
    result=digits[0]
    for el in digits[1:]:
        result=result*10
        result=result + el
    return result
Now the ouput is 2221 - i think is because of this line result=result + el
It has to output this:
input: [ 21, 12, 1]
output: 21121
Reply


Messages In This Thread
list digit into number - by Voldyy - Jul-10-2022, 12:51 PM
RE: list digit into number - by BashBedlam - Jul-10-2022, 05:02 PM
RE: list digit into number - by deanhystad - Jul-10-2022, 06:13 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Displaying list correspond to the column number danlopek14q 9 4,223 Aug-27-2021, 04:32 AM
Last Post: naughtyCat
  How to convert every even number in a list to odd? Bruizeh 4 3,995 Aug-27-2021, 03:04 AM
Last Post: naughtyCat
  Get the biggest number from a two dimensional list rs74 13 4,423 Aug-09-2020, 04:02 PM
Last Post: deanhystad
  Frequency in first digit from csv file, NO IMPORT bryan0901 6 3,001 May-28-2020, 09:50 AM
Last Post: bryan0901
  How can I print the number of unique elements in a list? AnOddGirl 5 3,488 Mar-24-2020, 05:47 AM
Last Post: AnOddGirl
  integer representing a 10-digit phone number critter70 5 27,803 May-17-2019, 09:29 AM
Last Post: saravanatn
  Four-digit number text translation. soz 3 2,814 May-13-2019, 03:02 PM
Last Post: buran
  create three digit numbers Krszt 4 4,668 Dec-09-2018, 03:12 PM
Last Post: ThePhi
  Sum of digit in a string MEH012 1 9,661 Apr-20-2018, 02:13 AM
Last Post: Larz60+
  Multiplying number in a list in an order pythoneer 12 6,940 Mar-23-2018, 08:21 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020