Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data structure to represent multiple equivalent keys in set in Python?

Currently, I want to find the correct data structure to meet the following requirement.

There are multiple arrays with disordered element, for example,

[1, 2], [2, 1], [3, 2, 2], [2], [2, 1, 3], [2, 2, 3]

After processing those data, the result is,

[1, 2], [2, 2, 3], [2], [1, 2, 3]

With sorted element in each array and filter the duplicate arrays.

Here are my thoughts:

  • Data structure Set(Arrays)? - Failed. It seems there is only one array in the build-in set

    set([])

  • Data structure Array(Sets)? - Failed. However, there is no duplicate element in the build-in set. I want to know whether there is one data structure like multiset in C++ within Python?

like image 613
zangw Avatar asked Oct 13 '15 11:10

zangw


People also ask

Is there a set data structure in Python?

Set is a Data Structure in Python with an unordered and unindexed collection of elements. Every element in a Set is always unique. The Set Data Structure does not allow any duplication of elements.

What is data structure which data structure used by Python?

The basic Python data structures in Python include list, set, tuples, and dictionary. Each of the data structures is unique in its own way. Data structures are “containers” that organize and group data according to type. The data structures differ based on mutability and order.

What are key structures in Python?

These structures are called List, Dictionary, Tuple and Set. Python allows its users to create their own Data Structures enabling them to have full control over their functionality.

What data structure does Python use in place of arrays?

A Python array is a little bit different to arrays in other programming languages in that it uses something called 'list' instead of array.


1 Answers

Transform your list to tuple(thus can be a item of set), then back to list.

>>> [list(i) for i in set([tuple(sorted(i)) for i in a])]
[[1, 2], [2], [2, 2, 3], [1, 2, 3]]
like image 148
luoluo Avatar answered Oct 29 '22 15:10

luoluo