0
votes

I am trying to make a script where a '-' is put in between all odd digits in a given number (ie 991453 would be 9-9-145-3), but for some reason python wont allow me to insert a str into a list of integers. The error I keep on getting is 'TypeError: not all arguments converted during string formatting'

My code:

def DashInsert(text):

    list_int = map(int, list(text))

    for i in xrange(len(list_int)-1):
        if (list_int[i] % 2 == 1) and (list_int[i+1] % 2 == 1):
           print i
           list_int.insert(i+1,'-')

    return list_int

Here is my actual input and error:

999472

0

Traceback (most recent call last):

File "DashInsert.py", line 17, in

print DashInsert(string)

File "DashInsert.py", line 11, in DashInsert

if (list_int[i] % 2 == 1) and (list_int[i+1] % 2 == 1):

TypeError: not all arguments converted during string formatting

3
Please show the actual input and the actual error trace back.thefourtheye
FYI this is happening because you are modifying (specifically, adding to) the list while you are iterating over it. This is almost never a good idea. I mean, look what you are doing. You turn everything to ints first, but then you find the one you want and you insert a string in the next position. Hence, next round your % is string formatting!Two-Bit Alchemist
@Two-BitAlchemist I understand what you are saying but the if statement should not allow the inserted '-' to through right? So I dont understand why its not workingRidwan Kazi
What? The insert takes place inside the if statement. The '-' can only be inserted if that expression evaluates to true.Two-Bit Alchemist

3 Answers

1
votes

Your error is because you are modifying the list that you are iterating over. When you insert - into the list, that becomes the target of % and you get a TypeError.

In Python, % is an operator for string formatting and '-' is a string; that is why you get a less than clear error:

>>> '-' % 2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: not all arguments converted during string formatting

For strings you use % this way:

>>> 'x %s y %s %i' % ('and', 'is', 13)
'x and y is 13'

The fix to your code is to append to a separate list:

def DashInsert(s):

    list_int = map(int, s)

    rtr=[]

    for i, e in enumerate(list_int[0:-1]):
        rtr.append(str(e))
        if e % 2 == 1 and list_int[i+1] % 2 == 1:
           rtr.append('-')
    rtr.append(str(list_int[-1]))    

    return rtr
1
votes

You could do this through regex.

>>> import re
>>> s = 991453
>>> re.sub(r'(?<=[13579])(?=[13579])', r'-', str(s))
'9-9-145-3'
1
votes

I suspect this is horrible code but it works-

number = 991453

number_list = []
for i, item in enumerate(str(number)):
    try:
        if int(item) % 2 != 0 and int(str(number)[i + 1]) % 2 != 0:
            number_list.append(item + '-')
        else:
            number_list.append(item)
    except:
        number_list.append(item)
print(''.join(number_list))

Edit: Actually, there's no need to make a list so we can do this-

number = 991453

dash_number = ''
for i, item in enumerate(str(number)):
    try:
        if int(item) % 2 != 0 and int(str(number)[i + 1]) % 2 != 0:
            dash_number += item + '-'
        else:
            dash_number += item
    except:
        dash_number += item
print(dash_number)

Edit: Here's how to do it without the try/except.

number = 991453

dash_number = ''
for i, item in enumerate(str(number)[:-1]):
    if int(item) % 2 != 0 and int(str(number)[i + 1]) % 2 != 0:
        dash_number += item + '-'
    else:
        dash_number += item
dash_number += str(number)[-1]

print(dash_number)