Convert array to dictionary (counter) - python

Suppose I have an array as follows
arr = [1 , 2, 3, 4, 5]
I would like to convert it to a dictionary like
{
1: 1,
2: 1,
3: 1,
4: 1,
5: 1
}
My motivation behind this is so I can quickly increment the count of any of the keys in O(1) time.
Help will be much appreciated. Thanks

from collections import Counter
answer = Counter(arr)

You can use the fromkeys method:
>>> arr = [1 , 2, 3, 4, 5]
>>> dict.fromkeys(arr,1)
{1: 1, 2: 1, 3: 1, 4: 1, 5: 1}
>>>

You can use a dictionary comprehension:
{k: 1 for k in arr}

from collections import Counter
arr = [1, 2, 3, 4, 5]
c = Counter(arr)

Try collections.Counter:
>>> import collections
>>> arr = [1, 2, 3, 4, 5]
>>> collections.Counter(arr)
Counter({1: 1, 2: 1, 3: 1, 4: 1, 5: 1})

arr = [1 , 2, 3, 4, 5]
print({p: arr.count(p) for p in arr})
It's more precise I think and it still works when an element in the array repeats.

Related

Python find frequency of numbers in list of lists

I have a list of lists of int as shown below
[[1, 2, 3],
[1, 5],
[4, 2, 6]]
I want to generate the frequency of the numbers in the lists as a dict, for example 1 occurs in 2 of the lists, and so on, expected output is
{1:2,
2:2,
3:1,
4:1,
5:1,
6:1}
How can this be generated?
You could try this:
L is your list of list.
expected = {1:2,
2:2,
3:1,
4:1,
5:1,
6:1}
>>> from itertools import chain
>>> from collections import Counter
>>> flattened = list(chain.from_iterable(L))
>>> flattened
[1, 2, 3, 1, 5, 4, 2, 6]
>>> counts = Counter(flattened)
>>> counts
Counter({1: 2, 2: 2, 3: 1, 5: 1, 4: 1, 6: 1})
# It's easy to make it to a function or one-liner too.
>>> counts = Counter(chain.from_iterable(L))
>>> assert counts == expected # your expected result shown above
# silence means matching.
you can use Counter for this
>>> from collections import Counter as c
>>> array = [[1, 2, 3],[1,5],[4,2,6]]
>>> result = c()
>>> for sublist in array:
... result += c(sublist)
...
>>> result
Counter({1: 2, 2: 2, 3: 1, 5: 1, 4: 1, 6: 1})

How to create a list that represent the number of times a given item was shown?

This problem seems really stupid bu I can't get my head around it.
I have the following list:
a = [2, 1, 3, 1, 1, 2, 3, 2, 3]
I have to produce a second list which have the same size as the previous one but the values that appear should be the amount of times that a value showed up in the array until that point. For example:
b = [1, 1, 1, 2, 3, 2, 2, 3, 3]
So b[0] = 1 because it's the first time the item '2' appear on the 'a' list. b[5] = 2 and b[7] = 3 because it's the second and third time that the item '2' appear on the list 'a'.
Here a solution:
from collections import defaultdict
a = [2, 1, 3, 1, 1, 2, 3, 2, 3]
b = []
d = defaultdict(int)
for x in a:
d[x] +=1
b.append(d[x])
print(b)
Output:
[1, 1, 1, 2, 3, 2, 2, 3, 3]
I think using dictionary might help you, basically I am iterating the list and storing the current frequency of the number.
a = [2, 1, 3, 1, 1, 2, 3, 2, 3]
d = {}
z = []
for i in a:
if i not in d:
d[i] = 1
z.append(1)
else:
d[i]+=1
z.append(d[i])
print(z)
output = [1, 1, 1, 2, 3, 2, 2, 3, 3]

Counting identical list-elements in a row in Python

I am using Python3. I have a list a of only integers. Now, I want to save the element and the number it repeats itself in a row in another list.
Example:
a = [6, 0, 0, 2, 2, 2, 2, 1, 89, 89]
Output:
result = ["6,1", "0, 2", "2, 4", "1, 1", "89, 2"]
# the number before the "," represents the element, the number after the "," represents how many times it repeats itself.
How to efficiently achieve my goal ?
I believe all the solutions given are counting the total occurrences of a number in the list rather than counting the repeating runs of a number.
Here is a solution using groupby from itertools. It gathers the runs and appends them to a dictionary keyed by the number.
from itertools import groupby
a = [6, 0, 0, 2, 2, 2, 2, 1, 89, 89]
d = dict()
for k, v in groupby(a):
d.setdefault(k, []).append(len(list(v)))
Dictionary created:
>>> d
{6: [1], 0: [2], 2: [4], 1: [1], 89: [2]}
Note that all runs only had 1 count in their list. If there where other occurrences of a number already seen, there would be multiple counts in the lists (that are the values for dictionary).
for counting an individual element,
us list.count,
i.e, here, for, say 2, we user
a.count(2),
which outputs 4,
also,
set(a) gives the unique elements in a
overall answer,
a = [6, 0, 0, 2, 2, 2, 2, 1, 89, 89]
nums = set(a)
result = [f"{val}, {a.count(val)}" for val in set(a)]
print(result)
which gives
['0, 2', '1, 1', '2, 4', '6, 1', '89, 2']
Method 1: using for loop
a = [6, 0, 0, 2, 2, 2, 2, 1, 89, 89]
result = []
a_set = set(a) # transform the list into a set to have unique integer
for nbr in a_set:
nbr_count = a.count(nbr)
result.append("{},{}".format(nbr, nbr_count))
print(result) # ['0,2', '1,1', '2,4', '6,1', '89,2']
Method 2: using list-comprehensions
result = ["{},{}".format(item, a.count(item)) for item in set(a)]
print(result) # ['0,2', '1,1', '2,4', '6,1', '89,2']
you can use Python List count() Method, method returns the number of elements with the specified value.
a = [6, 0, 0, 2, 2, 2, 2, 1, 89, 89]
print ({x:a.count(x) for x in a})
output:
{6: 1, 0: 2, 2: 4, 1: 1, 89: 2}
a = [6, 0, 0, 2, 2, 2, 2, 1, 89, 89]
dic = dict()
for i in a:
if(i in dic):
dic[i] = dic[i] + 1
else:
dic[i] = 1
result = []
for i in dic:
result.append(str(i) +"," + str(dic[i]))
Or:
from collections import Counter
a = [6, 0, 0, 2, 2, 2, 2, 1, 89, 89]
mylist = [Counter(a)]
print(mylist)
You can use Counter from collections:
from collections import Counter
a = [6, 0, 0, 2, 2, 2, 2, 1, 89, 89]
counter = Counter(a)
result = ['{},{}'.format(k, v) for k,v in counter.items()]

Initializing a dictionary with zeroes [duplicate]

