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 ?
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.
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.
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.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With