1

I C++11 I can do following:

multiset<int> ms {1,2,4,6,3,2,2,1}; // set will sort these elements automatically

std::pair<multiset<int>::iterator, multiset<int>::iterator> bounds;

// look for number 2 in the multiset
bounds = ms.equal_range(2);

for (auto v: ms)
{
    cout << v << " ";
}

It gives 2 2 2 as there are three 2's in the multiset. How to do same in python?

I can do set only like this: ms = {1,2,4,6,3,2,2,1}. This set will sort the elements as well, but also removes duplicates. If I cant use set, is there other data structure for that?

Also what is equivalent of this ms.equal_range(2); to search for multiple elements in python, like in c++.

MarioBross
  • 97
  • 1
  • 5
  • possible duplicate of [Is there a Python equivalent for C++ "multiset"?](http://stackoverflow.com/questions/17346905/is-there-a-python-equivalent-for-c-multisetint) – Pradhan Feb 27 '15 at 06:54
  • I saw this anwser, but it does not explain how to simulate equal_range in python. – MarioBross Feb 27 '15 at 06:58
  • Please reword your question then. Also, refrain from asking multiple questions in one. – Pradhan Feb 27 '15 at 06:59
  • I changed the tittle. thx. – MarioBross Feb 27 '15 at 07:00
  • [This](http://stackoverflow.com/questions/9343739/maplower-bound-equivalent-for-pythons-dict-class) question asks about `std::lower_bound`. Since `std::equal_range` is `[std::lower_bound, std::upper_bound)`, the answer should be relevant. – Pradhan Feb 27 '15 at 07:10

1 Answers1

-1

This is for lists not sets, however the .count method might be what you're looking for.

Quoting the documentation: list.count(x) Return the number of times x appears in the list. end quote.

So for example.

two_three = [2, 4, 5, 2, 2]
print two_three.count(2)
=> 3
GallegoDor
  • 39
  • 2