Home > Mobile >  how to convert a string of numbers to a list and square up ** and get it printed out back as string
how to convert a string of numbers to a list and square up ** and get it printed out back as string

Time:11-05

Given a string of the form "3,9,13,4,42". It is necessary to convert it into a list and calculate its square for each element. Then join the squares of those elements back into a string and print it in the console. input

input: string = "3,9,13,4,42"

output: string= "9,81,169,16,1764"

Managed to get it squared up, tried converting it to list fist, but when checked type at the end, always somehow getting it as tuple. Ty for help.

CodePudding user response:

Hope this answers your question.

# input
str_numbers = "3,9,13,4,42"

# string to list
str_number_list = str_numbers.split(",")

# list of strings to list of ints
number_list = [int(x) for x in str_number_list]

# square all numbers
squared_numbers = [x ** 2 for x in number_list]

# squared numbers back to a list of strings
str_squared_numbers = [str(x) for x in squared_numbers]

# joing the list items into one string
result = ",".join(str_squared_numbers)

# print it out
print(f"Input: {str_numbers}")
print(f"Output: {result}")

CodePudding user response:


// First Approach

string = "3,9,13,4,42"
array = string.split(',')
array = map(lambda x: str(int(x)**2),array)
result = ','.join(list(array))
print(result) // "9,81,169,16,1764"

// Second Approach
string = "3,9,13,4,42"
result = ','.join([str(int(x)**2) for x in string.split(',')])
print(result) // '9,81,169,16,1764'



CodePudding user response:

Can do with split join,

In [1]: ','.join(map(lambda x: str(int(x)**2), s.split(',')))
Out[1]: '9,81,169,16,1764'

CodePudding user response:

You have so many ways to solve the problem. I'll show you a few by degree of complexity of understanding. Each way has a different computational complexity, but for such a problem we will not go into detail.

N.B.: Casting should be done at float and not at int because it is not certain a priori that there are integers in the input string.


Using List Comprehension

List comprehension offers a shorter syntax when you want to create a new list based on the values of an existing list.

I divide the code into 3 steps just for greater understanding:

input_string = "3,9,13,4,42"

num_list = [float(x) for x in input_string.split(",")]  # split list by comma and cast each element to float
squares_list = [x**2 for x in num_list]  # make square of each number in list
output_string = [str(x) for x in squares_list]  # cast to string each element in list

print(output_string)

Using join, map and lambda

The join() method takes all items in an iterable and joins them into one string.

The map() function applies a given function to each item of an iterable (list, tuple etc.) and returns an iterator.

A Lambda Function is an anonymous function, that is, a function that has not been assigned a name and is used to use the features of functions but without having to define them.

output_string = ','.join(map(lambda x: str(float(x)**2), input_string.split(',')))

CodePudding user response:

You are absolutely right assume-irrational-is-rational

string = "3,9,13,4,42"

def squares_string(string):
    output = ",".join(tuple(map(lambda x: str(int(x)**2), "3,9,13,4,42".split(","))))

    return output

output = squares_string(string)
print(output)
print(type(output))

Result:

9,81,169,16,1764
<class 'str'>
  • Related