This question already has answers here:
How do I count the occurrences of a list item?
(30 answers)
Closed 3 years ago.
I am trying to track seen elements, from a big array, using a dict.
Is there a way to force a dictionary object to be integer type and set to zero by default upon initialization?
I have done this with a very clunky codes and two loops.
Here is what I do now:
fl = [0, 1, 1, 2, 1, 3, 4]
seenit = {}
for val in fl:
seenit[val] = 0
for val in fl:
seenit[val] = seenit[val] + 1
Of course, just use collections.defaultdict([default_factory[, ...]]):
from collections import defaultdict
fl = [0, 1, 1, 2, 1, 3, 4]
seenit = defaultdict(int)
for val in fl:
seenit[val] += 1
print(fl)
# Output
defaultdict(<class 'int'>, {0: 1, 1: 3, 2: 1, 3: 1, 4: 1})
print(dict(seenit))
# Output
{0: 1, 1: 3, 2: 1, 3: 1, 4: 1}
In addition, if you don't like to import collections you can use dict.get(key[, default])
fl = [0, 1, 1, 2, 1, 3, 4]
seenit = {}
for val in fl:
seenit[val] = seenit.get(val, 0) + 1
print(seenit)
# Output
{0: 1, 1: 3, 2: 1, 3: 1, 4: 1}
Also, if you only want to solve the problem and don't mind to use exactly dictionaries you may use collection.counter([iterable-or-mapping]):
from collections import Counter
fl = [0, 1, 1, 2, 1, 3, 4]
seenit = Counter(f)
print(seenit)
# Output
Counter({1: 3, 0: 1, 2: 1, 3: 1, 4: 1})
print(dict(seenit))
# Output
{0: 1, 1: 3, 2: 1, 3: 1, 4: 1}
Both collection.defaultdict and collection.Counter can be read as dictionary[key] and supports the usage of .keys(), .values(), .items(), etc. Basically they are a subclass of a common dictionary.
If you want to talk about performance I checked with timeit.timeit() the creation of the dictionary and the loop for a million of executions:
collection.defaultdic: 2.160868141 seconds
dict.get: 1.3540439499999999 seconds
collection.Counter: 4.700308418999999 seconds
collection.Counter may be easier, but much slower.
You can use collections.Counter:
from collections import Counter
Counter([0, 1, 1, 2, 1, 3, 4])
Output:
Counter({1: 3, 0: 1, 2: 1, 3: 1, 4: 1})
You can then address it like a dictionary:
>>> Counter({1: 3, 0: 1, 2: 1, 3: 1, 4: 1})[1]
3
>>> Counter({1: 3, 0: 1, 2: 1, 3: 1, 4: 1})[0]
1
Using val in seenit is a bit faster than .get():
seenit = dict()
for val in fl:
if val in seenit :
seenit[val] += 1
else:
seenit[val] = 1
For larger lists, Counter will eventually outperform all other approaches. and defaultdict is going to be faster than using .get() or val in seenit.

Checking for key in dict during comprehension

Is it possible to do something like this:
l = [1, 2, 2, 3, 4, 4, 1, 1]
d = {num: [num] if num not in d else d[num].append(num) for num in l}
Inherently, I wouldn't think so, without declaring d = {} first; even then, it doesn't append:
Output: {1: [1], 2: [2], 3: [3], 4: [4]}
# desired: {1: [1, 1, 1], 2: [2, 2], 3: [3], 4: [4, 4]}
Could use a defaultdict, curious if the comprehension is even possible?
No, it's not possible. If you think about, it will make sense why. When Python evaluates an assignment statement, it first evaluates the right-hand side of the assignment - the expression. Since it hasn't evaluated the entire assignment yet, the variable on the left-hand hasn't been added to the current namespace yet. Thus, while the expression is being evaluated, the variable will be undefined.
As suggested, you can use collections.defaultdict to accomplish what you want:
>>> from collections import defaultdict
>>>
>>> l = [1, 2, 2, 3, 4, 4, 1, 1]
>>> d = defaultdict(list)
>>> for num in l:
d[num].append(num)
>>> d
defaultdict(<class 'list'>, {1: [1, 1, 1], 2: [2, 2], 3: [3], 4: [4, 4]})
>>>
d doesn't exist in your dictionary comprehension.
Why not:
l = [1, 2, 2, 3, 4, 4, 1, 1]
d = {num: [num] * l.count(num) for num in set(l)}
EDIT: I think, it is better to use a loop there
d = {}
for item in l:
d.setdefault(item, []).append(item)
No, you cannot refer to your list comprehension before the comprehension is assigned to a variable.
But you can use collections.Counter to limit those costly list.append calls.
from collections import Counter
l = [1, 2, 2, 3, 4, 4, 1, 1]
c = Counter(l)
d = {k: [k]*v for k, v in c.items()}
# {1: [1, 1, 1], 2: [2, 2], 3: [3], 4: [4, 4]}
Related: Create List of Single Item Repeated n Times in Python

Categories

Resources