Home > Blockchain >  Joining loop output integers as a string?
Joining loop output integers as a string?

Time:02-28

This is my code so far. It takes numbers as STRINGS and if needed splits them down. Then converts them from OCTAL to decimal. However, in the cases where multiple numbers come as a single string; after conversion, I need to reassemble them back into a single string again. The same format in which they arrived.

dataset = "120 156 206" #Just a testing sample
delimiter = " "
if delimiter in dataset:
    text = dataset.strip('"')
    text = text.split()
    for i in text:
        c = int(i, base=8)
        var1 = "".join(str(c))
        print(var1)
else:
    c = int(dataset, base=8)
    print(c)

The expected output should be: "80 110 134"

I've tried:

d = " ".join(str(c))

OUTPUT:

8 0
1 1 0
1 3 4


d  = " ".join(str(c))

OUTPUT:

1 3 48 0
1 3 48 01 1 0
1 3 48 01 1 01 3 4

Also all kinds of other stuff that seems to just break everything, Type Errors etc... Without the .join this is the closest I have got so far:

80
110
134

CodePudding user response:

The problem is that print ends with a newline, that you don't want

  • Either change the end. Note that you don't a "".join(str(c)), just use c

    for i in text:
        c = int(i, base=8)
        print(c, end=" ")
    
  • Or save them in a loop, and print them all at once

    result = []
    for i in text:
        result.append(str(int(i, base=8)))
    print(" ".join(result))
    

Note that you neither need the strip nor the if/else and can combine all that in a list comprehension

dataset = "120"
result = [str(int(i, base=8)) for i in dataset.split()]
print(" ".join(result))

CodePudding user response:

I'd do it with a comprehension. Split the data at whitespace, turn the element into an int with base=8, turn that back into a string, and join the result. You don't need to check for a delimiter, split() does that for you (and returns a list with just one element if there is no delimiter, so you don't need to care for that).

" ".join(str(int(i, base=8)) for i in dataset.split())

CodePudding user response:

This code gives the output you needed:

dataset = "120 156 206" #Just a testing sample
delimiter = " "
if delimiter in dataset:
    text = dataset.strip('"')
    text = text.split(delimiter)
    c = []
    for i in text:
        c.append(str(int(i, base=8)))
    var1 = " ".join(c)
    print(var1)
else:
    c = int(dataset, base=8)
    print(c)

CodePudding user response:

Assuming you only need to print the result and not save it as a variable, you don't need join, convert to str or check for the delimiter at all. Using the default value of the sep argument of the print function (which is a space), you can do all that in a single simple line:

print(*(int(oct, 8) for oct in dataset.split()))

This unpacks the result of converting each space-separated element from the string to an int in octal base. Then the elements are separated by space by default (as explained above) and print takes care of converting to string for you.

CodePudding user response:

You don't have to care whether or not delimiters are used. Python will take care of that.

With that in mind, where's one verbose way to do this, and it would work even if you used a single number. Take note of singular/plural variable names to guide you:

dataset = "120 156 206" #Just a testing sample
delimiter = " "

numbers_as_octal_string = dataset.split(delimiter)
numbers_as_decimal_string = []


for number_as_octal_string in numbers_as_octal_string:
    number_as_decimal = int(number_as_octal_string, 8)
    number_as_decimal_string = str(number_as_decimal)
    numbers_as_decimal_string.append(number_as_decimal_string)

print(delimiter.join(numbers_as_decimal_string))

Having done so, there is a trick in Python called list comprehensions, and they can make your job a whole lot easier by doing a lot of the work in one line. It is also the "Pythonic" way of doing things (though I'd realistically create at least one or two more variables).

dataset = "120 156 206" #Just a testing sample
delimiter = " "

print(delimiter.join([str(int(number_as_octal_string)) for number_as_octal_string in dataset.split(delimiter)]))

CodePudding user response:

Have none of you got anything better to do on a Sunday afternoon?

I started writing this at 3 responses, there are now 7.

I'm not sure if I can tick more than one as the answer. They all work. This is the most difficult thing about Python, there seem to be so many ways of doing the same thing. For someone who is not neuro-typical and starts to overthink things very easily, I quickly become paralysed.

@Tomerikoo - I do need to output a variable. I've kept it as a print so far just so I can see the resultant output. It will form a function.

For this time, I have gone with @azro's solution. Though if possible I will try and tick others also. Not sure if it will allow me to do that though.

Finally, I have copied all your solutions into my notebook. So when my shockingly bad memory fails me in the future. You will be helping me again.

  • Related