2

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!!

LokiTheGAMER
  • 31
  • 1
  • 4
  • In `numslist` there are not only two-character strings ('0', '1', ...). Also, there is no such thing as a "two digit" integer. This being said, I am not sure about how your desired output looks like. Could you maybe provide the list you want to obtain? – offeltoffel Dec 05 '18 at 15:54
  • also are `numslist` and `numlist` in your function supposed to be the same. Like @offeltoffel said, could you give a complete example of how you would use this function. – ab15 Dec 05 '18 at 15:58
  • Probably not the spirit of this assignment, but `map(ast.literal_eval, numslist)` – user3483203 Dec 05 '18 at 16:20

3 Answers3

1

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]
jpp
  • 159,742
  • 34
  • 281
  • 339
0

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))
Woody1193
  • 7,252
  • 5
  • 40
  • 90
0

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)
kantal
  • 2,331
  • 2
  • 8
  • 15