How to use sep command with sorted
Question:
I want to separate sorted numbers with "<", but can’t do it.
Here is the code:
numbers = [3, 7, 5]
print(sorted(numbers), sep="<")
Answers:
The *
operator as mentioned by @MisterMiyagi, can be used to unpack the list variables and use the sep
.
Code:
print(*sorted(numbers), sep="<")
I dont know if this is the answer you want, but I have made a python code to seperate the sorted numbers with "<" with join after I convert the numbers to strings.
As the items in the iterable must be string types, I first use a list comprehension to create a list containing each interger as a string, and pass this as input to str.join()
# Initial String
test_str = [5,1,2,3,4]
# Sorting number
sortedNum = sorted(test_str)
# Changing numbers into string
string_ints = [str(int) for int in sortedNum]
# Joining the sorted string with "<"
output = '<'.join(string_ints)
print(output)
I want to separate sorted numbers with "<", but can’t do it.
Here is the code:
numbers = [3, 7, 5]
print(sorted(numbers), sep="<")
The *
operator as mentioned by @MisterMiyagi, can be used to unpack the list variables and use the sep
.
Code:
print(*sorted(numbers), sep="<")
I dont know if this is the answer you want, but I have made a python code to seperate the sorted numbers with "<" with join after I convert the numbers to strings.
As the items in the iterable must be string types, I first use a list comprehension to create a list containing each interger as a string, and pass this as input to str.join()
# Initial String
test_str = [5,1,2,3,4]
# Sorting number
sortedNum = sorted(test_str)
# Changing numbers into string
string_ints = [str(int) for int in sortedNum]
# Joining the sorted string with "<"
output = '<'.join(string_ints)
print(output)