python – converting a list of 2 digit string numbers to a list of 2 digit integers

Question:

I have a list of 2 character strings of numbers,
I’m trying to write a function to convert this to a list of 2 digit integers without using int() or knowing the length of the list, this is my code so far:

intslist = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
            19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
            36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52,
            53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
            70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86,
            87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]

numslist = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12',
            '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23',
            '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34',
            '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45',
            '46', '47', '48', '49', '50', '51', '52', '53', '54', '55', '56',
            '57', '58', '59', '60', '61', '62', '63', '64', '65', '66', '67',
            '68', '69', '70', '71', '72', '73', '74', '75', '76', '77', '78',
            '79', '80', '81', '82', '83', '84', '85', '86', '87', '88', '89',
            '90', '91', '92', '93', '94', '95', '96', '97', '98', '99']

def convert_num(numlist,list1,list2):
    returnlist = []
    templist = []
    convertdict = {k:v for k,v in zip(list1,list2)}
    p = 0
    num = ''.join(numlist)
    for c in num:
        templist.append(convertdict[num[p]])
        p += 2
    for i in templist:
    if templist[i] % 2 == 0:
        returnlist.append()
    return returnlist

this works but only returns a list of the individual digits, not the 2 digits i want.

I’m only a beginner and don’t really know how to proceed.
Any help appreciated!!

Asked By: LokiTheGAMER

||

Answers:

An integer is an integer. “Two digit integers” don’t exist as a concept.

Without using int or len, to return an integer from a string, you can reverse a string, use ord instead of int, multiply by 10k and sum:

x = '84'

res = sum((ord(val)-48)*10**idx for idx, val in enumerate(reversed(x)))  # 84

You can use map to apply the logic to every string in a list:

def str_to_int(x):
    return sum((ord(val)-48)*10**idx for idx, val in enumerate(reversed(x)))

res = list(map(str_to_int, numslist))

print(res)

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
 ...
 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
Answered By: jpp

The core of your solution will be taking the string and converting it to an integer:

def str_to_int(number):
    return sum((ord(c) - 48) * (10 ** i) for i, c in enumerate(number[::-1]))

This method takes your number in, enumerates over it from the end and then converts the ASCII value of each character to its numeric representation and then makes sure it will occupy the proper digit in the overall number.

From there, you can use map to convert the entire list:

intsList = list(map(str_to_int, numsList))
Answered By: Woody1193

The very simple solution:

dd={ str(i):i for i in range(10) } # {"0":0,"1":1,..."9":9}
rslt=[]
for ns in numslist: 
        n=0 
        for i in range(len(ns)): 
            n=10*n+dd[ns[i]] 
        rslt.append(n)
Answered By: kantal
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.