Fill list using Counter with 0 values - python

Is possible to have a count of how many times a value appears in a given list, and have '0' if the item is not in the list?
I have to use zip but the first list have 5 items and the other one created using count, have only 3. That's why I need to fill the other two position with 0 values.

You can achieve your purpose with itertools zip_longest.
With zip_longest, you can zip two lists of different lengths, just that the missing corresponding values will be filled with 'None'. You may define a suitable fill values as i have done below.
from itertools import zip_longest
a = ['a','b','c','d','e']
b = [1,4,3]
final_lst = list(zip_longest(a,b, fillvalue=0))
final_dict = dict(list(zip_longest(a,b, fillvalue=0))) #you may convert answer to dictionary if you wish
ELSE
If what you are trying to do is count the number of times items in a reference list appear in another list(taking record also of reference items that don't appear in the other list), you may use dictionary comprehension:
ref_list = ['a','b','c','d','e']#reference list
other_list = ['a','b','b','d','a','d','a','a','a']
count_dict = {n:other_list.count(n) for n in ref_list}
print (count_dict)
Output
{'a': 5, 'b': 2, 'c': 0, 'd': 2, 'e': 0}

Use collections.Counter, and then call get with a default value of 0 to see how many times any given element appears:
>>> from collections import Counter
>>> counts = Counter([1, 2, 3, 1])
>>> counts.get(1, 0)
2
>>> counts.get(2, 0)
1
>>> counts.get(5, 0)
0

If you want to count how many times a value appears in a list, you could do this:
def count_in_list(list_,value):
count=0
for e in list_:
if e==value:
count+=1
return count
And use the code like this:
MyList=[1,3,1,1,1,1,1,2]
count_in_list(MyList,1)
Output:
6
This will work without any additional things such as imports.

Related

Removing Entries From Dictionaries

Say I have a dictionary with a lot of entries but I only need the first 5-10 entries to be printed, how would I go about doing this? I thought about using a for loop but I cannot find a way to make that work with dictionaries since as far as I am aware you cannot access dictionary values without knowing the key names. I also tried converting the dictionary into a list of tuples but this causes the order of the entries to be changed in an unwanted way. Any tips?
For dictionary d, to print the first n values:
print(list(d.values())[:n])
If the dictionary represent counts of words and you want the list of the top n words:
d = {'red': 4, 'blue': 2, 'yellow': 1, "green":5} # Example dictionary
sorted_d = sorted(d.items(), key = lambda kv: -kv[1]) # Sort descending
n = 2 # number of values wanted
print(sorted_d[:n]) # list of top n tuples
# Out: [('green', 5), ('red', 4)]
You can get words and counts as separate list
words, counts = zip(*sorted_d) # split into words and values
print(counts[:n]) # counts of top n words
# Out: (5, 4) # top n values
Another option is to convert the dictionary to a Counter
from collections import Counter
c = Counter(d)
print(c.most_common(n)) # Shows the n most common items in dictionary
# Out: {'green': 5, 'red': 4}
If using the counter, you could also use the counter to count the words as explained by Counting words with Python's Counter
If asd is your dictionary
asd = {'a':'1', 'b':'2', 'c':'3'}
for i, el in enumerate(asd.values()):
if i < 5:
print(el)
This will print the first 5 values, regardless of the name of the keys.

Check most common occurrence of a value of one list in a separate list

I have a list of numbers.
somelist = [5.000007,5.00099,5.0000075,5.0000075,5.0000075,5.0000099,5.00099,5.0000080,5.0000081,5.00099,5.0000080,5.0000096,5.0000087,5.008,5.00099,5.00000009]
I’m using the following to produce a unique list of the 3 lowest values:
def lowest_three(somelist):
lowest_unique = set(somelist)
return nsmallest(3, lowest_unique)
It produces the output:
[5.00000009, 5.000007, 5.0000075]
Now I want a separate function to tell me which of the three lowest values is the most commonly occuring in the original list.
So I want it to tell me that 5.0000075 is the most common number from the lowest_three list in the original list (somelist).
I’ve tried the following but it’s not working (it’s currently producing an output of 5.00099 which isn’t even in the lowest_three list).
def most_common_lowest(somelist):
for x in lowest_three(somelist):
return max(set(somelist), key=somelist.count)
How can achieve this?
Now I want a separate function to tell me which of the three lowest values is the most commonly occuring in the original list.
def most_common_lowest(somelist):
for x in lowest_three(somelist):
return max(set(somelist), key=somelist.count)
That code doesn't make sense. Should be:
def most_common_lowest(somelist):
return max(lowest_three(somelist), key=somelist.count)
You could possibly collect the counts with collections.Counter(), with only values from somelist that exist in top_three, then take the most_common of this:
from heapq import nsmallest
from collections import Counter
somelist = [5.000007,5.00099,5.0000075,5.0000075,5.0000075,5.0000099,5.00099,5.0000080,5.0000081,5.00099,5.0000080,5.0000096,5.0000087,5.008,5.00099,5.00000009]
def lowest_three(somelist):
lowest_unique = set(somelist)
return nsmallest(3, lowest_unique)
top_three = lowest_three(somelist)
# [5.00000009, 5.000007, 5.0000075]
freqs = Counter(x for x in somelist if x in top_three)
# Counter({5.0000075: 3, 5.000007: 1, 5.00000009: 1})
print(freqs.most_common(1)[0][0])
# 5.0000075
O you could group them in a collections.defaultdict, and take the max manually:
from collections import defaultdict
from operator import itemgetter
filtered_values = [x for x in somelist if x in top_three]
# [5.000007, 5.0000075, 5.0000075, 5.0000075, 5.00000009]
freqs = defaultdict(int)
for val in filtered_values:
freqs[val] += 1
# defaultdict(<class 'int'>, {5.000007: 1, 5.0000075: 3, 5.00000009: 1})
print(max(freqs.items(), key = itemgetter(1))[0]) # or key = lambda x: x[1]
# 5.0000075
Given the returned list from lowest_three, you can use list.count:
somelist = [5.000007,5.00099,5.0000075,5.0000075,5.0000075,5.0000099,5.00099,5.0000080,5.0000081,5.00099,5.0000080,5.0000096,5.0000087,5.008,5.00099,5.00000009]
new_list = lowest_three(somelist)
final_data = sorted(new_list, key=lambda x:somelist.count(x))[-1]
Output:
5.0000075
One option is to use collections.Counter.
from collections import Counter
counts = Counter(somelist)
lowest = lowest_three(somelist)
for num in lowest:
print counts[num]
// i think you better write an algorithm for this operation your self (for the practice)
a simple algorithm :
create a map contining only those 3 elements ,(witch you already found), as keys, and 0 as value.
run over the array and for each element in the array chack if the map contains him, if it does inc the value by 1 (map[key] = map[key]+1) .
iterate over your map and find the key with the highest value.
(it's like a counters array but with map data structure)
Use Counter from collections module and use sorted function, twice once for getting the 3 minimum elements and and second time for getting maximum occurring element
from collections import Counter
somelist = [5.000007,5.00099,5.0000075,5.0000075,5.0000075,5.0000099,5.00099,5.0000080,5.0000081,5.00099,5.0000080,5.0000096,5.0000087,5.008,5.00099,5.00000009]
lowest_three=sorted(Counter(somelist).items(), key=lambda i: i[0])[:3]
print(sorted(lowest_three,key=lambda i :-i[1])[0])
OUTPUT
(5.0000075, 3)
You can use the function min. It might solve your problem out.
#!/usr/bin/python
var list = [5.00000009, 5.000007, 5.0000075]
print "min value element : ", min(list)
https://www.tutorialspoint.com/python/list_min.htm
Everyone suggesting you collection module , You can do without collection and in few lines , Here you go:
somelist = [5.000007,5.00099,5.0000075,5.0000075,5.0000075,5.0000099,5.00099,5.0000080,5.0000081,5.00099,5.0000080,5.0000096,5.0000087,5.008,5.00099,5.00000009]
values=[5.00000009, 5.000007, 5.0000075]
track={}
for j,i in enumerate(somelist):
if i in values:
if i not in track:
track[i]=1
else:
track[i]+=1
print(max(list(map(lambda x:(track[x],x),track))))
output:
(3, 5.0000075)

Multi Dimensional List - Sum Integer Element X by Common String Element Y

I have a multi dimensional list:
multiDimList = [['a',1],['a',1],['a',1],['b',2],['c',3],['c',3]]
I'm trying to sum the instances of element [1] where element [0] is common.
To put it more clearly, my desired output is another multi dimensional list:
multiDimListSum = [['a',3],['b',2],['c',6]]
I see I can access, say the value '2' in multiDimList by
x = multiDimList [3][1]
so I can grab the individual elements, and could probably build some sort of function to do this job, but it'd would be disgusting.
Does anyone have a suggestion of how to do this pythonically?
Assuming your actual sequence has similar elements grouped together as in your example (all instances of 'a', 'b' etc. together), you can use itertools.groupby() and operator.itemgetter():
from itertools import groupby
from operator import itemgetter
[[k, sum(v[1] for v in g)] for k, g in groupby(multiDimList, itemgetter(0))]
# result: [['a', 3], ['b', 2], ['c', 6]]
Zero Piraeus's answer covers the case when field entries are grouped in order. If they're not, then the following is short and reasonably efficient.
from collections import Counter
reduce(lambda c,x: c.update({x[0]: x[1]}) or c, multiDimList, Counter())
This returns a collection, accessible by element name. If you prefer it as a list you can call the .items() method on it, but note that the order of the labels in the output may be different from the order in the input even in the cases where the input was consistently ordered.
You could use a dict to accumulate the total associated to each string
d = {}
multiDimList = [['a',1],['a',1],['a',1],['b',2],['c',3],['c',3]]
for string, value in multiDimList:
# Retrieves the current value in the dict if it exists or 0
current_value = d.get(string, 0)
d[string] += value
print d # {'a': 3, 'b': 2, 'c': 6}
You can then access the value for b by using d["b"].

how to step through a list and accumulate a integer value telling how many times an element based on it's position in list was seen

I'm trying to see how many times an element has been seen in a list
for instance:
list = [125,130,140,123,125,140,130,140]
I want to figure out perhaps how many times the element in position 0 (here, 125) was seen in the list, and accumulate the value with a counter. For the element in position 0, I would want to yield the int, 2.
Actually, a complex machinery is unnecessary:
>>> l = [1, 2, 3, 2]
>>> l.count(l[1])
2
You can use Counter:
from collections import Counter
my_list = [125,130,140,123,125,140,130,140]
Counter(my_list)
Output:
Counter({140: 3, 130: 2, 125: 2, 123: 1})
Using list comprehension and len:
count = len([l for l in list if l == list[n]])
where n is the index of the item you are counting.
def foo(pos, values):
counter = 0
element_to_find = values[pos]
for element in values:
if element == element_to_find:
counter += 1
return counter
You could store them in a dictionary with a dict comprehension (which works exactly like list comprehensions):
l = [125,130,140,123,125,140,130,140]
counts = {x : l.count(x) for x in l}
Also, it is bad practice to name your list "list". This will conflict with the built-in list function in Python.

Search through list with duplicates

I have a list that looks like this:
list1 = [1,2,4,6,8,9,2]
If I were to say
if 2 in list1:
print True
It prints True once. Is there a way to determine if 2 or any variable x is in the list multiple times and if so how many without iterating through the entire list like this?
for item in list1:
if item = 2:
duplicates +=1
I think you're looking for list.count:
if list1.count(2) > 1:
print True
In Sequence Types:
s.count(i) total number of occurrences of i in s
Of course under the covers, the count method will iterate through the entire list (although it will do so a lot faster than a for loop). If you're trying to avoid that for performance reasons, or so you can use a lazy iterator instead of a list, you may want to consider other options. For example, sort the list and use itertools.groupby, or feed it into a collections.Counter, etc.
from collections import Counter
y = Counter(list1)
print y[2]
print y[5] # and so on
list1 = [1,2,4,6,8,9,2]
print list1.count(2)
I would use a collections.Counter object for this:
from collections import Counter
myCounter = Counter(list1)
print myCounter[2] > 1 #prints 'True'
If you only plan on doing this with one or a few elements of the list, I would go with abarnert's answer, however.
list1 = [1,2,4,6,8,9,2]
dict1 = {}
for ele in list1:
# you iterate through the list once
if ele in dict1:
# if a key is already in the dictionary
# you increase the corresponding value by one
dict1[ele] += 1
else:
# if a key is not yet in the dictionary
# you set its corresponding value to one
dict1[ele] = 1
Result:
>>> dict1
{1: 1, 2: 2, 4: 1, 6: 1, 8: 1, 9: 1}
Collections.counter (as others have pointed out) is how I would do this. However, if you really want to get your hands dirty:
def count(L):
answer = {}
for elem in L:
if elem not in answer:
answer[elem] = 0
answer[elem] += 1
return answer
>>> counts = count(myList)
>>> duplicates = [k for k,v in counts.iteritems() if v>1]

Categories

Resources