Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a list under a dictionary?

Tags:

python

I have as input a csv file of the following format:

#date,time,process(id),thread(id),cpuusage
201412120327,03:27,process1(10),thread1(12),10
201412120327,03:27,process2(11),thread1(13),10
201412120328,03:28,process1(10),thread2(12),10
201412120328,03:28,process2(10),thread2(13),10

I'm trying to create a data structure where I can use the process id as an has key for all the entries of csv that match it. See the code below.

# open the file
f = open (cvs_file)
csv_f = csv.reader(f)

# List of processes, with all the repetitions
processes = []
# Dictionary for the threads
threads = {}
for row in csv_f :
    # Populate already the list of processes
    processes.append(row[2])
    threads[row[2]] = row

My problem is that using this I don't get a list of rows under the key, but only the last value I put there. Which is sort of logical, if I thik about it. How can I add a list of (lists) rows, which is what I intended ?

like image 631
Rui Avatar asked Dec 15 '14 10:12

Rui


People also ask

How do I add a list to a dictionary?

To convert a list to a dictionary using the same values, you can use the dict. fromkeys() method. To convert two lists into one dictionary, you can use the Python zip() function. The dictionary comprehension lets you create a new dictionary based on the values of a list.

Can you have a list inside a dictionary Python?

In Python, the dictionary of lists means we have to assign values as a list. In Python dictionary, it is an unordered and immutable data type and can be used as a keys element.

How do you create a nested list in a dictionary?

To create a nested dictionary, simply pass dictionary key:value pair as keyword arguments to dict() Constructor. You can use dict() function along with the zip() function, to combine separate lists of keys and values obtained dynamically at runtime.

How do you store a list in the dictionary?

Since python dictionary is unordered, the output can be in any order. To convert a list to dictionary, we can use list comprehension and make a key:value pair of consecutive elements. Finally, typecase the list to dict type.


1 Answers

You can use dict.setdefault() to create an empty list if the key isn't there yet, and append your rows to the list (freshly created or not):

threads = {}
for row in csv_f:
    # Populate already the list of processes
    processes.append(row[2])
    threads.setdefault(row[2], []).append(row)
like image 101
Martijn Pieters Avatar answered Oct 08 '22 19:10

Martijn Pieters