firstly thanks for clicking in.
I am just a beginner and have no background.
so I have two list of words, one is a checklist, another one is a sort of article,
what I need to do is use the words in the checklist to check the article, see how many time it repeated.
here is what I wrote:
my idea is use for loop to take one word in the first list and use it to compare with each word of the second list, and take another word in the first list and use it to compare with each word of the second list until the first list's words are all done.
what's the problem of my code?
could anyone just some suggestion for my code.
CodePudding user response:
Some error handling fixes your code right up. You see, your code trys to add numbers to a key that doesn't exist. Then, if the word isn't in it, it creates that value at 1?
Try this instead:
def check_lists(listA,listB):
dictionary={}
for x in listA:
for y in listB
if x == y:
try:
dictionary[x] =1
except KeyError:
dictionary[x]=1
return dictionary
This basically takes two lists and compares each value in listA against each value in listB. It then tries to add one to the key in the dictionary, and if the key doesn't exist (hence the try
and except
statements) creates a key and adds one.
Call it like this:
check_dict=check_lists(joylist,poslist)
CodePudding user response:
If I understand your post correctly, you have an article (or a long list of words, called joylist
in your example) and you want to extract the word count for a subset of these (poslist
in your case)?
Looking at your current solution, you'll get what you want with a little more work (@Agent Biscuit's solution), but this is not the most efficient solution because you'll be iterating over the entire keywords list every time, which you don't need to do. Since you're using a dictionary anyway, you might as well pre-load it:
# Create a new dictionary from the checklist with counts of 0
word_counts = {x: 0 for x in poslist}
# Iterate over all the words in the article
for word in joylist:
# Check if the word is one we care about tracking. If it is then
# increment the count associated with that word by one
if word in word_counts:
word_counts[word] = 1
If you don't want to use a list initialization to create your dictionary then we can do this from an empty dictionary as well:
# Create the word-count dictionary from our list of keywords
word_counts = dict()
for word in poslist:
word_counts[word] = 0
# Iterate over all the words in the article
for word in joylist:
# Check if the word is one we care about tracking. If it is then
# increment the count associated with that word by one
if word in word_counts:
word_counts[word] = 1
Finally, if you want to filter out counts of zero then you can do that too:
for k in list(word_counts.keys()):
if word_counts[k] == 0:
del word_counts[k]
CodePudding user response:
You can use the get method of the dict.
poslist = ['a', 'b', 'c', 't']
joylist = ['a', 'd', 'b', 'n', 'a', 'd', 'x', 'm']
c = {}
for j in joylist:
if j in poslist:
c[j] = c.get(j, 0) 1
else:
c[j] = c.get(j, 0)
print(c)
Output:
{'a': 2, 'd': 0, 'b': 1, 'n': 0, 'x': 0, 'm': 0}