Open HananRosenthal opened 8 years ago
We are on it. In the meantime, please submit only the files in the text.
what are the correct names of the files in the text?
Hi, I am using the following input: add dad help more rome groom romog
My code follows:
import argparse
from collections import Counter
parser = argparse.ArgumentParser(description='look for anagrams in the input file')
parser.add_argument('-p','--path', help='', required = True)
kuku = parser.parse_args()
li=[]
freq={}
li1=[]
#Populate a list with all words in the input file (each word contains the \n at its end)
with open(kuku.path, 'r') as f:
for line in f:
li.append(line)
#print(li)
for word in li:
for letter in word:
count = freq.setdefault(letter,0)
freq[letter] = count + 1
li1.append(freq)
freq.clear()
I would expect the list (li1) to contain all words letter counting, however it contains the same item all over. Why is that? 10X
Hi Hanan,
This is a very interesting issue! Here's the short explanation with some code example to clarify:
a = { 'a': 10, 'b': 20 }
b = a
a.clear()
But now b
also points to an empty dict.
import argparse
from collections import Counter
parser = argparse.ArgumentParser(description='look for anagrams in the input file')
parser.add_argument('-p','--path', help='', required = True)
kuku = parser.parse_args()
li=[]
freq={}
li1=[]
#Populate a list with all words in the input file (each word contains the \n at its end)
with open(kuku.path, 'r') as f:
for line in f:
li.append(line)
#print(li)
for word in li:
for letter in word:
count = freq.setdefault(letter,0)
freq[letter] = count + 1
li1.append(dict(freq))
freq.clear()
when trying to use 'reduce', i get "NameError: name 'reduce' is not defined" ?
reduce
has been removed in Python 3 https://docs.python.org/3.0/whatsnew/3.0.html#builtins You can revive it using from functools import reduce
or you need to find another way to solve the problem.
In Data Strustures chapter: