python - When making a set out of sorted list, the output set is not sorted -
this question has answer here:
- understanding set() function 5 answers
could please explain me why making set out of sorted list results in set not sorted?
list.sort() should sort list in place, does. set(list) should return unique values of list, does.
but why resulting set not sorted? need make set, turn list2 , sort list2?
(using python 2.7.6)
code:
def longest(s1, s2): list = [] s = s1 + s2 letter in s: list.append(letter) print list list.sort() print list unique = set(list) print unique s4 = "" item in unique: s4 = s4 + item print s4 return s4
console output:
['a', 'r', 'e', 't', 'h', 'e', 'y', 'h', 'e', 'r', 'e', 'y', 'e', 's', 't', 'h', 'e', 'y', 'a', 'r', 'e', 'h', 'e', 'r', 'e'] ['a', 'a', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'h', 'h', 'h', 'h', 'r', 'r', 'r', 'r', 's', 't', 't', 'y', 'y', 'y'] set(['a', 'e', 'h', 's', 'r', 't', 'y']) aehsrty
a python set nature unordered. may use ordered-set
package if order matters.
wiki
Comments
Post a Comment