A Python module that implements specialized container datatypes providing alternatives to Python’s general purpose built-in containers, dict, list, set, and tuple.
Questions tagged [python-collections]
129 questions
-1
votes
1 answer
Why does the set function in Python produce different outputs for the same inputs?
This is my code
string='AABCAAADA'
k=3
for i in range(0,len(string),k):
print(set(string[i:i+k]))
each time I run it produces a different output. how can i fix this?
outputs:
{'B', 'A'}
{'A', 'C'}
{'D', 'A'}
{'B', 'A'}
{'C', 'A'}
{'D',…

Morteza Saki
- 7
- 1
- 4
-1
votes
3 answers
Does OrderedDict.items() also keeps the order preserved?
Assuming that I'm using the following OrderedDict:
order_dict = OrderedDict([("a",1), ("b",2), ("c",3)])
At some point, I would like to get the (key,value) items and define an iterator, and start moving it once desired:
ordered_dict_items_iter =…

user3019483
- 67
- 6
-1
votes
1 answer
Pandas itertuples are not named tuples as expected?
Using this page from the Pandas documentation, I wanted to read a CSV into a dataframe, and then turn that dataframe into a list of named…

max
- 4,141
- 5
- 26
- 55
-1
votes
1 answer
doubts of Python's collection.defaultdict usage
I understand collections.defaultdict is assigning default value to a dict like this:
dict = collections.defaultdict(int) # default value of dict is 0
or
dict = collections.defaultdict(lambda: 5) # default value of dict is 5
Then I see a usage of…

derek
- 9,358
- 11
- 53
- 94
-1
votes
1 answer
Python: Creating a dictionary with key as a set and value as its count
I an implementing a Data Mining algo. I which my smallest object is a set. A set may contain a single item or multiple items (Itemset). I need to count the occurrences of such sets in a dictionary as :
Dict={set([] : count)}
I need such…

Anirban
- 550
- 3
- 19
-2
votes
1 answer
How to apply a function to all elements in a collections namedtuple
I need to apply a function for each of the elements of a collections namedtuple.
Product = collections.namedtuple('Product', [
'ticker_symbol', 'entity', 'unique_id', 'as_of_date', 'company_name',
'followers', 'items', 'linkedin',…

gogasca
- 9,283
- 6
- 80
- 125
-2
votes
2 answers
Printing counts of elements in a Tuple based on condition
I want to print the count of items based on the IF statement below. What I have below is printing the entire list, 7 times, and a count for each item (1). That is not what I want. Ideally it would return:
5
1
1
Any ideas?
from collections…

JD2775
- 3,658
- 7
- 30
- 52
-3
votes
2 answers
Python's `collections.Counter` with non-integer values
collections.Counter is extremely useful to gather counts and operate on counts as objects. You can do things like:
>>> Counter({'a': 2, 'b': 5}) + Counter({'a': 3, 'c': 7})
Counter({'c': 7, 'a': 5, 'b': 5})
This essentially groups items by their…

thorwhalen
- 1,920
- 14
- 26
-5
votes
1 answer
Why iterating on a collection does not require index?
I'm trying to sort my confusions learning python.
>>> cities = ['London', "Toronto", 'Paris', 'Oslo']
>>> cities
['London', 'Toronto', 'Paris', 'Oslo']
>>> for i in cities:
... print(i)
...
London
Toronto
Paris
Oslo
>>> for i in cities:
... …

Master_Roshy
- 1,245
- 3
- 12
- 19