I have a large list, which I separated into small sized lists which have elements of occurrences of 1s and 0s, randomly.
Also, the first two lists are made with different parameters from the last two.
Example:
list_of_lists[0] =[1,0,1,1,1,0,1,1,1,0]
list_of_lists[1] =[0,0,0,0,0,0,0,0,0,0]
list_of_lists[2] =[1,1,1,1,1,1,1,1,1,1]
list_of_lists[3] =[0,0,1,1,1,1,1,1,1,0]
I would like to count the occurrences of 1s and 0s in each list, and append them into a dictionary to plot the occurrences.
My trial is as follows:
counts_each = dict()
for i in range(4): #all 4 lists
for k in list_of_lists[i]: #elements of the lists
counts_each[k] = counts_each.get(k, 0) + 1
print(counts_each)
which calculates the general occurrences of the 1s and 0s for the al lists:
{0: 16, 1: 24}
If I do:
list_counts = []
for i in range(4):
counts_each = dict()
for k in list_of_lists[i]:
counts_each[k] = counts_each.get(k, 0) + 1
list_counts.append(counts_each)
print(list_counts)
It does not accumulate all of the counts:
[{0: 3, 1: 7},
{0: 3, 1: 7},
{0: 3, 1: 7},
{0: 3, 1: 7},
{0: 3, 1: 7},
{0: 3, 1: 7},
{0: 3, 1: 7},
{0: 3, 1: 7},
{0: 3, 1: 7},
{0: 3, 1: 7},
{0: 10},
{0: 10},
{0: 10},
{0: 10},
{0: 10},
{0: 10},
{0: 10},
{0: 10},
{0: 10},
{0: 10},
{1: 10},
{1: 10},
{1: 10},
{1: 10},
{1: 10},
{1: 10},
{1: 10},
{1: 10},
{1: 10},
{1: 10},
{0: 3, 1: 7},
{0: 3, 1: 7},
{0: 3, 1: 7},
{0: 3, 1: 7},
{0: 3, 1: 7},
{0: 3, 1: 7},
{0: 3, 1: 7},
{0: 3, 1: 7},
{0: 3, 1: 7},
{0: 3, 1: 7}]
I would be glad to have some insights of what I am doing wrong.
Thank you.
You can let the collections module do all the counting work for you.
from collections import Counter
list_of_lists = [[] for _ in range(4)]
list_of_lists[0] =[1,0,1,1,1,0,1,1,1,0]
list_of_lists[1] =[0,0,0,0,0,0,0,0,0,0]
list_of_lists[2] =[1,1,1,1,1,1,1,1,1,1]
list_of_lists[3] =[0,0,1,1,1,1,1,1,1,0]
counters = [Counter(l) for l in list_of_lists]
print(*counters, sep="\n")
OUTPUT
Counter({1: 7, 0: 3})
Counter({0: 10})
Counter({1: 10})
Counter({1: 7, 0: 3})
You could use a Dict Comprehension, given your nested list:
list_of_lists = [[1,0,1,1,1,0,1,1,1,0], [0,0,0,0,0,0,0,0,0,0], [1,1,1,1,1,1,1,1,1,1], [0,0,1,1,1,1,1,1,1,0]]
use it in this way:
{ idx: {0: lst.count(0), 1: lst.count(1)} for idx, lst in enumerate(list_of_lists) }
#=> {0: {0: 3, 1: 7}, 1: {0: 10, 1: 0}, 2: {0: 0, 1: 10}, 3: {0: 3, 1: 7}}
In the above case I used the index as a key, but you could just use a list comprehension to get a list of dictionaries:
[ {0: lst.count(0), 1: lst.count(1)} for lst in list_of_lists ]
#=> [{0: 3, 1: 7}, {0: 10, 1: 0}, {0: 0, 1: 10}, {0: 3, 1: 7}]
Chris Doyle's answer is excellent, but perhaps your goal is to understand the problem with your solution, specifically.
You have not included your expected output. If I am correct that your issue with your current solution is the repetition of the counts, and you want an output like this:
[{1: 7, 0: 3}, {0: 10}, {1: 10}, {0: 3, 1: 7}]
Then the issue appears to be with the indenting of the line list_counts.append(counts_each). You are doing this each time through the k loop (looping through the items in the list) when I think you want to do it only after finishing the count for a given list:
list_counts = []
for i in range(4):
counts_each = dict()
for k in list_of_lists[i]:
counts_each[k] = counts_each.get(k, 0) + 1
list_counts.append(counts_each)
print(list_counts)
I need to assign and retrieve slices of data using two keys, and I do not a priori know the values for one of the keys.
Specifically, I'm downloading and processing text data files that list float values by year and duration (e.g., 1 hour). The duration keys are predetermined, but the years are not. The data are provided sequentially, one line at a time (not tabular, in other words).
Because I don't know all the years in a given file, so far I've tried using defaultdict(dict). Here's my sample code.
from collections import defaultdict
a = defaultdict(dict)
a[2006][2]=0.024
a[2004][2]=0.157
a[2000][1]=0.64
a[2005][2]=0.346
a[2003][2]=0.165
a[2003][6]=0.8
a[2007][12]=0.642
a[2003][1]=0.664
a[2002][6]=0.579
a[2004][1]=0.829
a[2001][6]=0.344
a[2003][3]=0.508
a[2003][12]=0.66
a[2002][1]=0.923
:a
defaultdict(dict,
{2006: {2: 0.024},
2004: {2: 0.157, 1: 0.829},
2000: {1: 0.64},
2005: {2: 0.346},
2003: {2: 0.165, 6: 0.8, 1: 0.664, 3: 0.508, 12: 0.66},
2007: {12: 0.642},
2002: {6: 0.579, 1: 0.923},
2001: {6: 0.344}})
I need to do three things.
Retrieve all the year keys. Remember I don't know them ahead of time.
For each year, retrieve the duration key:value pairs. I figured that one out.
: a[2002]
{6: 0.579, 1: 0.923}
For each duration, retrieve the year key: value pairs. I'm stuck on this one.
I appreciate any help you can offer. If I should be doing this in numpy, pandas, or something else, feel free to redirect me. Keep in mind I don't know the year range ahead of time, and even if I did there are random gap years with no data.
Not sure what you're looking for, but for getting all the year keys and their values you can iterate over that dictionary, by doing like this:
for i in a:
print(i, a[i])
Output:
2006 {2: 0.024}
2004 {2: 0.157, 1: 0.829}
2000 {1: 0.64}
2005 {2: 0.346}
2003 {2: 0.165, 6: 0.8, 1: 0.664, 3: 0.508, 12: 0.66}
2007 {12: 0.642}
2002 {6: 0.579, 1: 0.923}
2001 {6: 0.344}
from collections import defaultdict
a = defaultdict(dict)
a[2006][2]=0.024
a[2004][2]=0.157
a[2000][1]=0.64
a[2005][2]=0.346
a[2003][2]=0.165
a[2003][6]=0.8
a[2007][12]=0.642
a[2003][1]=0.664
a[2002][6]=0.579
a[2004][1]=0.829
a[2001][6]=0.344
a[2003][3]=0.508
a[2003][12]=0.66
a[2002][1]=0.923
print(a)
"""
defaultdict(<class 'dict'>, {2006: {2: 0.024}, 2004: {2: 0.157, 1: 0.829}, 2000: {1: 0.64}, 2005: {2: 0.346}, 2003: {2: 0.165, 6:0.8, 1: 0.664, 3: 0.508, 12: 0.66}, 2007: {12: 0.642}, 2002: {6:0.579, 1: 0.923}, 2001: {6: 0.344}})
"""
# Retrieve all the year keys. Remember I don't know them ahead of time.
for item in a:
print(item)
"""
2006
2004
2000
2005
2003
2007
2002
2001
"""
# For each year, retrieve the duration key:value pairs. I figured that one out.
for year in a:
dur_key_val = a[year]
print(year,'=>',dur_key_val)
# For each duration, retrieve the year key: value pairs. I'm stuck on this one.
durationDict = {}
for year in a:
dur_key_val = a[year]
for inner_key in dur_key_val:
duration = dur_key_val[inner_key]
durationDict[duration] = { year:inner_key }
print(durationDict)
"""
{0.024: {2006: 2}, 0.157: {2004: 2}, 0.829: {2004: 1}, 0.64: {2000: 1}, 0.346: {2005: 2}, 0.165: {2003: 2}, 0.8: {2003: 6}, 0.664: {2003: 1}, 0.508: {2003: 3}, 0.66: {2003: 12}, 0.642: {2007: 12}, 0.579: {2002: 6}, 0.923: {2002: 1}, 0.344: {2001: 6}}
"""
I want to create a dictionary from a given list, nesting elements as shown below. For instance, given:
lst = range(1, 11)
how do I create a function to create a nested dictionary from this list:
dic = {1: {2: {3: {4: {5: {6: {7: {8: {9: 10}}}}}}}}}
Reverse your list (or better range object). Take the last (now first) element as start value and create a new dict in each iteration through the rest of the reversed list:
>>> r = reversed(range(1, 11))
... d = next(r)
... for x in r:
... d = {x: d}
... d
...
{1: {2: {3: {4: {5: {6: {7: {8: {9: 10}}}}}}}}}
You could use functools.reduce.
import functools
lst = range(1, 11)
functools.reduce(lambda x, y: {y: x}, reversed(lst))
# {1: {2: {3: {4: {5: {6: {7: {8: {9: 10}}}}}}}}}
You can build it from inside out:
result = {9: 10}
for i in range(8, 0, -1):
temp = {i: result}
result = temp
print(result)
# outputs {1: {2: {3: {4: {5: {6: {7: {8: {9: 10}}}}}}}}}
Start from the innermost value, working outward.
At each step, use the previous step's dict as the new val.
def nest_dict(lst):
my_dict = lst[-1]
for val in lst[-2::-1]:
my_dict = {val: my_dict}
return my_dict
print nest_dict(range(1, 11))
Output:
{1: {2: {3: {4: {5: {6: {7: {8: {9: 10}}}}}}}}}
my_list = [{0: 0}, {1: 4.2}, {2: 3.7}, {3: 5.0}, {4: 4.0}, {5: 3.3}, {6: 4.3}, {7: 4.0}, {8: 3.9}, 0, {10: 4.0}]
What I want my program to do is go through the list, record the highest value (as in the value from a key-value pair) once it's scanned through the entire thing, append that key-pair value to a new list, remove that key-pair value from the original list [my_list], and repeat the process twice more. So the desired output would look like this:
desired output: [{3: 5.0},{6: 4.3},{1: 4.2}]
I'm not sure how to achieve the desired output.
I'm assuming that the single integer in your my_list is a typo.
Use the heapq module to get the three largest items. This has slightly better complexity and memory efficiency than sorting the whole list and then extracting the last three elements.
>>> from heapq import nlargest
>>> my_list = [{0: 0}, {1: 4.2}, {2: 3.7}, {3: 5.0}, {4: 4.0}, {5: 3.3}, {6: 4.3}, {7: 4.0}, {8: 3.9}, {10: 4.0}]
>>> nlargest(3, my_list, key=lambda d: d.values()[0])
[{3: 5.0}, {6: 4.3}, {1: 4.2}]
The key function specifies the criterion by which the items from your list are to be ordered, it simply fetches the only value any individual dictionary has.
Suppose a list composed by several dict in python:
a = [{1: u'100'}, {2: u'200'}, {3: u'300'}]
I'd like to change the datatype of items of the dict from unicode to float, i.e.,
a = [{1: 100.0}, {2: 200.0}, {3: 300.0}]
The following are my current codes:
for i in a:
for j in i.keys():
if type(i[j]) == unicode:
i[j] = float(i[j])
It works but I hate this stupid expression.
There must be some much more elegant expression.
Please help.
>>> a = [{1: u'100'}, {2: u'200'}, {3: u'300'}]
>>> [{k:float(v) for k,v in d.iteritems()} for d in a]
[{1: 100.0}, {2: 200.0}, {3: 300.0}]
If you need to add a Unicode type check, you can, but then arguably a nested list/dict comprehension isn't all that readable any more:
>>> a = [{1: u'100'}, {2: u'200'}, {3: u'300', 4: "not unicode"}]
>>> [{k:float(v) if isinstance(v, unicode) else v for k,v in d.iteritems()} for d in a]
[{1: 100.0}, {2: 200.0}, {3: 300.0, 4: 'not unicode'}]