10

I have a code for finding the possible combinations of a given string in the list. But facing an issue with leading zero getting error like SyntaxError: leading zeros in decimal integer literals are not permitted; use an 0o prefix for octal integers . How to overcome this issue as I wanted to pass values with leading zeros(Cannot edit the values manually all the time). Below is my code

def permute_string(str):
    if len(str) == 0:
        return ['']
    prev_list = permute_string(str[1:len(str)])
    next_list = []
    for i in range(0,len(prev_list)):
        for j in range(0,len(str)):
            new_str = prev_list[i][0:j]+str[0]+prev_list[i][j:len(str)-1]
            if new_str not in next_list:
                next_list.append(new_str)
    return next_list

list = [129, 831 ,014]
length = len(list)
i = 0

# Iterating using while loop
while i < length:
    a = list[i]
    print(permute_string(str(a)))
    i += 1;
Midhun
  • 331
  • 2
  • 5
  • 15
  • 3
    Where do the numbers with 0s come from? If they are in your code, just do not write them. Also, please remove everything not related to your question (only the line `list = [129, 831 ,014]` is related, everything else is not.) *Also*, remove one of the tags 3.x or 2.7: these two languages handle the leading zeros differently. – DYZ Jul 22 '20 at 04:10
  • You can use a string instead of a number: `list = ['129', '831' ,'014']` – 정도유 Jul 22 '20 at 04:32
  • 2
    „I wanted to pass values with leading zeros“ please clarify whether you actually want to pass integer *literals* with leading zeroes or integer *values* with leading zeroes. The two need very different approaches-the first needs embedding in a string, the latter is not well-defined and needs an entirely different program design. – MisterMiyagi Jul 22 '20 at 04:41
  • Is there any way to convert Interger list to string with leading zeros? – Midhun Jul 22 '20 at 04:57
  • 1
    Integers do not have leading zeroes. They are just values — there is no way to distinguish *the value* of 0xA0 from 0o12 from 10, nor from the same value created with any amount of leading zeroes (by calling int instead of using a literal). Once you create a list of integers „with leading zeroes“ these zeroes are gone. – MisterMiyagi Jul 22 '20 at 05:05
  • Note that 0 with leading 0s is still allowed, try 00 or 000 etc.. – abdelgha4 Jun 25 '23 at 01:17

2 Answers2

17

Integer literals starting with 0 are illegal in python (other than zero itself, obviously), because of ambiguity. An integer literal starting with 0 has the following character which determines what number system it falls into: 0x for hex, 0o for octal, 0b for binary.

As for the integers themselves, they're just numbers, and numbers will never begin with zero. If you have an integer represented as a string, and it happens to have a leading zero, then it'll get ignored when you turn it into an integer:

>>> print(int('014'))
14

Given what you're trying to do here, I'd just rewrite the list's initial definition:

lst = ['129', '831', '014']
...
while i < length:
    a = lst[i]
    print(permute_string(a))  # a is already a string, so no need to cast to str()

or, if you need lst to be a list of integers, specifically, then you can change how you convert them to strings by using a format-string literal, instead of the str() call, which allows you to pad the number with zeros:

lst = [129, 831, 14]
...
while i < length:
    a = lst[i]
    print(permute_string(f'{a:03}'))  # three characters wide, add leading 0 if necessary

throughout this answer, I use lst as the variable name, instead of the list in your question. You shouldn't use list as a variable name, because it's also the keyword that represents the built-in list datastructure, and if you name a variable that then you can no longer use the keyword.

Green Cloak Guy
  • 23,793
  • 4
  • 33
  • 53
  • 2
    Just a note, integer literals with leading zeros were OK in Python 2 but I think they defaulted to Octal, this was changed with Python 3. Your description of the current state of affairs is completely accurate. I consider it unfortunate that Python made this decision, because leading zeros shouldn't be significant. – Mark Ransom Jul 22 '20 at 04:26
  • @Green Cloak Guy, I cannot edit the values as 2digit as the values will be always 3digits (Contains leading zero as well). – Midhun Jul 22 '20 at 04:34
  • @Midhun If someone else gives you the values, then it doesn't matter. It's only if you're writing them in your own code that it matters how you write them - like I said, _integer literal_, not _integer_. Why can't you edit your own code to change how `lst` is defined? – Green Cloak Guy Jul 22 '20 at 12:35
  • @ Green Cloak Guy , Actually I am not creating the list values as it from a different sources with Integer data type. I wanted to automate this process rather than modifying it manually as a string.At least if I could able to handle the leading zero values then v can interchange the 0, as am trying to get the possible combination of given numbers, like for 012 will give the same output of 210. – Midhun Jul 23 '20 at 04:48
  • 2
    There's of course one exception to the rule that integer literals can't start with 0, and that's 0 itself. (Building parsers is fun, really.) – MSalters Nov 10 '21 at 11:18
  • 00 or 000 etc.. is still allowed – abdelgha4 Jun 25 '23 at 01:19
-2

Finally, I got my answer. Below is the working code.

def permute_string(str):
    if len(str) == 0:
        return ['']
    prev_list = permute_string(str[1:len(str)])
    next_list = []
    for i in range(0,len(prev_list)):
        for j in range(0,len(str)):
            new_str = prev_list[i][0:j]+str[0]+prev_list[i][j:len(str)-1]
            if new_str not in next_list:
                next_list.append(new_str)
    return next_list

#Number should not be with leading Zero
actual_list = '129, 831 ,054, 845,376,970,074,345,175,965,068,287,164,230,250,983,064'
list = actual_list.split(',')
length = len(list)
i = 0

# Iterating using while loop
while i < length:
    a = list[i]
    print(permute_string(str(a)))
    i += 1;
Midhun
  • 331
  • 2
  • 5
  • 15