I want to create a dictionary within my function, but I cannot. The only possible solution I have found is to create the name of the dictionary outside the function and before calling it.
argentina = {}
def get_dic(file,phone):
for line in file.readlines():
if (line[0] == '#'):
names = line.rstrip().strip()
phone[names] = ''
else:
phone[names] = phone[names] + line.rstrip().strip()
get_dic(open(sys.argv[1],'r'), argentina)
get_dic(open(sys.argv[1],'r'), argentina) will open a dictionary called 'argentina', but I wont need to create argentina = {} in advance.
Just create the dict inside and return it from the function:
def get_dic(file):
phone = {}
names = None
for line in file.readlines():
if line[0] == '#':
names = line.strip()
phone[names] = ""
else:
phone[names] += line.strip()
return phone
argentina = get_dic(open(sys.argv[1],'r'))
Related
I'm following a tutorial (uploaded oct. 2020) about web scraping and storing the data in a dictionary. Everything works fine except the data in my dictionary starts with the newest entry and ends with the first one.
Should: {title,........, budget}
Is: {budget,........, title}
What could be the resaon for this to happen?
Part of the code:
def get_content_value(row_data):
if row_data.find("li"):
return [li.get_text(" ", strip = True).replace("\xa0", " ") for li in row_data.find_all("li")]
else:
return row_data.get_text(" ", strip = True).replace("\xa0", " ")
movie_info = {}
for index, row in enumerate(info_rows):
if index == 0:
movie_info['title'] = row.find("th").get_text(" ", strip = True)
elif index == 1:
continue
else:
content_key = row.find("th").get_text(" ", strip = True)
content_value = get_content_value(row.find("td"))
movie_info[content_key] = content_value
movie_info
index == 0 is the title
index == 1 is a picture i don't want to have
EDIT:
It's not reversed, it's in alphabetical order! Why? And how can i change it to chronological order?
Dictionaries are inherently not sorted/ordered. This is unlike lists and tuples that are ordered. To get around this issue, the collections library has OrderedDict.
You can use something like this:
# Import the OrderedDict object from `collections`
from collections import OrderedDict
def get_content_value(row_data):
if row_data.find("li"):
return [li.get_text(" ", strip = True).replace("\xa0", " ") for li in row_data.find_all("li")]
else:
return row_data.get_text(" ", strip = True).replace("\xa0", " ")
# Instead of using a regular dictionary ("{}"), we set `movie_info` to be an OrderedDict
movie_info = OrderedDict()
for index, row in enumerate(info_rows):
if index == 0:
movie_info['title'] = row.find("th").get_text(" ", strip = True)
elif index == 1:
continue
else:
content_key = row.find("th").get_text(" ", strip = True)
content_value = get_content_value(row.find("td"))
movie_info[content_key] = content_value
movie_info
It's a bit tricky, but you can do it like below.
from collections import OrderedDict
dictionary = {'one': 1, 'two': 2, 'three':3}
res = OrderedDict(reversed(list(dictionary.items())))
This is my code. I'm new to python and I have defenitely messed up. I'm trying to create a program that calculates the frequency of words in a text file called 'romeoandjuliet.txt'. When I run it, nothing happens and the f1 and f2 files are not created. When type 'listofwords' or 'freq' in the console I get a NameError. I have already taken care of the stopwords situation. I appreciate any help.
def main():
listofwords = formlistofwords('romeoandjuliet.txt')
def formlistofwords(filename):
global listofwords
listofwords = formlistofwords("romeoandjuliet.txt")
infile = open(filename)
originalline = (infile.readline().lower() for line in infile)
line = " "
for ch in originalline:
if ('a' <= ch <= 'z') or (ch == " "):
line += ch
listofwords = line.split()
listofwords = [i.strip('.,?!:;') for i in listofwords]
listofwords = [words for words in listofwords if words not in stopwords]
return listofwords
def createfrequencydict(listofwords):
global freq
freq = [listofwords.count(p) for p in listofwords]
return dict(list(zip(listofwords, freq)))
def displaywordcount(listofwords, freq):
print('romeoandjuliet contains: ', len(listofwords), 'words')
print('romeoandjuliet contains: ', len(freq), 'different words')
print()
def displaymostcommon(freq):
file1 = open('f1.txt', 'w')
file2 = open('f2.txt', 'w')
print('most common words and frequencies are: ')
listofmostcommonwords = []
for word in freq.keys():
if freq[word] >= 30:
listofmostcommonwords.append((word, freq[word]))
listofmostcommonwords.sort(key = lambda x: x[1], reverse=True)
for item in listofmostcommonwords:
print(item[0] + ':', item[1])
for item in listofmostcommonwords:
file1.write(str(item[1]) + ',')
file2.write(str(item[0]) + ',')
file1.close()
file2.close()
I studied your code. I noticed following points:
You need to call each function in order to execute
You have some global variables such as listofwords,freq. Declare them and assign None to them. Then call the functions at then end passing these variable. This way code is working
Following return statement
return dict(list(zip(listofwords, freq)))
Here the return statement is not returning dictionary. It is returning list. Instead of this, first try this using for loop. Here is the code that worked for me
def createfrequencydict(listofword):
global freq
freq = [listofwords.count(p) for p in listofwords]
#print(dict(list(zip(listofwords, freq))))
for key in listofwords:
for value in freq:
res[key] = value
freq.remove(value)
break
return res
def file_read ():
dictionary = {}
state = ""
capital = ""
flag = 0
with open("state_capitals.txt","r") as f:
for line in f:
if flag == 0:
state = line.rstrip('\n')
flag = 1
elif flag == 1:
capital = line.rstrip('\n')
flag = 0
dictionary[state] = capital
print(dictionary)
How would I use a while loop to make the dictionary instead of the for loop. Code works perfectly fine but professor insists I use a while loop.
def file_read():
dictionary = {}
state = ""
capital = ""
flag = 0
with open("state_capitals.txt", "r") as f:
while True:
line = f.readline()
if not line:
break
if flag == 0:
state = line.rstrip('\n')
flag = 1
elif flag == 1:
capital = line.rstrip('\n')
flag = 0
dictionary[state] = capital
print(dictionary)
Hi guys i got my code to work by putting everything in one function which is this
spam = ''
def enterList (names):
newList = []
while True:
names = raw_input('list a series of items and press blank when finished: ')
if names == '':
break
newList = newList + [names]
a = ''
finalText = ''
listOfStuff = []
item = 0
for i in newList:
if item < len(newList)-2:
a = (i + ', ')
listOfStuff.append(a)
item +=1
elif item == len(newList)-2:
a = (i + ' and ')
listOfStuff.append(a)
item +=1
else:
a = i
listOfStuff.append(a)
break
finalText = finalText.join(listOfStuff)
return finalText
print enterList(spam)
So the above code works as i want it to. However i was trying to do the same thing by having two separate functions, the issue that i was having was that i couldn't take the return value of one function and use it in the next function.
This is the old code
spam = ''
def enterList (names):
newList = []
while True:
names = raw_input('list a series of items and press blank when finished: ')
if names == '':
break
newList = newList + [names]
return newList
print enterList(spam)
def newFunc(Addand):
a = ''
finalText = ''
listOfStuff = []
item = 0
for i in spam:
if item < len(spam)-2:
a = (i + ', ')
listOfStuff.append(a)
item +=1
elif item == len(spam)-2:
a = (i + ' and ')
listOfStuff.append(a)
item +=1
else:
a = i
listOfStuff.append(a)
break
finalText = finalText.join(listOfStuff)
return finalText
newFunc(spam)
print newFunc (spam)
I'm not sure what I was doing wrong doing it this way.
Thanks for any help to get my head around the error with this approach.
In your first function make the return statement
return newFunc(newlist)
It's not working because the second function is never actually called.
so here's some code i have that is supposed to take text entered by the user and create a dictionary. Can anyone tell me why I get a traceback error when I call the function main()?
def build_index(text):
index = {}
words = text.split()
position = 0
for x in text:
if x.isalpha() == False and x.isdigit() == False:
text.join(x)
else:
text.replace(x,'')
while position < len(words):
nextword = words[position]
if nextword in index:
ref = index[nextword]
ref.append(position)
index[nextword] = ref
else:
list = []
list.append(position)
index[nextword] = list
position += 1
def displayindex(index):
keys = sorted(index.keys())
for key in keys:
print(key + ':' + str(index[key]))
def main():
text = input("enter text")
build_index(text)
displayindex(index)
main()
The traceback error contents depends on which version of Python you're running your code in. In Python 3.x, the traceback explains why it's producing the error:
Traceback (most recent call last):
File "./prog.py", line 37, in
File "./prog.py", line 36, in main
NameError: name 'index' is not defined
TLDR: Need to add/change only 3 lines of code. See comments in code below
The NameError is telling us that it doesn't know what the name index refers to, because it's out of the scope of the main method and hasn't gotten defined yet. You could create the global instance of the index variable as mentioned in MeterLongCat's answer, but since index does get created and defined when we call build_index, we can just return index after that method call, save its return value, then pass it to the displayindex function, as follows.
OTOH, in Python 2, as MeterLongCat points out, you're wanting to get a string from the user, which is not what input is for, you want raw_input.
def build_index(text):
index = {}
words = text.split()
position = 0
for x in text:
if x.isalpha() == False and x.isdigit() == False:
text.join(x)
else:
text.replace(x,'')
while position < len(words):
nextword = words[position]
if nextword in index:
ref = index[nextword]
ref.append(position)
index[nextword] = ref
else:
list = []
list.append(position)
index[nextword] = list
position += 1
return index # Return the index
def displayindex(index):
keys = sorted(index.keys())
for key in keys:
print(key + ':' + str(index[key]))
def main():
text = raw_input("enter text") # Use raw_input
index = build_index(text) # Assign the index
displayindex(index)
main()
I was able to get rid of the traceback error by changing input to raw_input (for Python 2.7). You have other errors though, for example index in method main isn't defined. The following works:
index = {}
def build_index(text):
global index
words = text.split()
position = 0
for x in text:
if x.isalpha() == False and x.isdigit() == False:
text.join(x)
else:
text.replace(x,'')
while position < len(words):
nextword = words[position]
if nextword in index:
ref = index[nextword]
ref.append(position)
index[nextword] = ref
else:
list = []
list.append(position)
index[nextword] = list
position += 1
def displayindex(index):
keys = sorted(index.keys())
for key in keys:
print(key + ':' + str(index[key]))
def main():
global index
text = raw_input("enter text")
build_index(text)
displayindex(index)
main()