For example I have the following set up:
dic = {"A":0, "B":0, "C":0}
tokens = ["A", "B", "C", "C", "D", "E", "F"]
If the element in the token exists in the dictionary keys, increment the value by 1.
How do I do this without using the loop?
I have the following for
loop right now
for key in dict.keys():
if key in tokens:
dict[key] = 1
CodePudding user response:
This is a no-loop version using Counter:
from collections import Counter
dic = {"A": 0, "B": 0, "C": 0}
tokens = ["A", "B", "C", "C", "D", "E", "F"]
res = Counter(filter(dic.__contains__, tokens))
print(res)
Output
{'C': 2, 'A': 1, 'B': 1}
UPDATE
If there is a key in dic
, that is not present in tokens
, you could do:
from collections import Counter
dic = {"A": 0, "B": 0, "C": 0, "Z": 0}
tokens = ["A", "B", "C", "C", "D", "E", "F"]
dic.update(Counter(filter(dic.__contains__, tokens)))
print(dic)
Output
{'A': 1, 'B': 1, 'C': 2, 'Z': 0}
CodePudding user response:
You can use a recursive approach with update the dictionary by slicing one by one each tokens.
dic = {"A":0, "B":0, "C":0}
tokens = ["A", "B", "C", "C", "D", "E", "F"]
def key_counter(d, tokens):
if tokens == []:
return
if tokens[0] in d:
d[tokens[0]] = 1
key_counter(d, tokens[1:])
key_counter(dic, tokens)
print(dic)
Output
{'A': 1, 'B': 1, 'C': 2}
CodePudding user response:
Assuming that initial values in dic are always zero, you can map the dictionary keys to the tokens's count method and reform the dictionary using zip:
dic = {"A":0, "B":0, "C":0}
tokens = ["A", "B", "C", "C", "D", "E", "F"]
dic = dict(zip(dic,map(tokens.count,dic)))
print(dic)
# {'A': 1, 'B': 1, 'C': 2}
This way, you have no import library and no for-loop (not even inside a comprehension).
but if you are concerned about performance and don't mind using a library, I would suggest using the Counter class instead of the list's .count() method:
dic = dict(zip(dic,map(Counter(tokens).__getitem__,dic)))
If initial values in dic can be other than zero, you will need to add them in:
dic = dict(zip(dic,map(lambda k:dic[k] tokens.count(k),dic)))
# or
dic = dict(zip(dic,map((Counter(dic) Counter(tokens)).__getitem__,dic)))
Alternatively, you can write a recursive function (but that is much slower and will be limited by the maximum recursion depth):
def countTokens(counts,token,*more):
if token in counts: counts[token] = 1
if more: countTokens(counts,*more)
dic = {"A": 0, "B": 0, "C": 0}
tokens = ["A", "B", "C", "C", "D", "E", "F"]
countTokens(dic,*tokens)
print(dic)
{'A': 1, 'B': 1, 'C': 2}
CodePudding user response:
You need to iterate on tokens
to read them all, not only on the keys you already set
dic = {"A": 0, "B": 0, "C": 0}
tokens = ["A", "B", "C", "C", "C", "D", "E", "F"]
for token in tokens:
if token in dic:
dic[token] = 1
print(dic) # {'A': 1, 'B': 1, 'C': 3}
Without a for loop you could use collections.Counter
, but that doesn't filtering
dic = Counter(tokens)
print(dic) # {'C': 3, 'A': 1, 'B': 1, 'D': 1, 'E': 1, 'F': 1}
CodePudding user response:
you can use comprehension
dic = { k:v tokens.count(k) for k,v in dic.items() }
but this is inefficient if the dictionary has a large number of items and tokens is a small list
CodePudding user response:
= {"A":0, "B":0, "C":0} tokens = ["A", "B", "C", "C", "D", "E", "F"] 1# convert list to set tokens 2# convert set to list 3# sort list 4# delete character D , E , F 5# convert list to tuples 6# convert to tuple to dictionary and apply dictionary 7# simple trick you take form user input