+ 1
Can anyone explain this
X=sorted(list(set(input))) And join in python
3 Answers
+ 1
Regarding your first question, what your code really does is get the input (you haven't put brackets around 'input', so it's not valid here), remove all the duplicates, convert the string into a list and sort it in the ascending order (using a sorting algorithm known as Timsort). Now, considering your second one, str.join is used to convert a list into a string with the appropriate joining characters. For example, suppose that you have a list ["67", "78", "9"] and you want to display it as "67, 78, 9". One way to do this is to use this code:
for x in range(len(list))
print(list[x] + ", " * (x < len(list) - 1, end="")
But that would be too tedious to do. Here's where the join function provides an alternative way;
print(", ".join(list))
Hope this helps.
+ 4
That's broken code so it is hard to say much more than that.
It causes this error message:
1
X=sorted(list(set(input)))
Traceback (most recent call last):
File "file0.py", line 1, in <module>
X=sorted(list(set(input)))
TypeError: 'builtin_function_or_method' object is not iterable
The following code is vaguely similar and prints the inputted words in alphabetical order:
X=sorted(input().split())
print(X)
For example,
If you input: Hello World How Are You
It will print:
['Are', 'Hello', 'How', 'World', 'You']
0
And why we use join in python.... In joining the sentence?