1

I'm new to programming and I'm doing a text mining task. I have a dictionary with the keys as tokens and values as the occurrence of the token in the database.

dic={'a':48,'b':32,'c':26}

and also I have a list of tokens, and I'm trying to create a new list with the occurrence of each token. if a token is not found in the dictionary, append 0.

my_list=['a','b','d','c','a']


ideal_output=[48,32,0,26,48]

my original code is like this:

for word in my_list:
    for k,v in dic.items():        
       if word==k:
           ideal_output.append(v)
       else:
           ideal_output.append('0')

but it would yield more integers than I expected, I don't know what's wrong with it. I'd appreciate your help!

current output looks like this

['0', 48, '0', '0', '0', 32, '0', '0', '0', 26, '0', '0', '0', 48, '0']

bb Yang
  • 51
  • 6
  • 1
    Please edit post and show your current output – pstatix May 14 '17 at 15:17
  • Possible duplicate of [Searching array reports "not found" even though it's found](http://stackoverflow.com/questions/42913798/searching-array-reports-not-found-even-though-its-found) – Barmar May 14 '17 at 15:18

4 Answers4

2
my_list=['a','b','d','c','a']
ideal_output = [dic.get(token, 0) for token in my_list]
mohammad
  • 2,232
  • 1
  • 18
  • 38
1
dic={'a':48,'b':32,'c':26}
my_list=['a','b','d','c','a']


ideal_output=[]

for key in my_list:
  if key in dic:
    value = dic[key]
    ideal_output.append(value)
  else:
    ideal_output.append(0)

print(ideal_output)    
Fuji Komalan
  • 1,979
  • 16
  • 25
0

You could use a collections.defaultdict. In the example below, it will return 0 for every key that is not found.

In [1]: from collections import defaultdict

In [2]: di = defaultdict(lambda: 0, {'a':48,'b':32,'c':26})

In [3]: di['a']
Out[3]: 48

In [4]: di['x']
Out[4]: 0

In [5]: di['p']
Out[5]: 0

But since you seem to be counting letters in a text, have a look at collections.Counter.

In [6]: from collections import Counter

In [7]: c = Counter('This is a text')

In [8]: c
Out[8]: 
Counter({' ': 3,
         'T': 1,
         'a': 1,
         'e': 1,
         'h': 1,
         'i': 2,
         's': 2,
         't': 2,
         'x': 1})
Roland Smith
  • 42,427
  • 3
  • 64
  • 94
-1

Try this, to avoid the second for loop:

ideal_output=[]

for element in my_list:
  if element in dic.keys():
    ideal_output.append(dic[element])
  else:
    ideal_output.append(0)

print(ideal_output)
Stuti Rastogi
  • 1,162
  • 2
  • 16
  • 26