Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort a set in python? [duplicate]

Tags:

python

set

I am trying to sort a set in python, by adding elements to it. I tried using the sorted() method, but it is converting my set into a list. I need to sort my elements and print it as a set itself. How can I do that?

I tried using the sorted() method and obtained a list. Tried to put the list into a set. But, didn't receive the required result

a={}
a=set()
a.add(1)
a.add(-1)
a.add(0)
a.add(4)
a.add(-3)
print(a)

Expected result: {-3, -1, 0, 1, 4} Actual result: {0, 1, 4, -3, -1}

like image 669
Peyton Avatar asked Mar 28 '19 02:03

Peyton


People also ask

How do you sort duplicates in Python?

Method #1 : Using count() + set() + sorted() The sorted function can be used to sort the elements as desired, the frequency can be computed using the count function and removal of duplicates can be handled using the set function.

Does sorted () remove duplicates?

5. Using sort() We can use the sort() method to sort the set that we obtained in approach 2. This will also remove any duplicates, while preserving the order, but is slower than the dict.

Can you sort a set in Python?

There are various methods to sort values in Python. We can store a set or group of values using various data structures such as list, tuples, dictionaries which depends on the data we are storing. So, in this article, we will discuss some methods and criteria to sort the data in Python.

Do sets remove duplicates in Python?

Sets, like dictionaries, cannot contain duplicate values. If we convert a list to a set, all the duplicates are removed.


1 Answers

You can't

Just read python documentation:

Python also includes a data type for sets. A set is an unordered collection with no duplicate elements. Basic uses include membership testing and eliminating duplicate entries. Set objects also support mathematical operations like union, intersection, difference, and symmetric difference.

Emphasis mine.

That means you will never be able to sort items inside a set*.

At least, with "normal" sets, you can't. You should better look for third-party libraries (both, default dictionaries and collections.OrderedDict), because I don't think that python has inbuilt ordered sets, at least no in collections.

*Well, not never, I have understood that early versions of python hadn't ordered dictionaries, and now they have. But at least in the current version (python 3.7), there aren't ordered sets.

Anyway, dictionaries work in a similar manner to sets. In a dictionary, you can't have two times the same key, like in a set you can't have two times the same item. So if you work with the keys of a dictionary and just ignore the values of those keys, a collections.OrderedDict may work for you. Even base dictionaries (those you make with dict()) will work since Python 3.7.

>>> a = dict()
>>> a[1] = None
>>> a[-1] = None
>>> a[0] = None
>>> a[4] = None
>>> a[-3] = None
>>> print(a.keys())
dict_keys([1, -1, 0, 4, -3])
like image 88
Ender Look Avatar answered Sep 24 '22 08:09

Ender Look