Questions tagged [python-collections]

A Python module that implements specialized container datatypes providing alternatives to Python’s general purpose built-in containers, dict, list, set, and tuple.

129 questions
0
votes
2 answers

Why is the time taken by collections.Counter to construct a frequency map less than a simple dict creation by a for loop?

From what I understand, since count() iterates over the list again for each element it is slower. Since, the Counter and dict construction both iterate over the list once shouldn't the dict construction and counter time results be similar? I used…
sparkstar
  • 593
  • 1
  • 7
  • 14
0
votes
3 answers

How to fill a tuple with a for loop

I am trying to fill a tuple with named tuples using a for loop. The example code below works: import collections Experiment = collections.namedtuple('Experiment', ['parameter', ]) nsize = 3 parameters = {} for n in range(0, nsize): …
coccolith
  • 47
  • 8
0
votes
2 answers

Last added element value reflected in whole list in python - How to solve this problem?

The following is my data: "Jay": 1, "Raj": 1,"Jay": 3,"Raj": 3,"Jay": 10,"Raj": 2,"Jay": 2 I would like to put it into a list to make it look like this: ["Jay": 1,"Raj": 1,"Jay": 3,"Raj": 3,"Jay": 10,"Raj": 2,"Jay": 2] My issue is that only the…
Arpit Shah
  • 45
  • 1
  • 6
0
votes
1 answer

Iterating over a Pandas Dataframe column and adding its elements to a Python Collections Counter object

I have a pandas dataframe of N columns of integer object values. The values in the columns are associated with outcome of a particular random experiment. For example, if I were to call df.head(): 0 1 2 3 0 13 4 0 5 1 8 2 16 6 2…
0
votes
0 answers

Given a set, perform a pairwise operation on every element of the set?

Assume I have a set: s = {1,2,3,4,5} And now I want to perform an operation of every possible unordered pair of that set, say addition. For a list I would do: for i in range(len(s)): for j in range(i+1,len(s)): print(s[i]+s[j] But for…
0
votes
3 answers

Most pythonic way to check list boils down to one value

I want a function check to check that a given list reduces ("boils down") to exactly one value under a given function reduce_function. (A common example could be to check that a list of lists contains sublists of equal length only.) I see at least…
Jonathan Scholbach
  • 4,925
  • 3
  • 23
  • 44
0
votes
1 answer

Python - count word frequency of string from list, number of words from list varies

I am trying to create a program that runs though a list of mental health terms, looks in a research abstract, and counts the number of times the word or phrase appears. I can get this to work with single words, but I'm struggling to do this with…
0
votes
1 answer

Build a string with a deque of integer displayed as hexadecimal

I have a deque full of integer and I would like to build a string (or just print it) in hexadecimal format. I tried from collections import deque a = deque([10,11]) my_string = hex(a) It says that deque can't be interpreted as an integer. I would…
Welgriv
  • 714
  • 9
  • 24
0
votes
0 answers

collections.Counter().values() view appears to be sorted by key in certain conditions. Why and what are they?

Counter() from Python's collections module is an unordered container, yet when built from same-size integers, the values() view appears as if the Counter was sorted by key first. This happens consistently both on my computer (3.4.3rc1), as well as…
0
votes
0 answers

Python collections.Mapping: Uses of dict_keys that __iter__ and __contains__ don't already serve

In python 3, a dict (or anything implementing the collections.Mapping interface) has a keys() method that returns a dict_keys, which is iterable (yielding keys, as __iter__() does) but can also respond to key in dict_keys in O(1) time (as…
thorwhalen
  • 1,920
  • 14
  • 26
0
votes
1 answer

Why does adding two Counters with timedelta values raise a TypeError?

I am trying to add two Counters, which contain timedeltas. Adding the Counters raises the following: Traceback (most recent call last): File "", line 1, in File…
0
votes
1 answer

I get this error TypeError: filter must be an instance of dict, bson.son.SON, or other type that inherits from collections.Mapping

I am retrieving last saved data to a text field so the user can continue what they were working on earlier, this command works fine in mongo console, in PyCharm I can't figure out the right way to write the find() query I tried storing filter…
0
votes
2 answers

How does the python del function work without calling __getitem__

In python, an object is subscriptable when it's class defines a __getitem__(self, k) method, allowing us to "get items" through the bracket syntax: obj[k] The syntax for the builtin del function is: del(obj[k]) This deletes item k from…
thorwhalen
  • 1,920
  • 14
  • 26
0
votes
1 answer

Add parameters to a short hand for loop in Python

I am very sorry that I was not able to give a better Headline for my problem. I am very new to Python programming and I have to make a small change to the existing code in an application. The current python code reads each and every row and each…
0
votes
2 answers

Subclassing a file object to "fake" it as an iterable - Python

My thought was to get rid of how users are constantly using seek(0) to reset the text file reading. So instead I've tried to create a MyReader that's an collections.Iterator and then using .reset() to replace seek(0) and then it continues from…
alvas
  • 115,346
  • 109
  • 446
  • 738
1 2 3
8 9