python - When making a set out of sorted list, the output set is not sorted -




this question has answer here:

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.

https://pypi.python.org/pypi/ordered-set





wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -