Created
June 28, 2022 14:35
-
-
Save Venkat-Swaraj/46da155d0a04451a0a80dab23fa977b2 to your computer and use it in GitHub Desktop.
Files-Replit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def category_dict(filename): | |
# Write your logic here | |
dictionary = {} | |
fp = open(filename,'r') | |
names = fp.readlines() | |
for name in names: | |
words = name.split(' ') | |
words[1] = words[1].strip('\n') | |
if words[1] in dictionary: | |
dictionary[words[1]].append(words[0]) | |
else: | |
dictionary[words[1]] = [words[0]] | |
return dictionary | |
if __name__=="__main__": | |
print(category_dict("places1.txt")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def file_to_dict(filename): | |
dictionary = {} | |
# Write your logic here | |
fp = open(filename,'r') | |
lines = fp.readlines() | |
for line in lines: | |
dict = line.split(" - ") | |
words = dict[1].split(', ') | |
for i in range(len(words)): | |
words[i] = words[i].strip() | |
words[i] = words[i].strip('\n') | |
dictionary[dict[0]] = words | |
return dictionary | |
def main(): | |
filename = input() | |
print(file_to_dict(filename)) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment