Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Homework Help
#1
Hi, I currently need some help getting the index to show with the words I split and and made uppercase. This may me an easy fix or more complicated, I just don't understand what to do now. If anyone knows how to do this or has suggestions to guide me in the right path that would be greatly appreciated. Commented out is what the output should look like.


def get_inverted_index(text):
    newText = text.upper().split(" ")
    return newText
gettysburg_address = "Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the ..."
print(get_inverted_index(gettysburg_address))
# Output starts:
# {'FOUR': [0], 'SCORE': [1], 'AND': [2, 19, 48, 95,...
This is what I have so far:
['FOUR', 'SCORE', 'AND', 'SEVEN', 'YEARS', 'AGO', 'OUR', 'FATHERS', 'BROUGHT', 'FORTH', 'ON', 'THIS', 'CONTINENT,', 'A', 'NEW', 'NATION,', 'CONCEIVED', ..]
Reply
#2
Well, here's what you do:
  • Set a counter equal to zero.
  • Set up an empty dictionary
  • Loop through the words in new_text
    • If the word is not in the dictionary, add it with a list of the counter as the value.
    • If the word is in the dictionary, append the counter to that key.
    • Increment the counter
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(Dec-12-2018, 03:09 PM)ichabod801 Wrote: Well, here's what you do:
  • Set a counter equal to zero.
  • Set up an empty dictionary
  • Loop through the words in new_text
    • If the word is not in the dictionary, add it with a list of the counter as the value.
    • If the word is in the dictionary, append the counter to that key.
    • Increment the counter
thank you!!!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  HELP in python homework makashito 4 3,895 Oct-12-2021, 10:12 AM
Last Post: buran
  CyperSecurity Using Python HomeWork ward1995 1 1,948 Jul-08-2021, 03:55 PM
Last Post: buran
Exclamation urgent , Python homework alm 2 2,284 May-09-2021, 11:19 AM
Last Post: Yoriz
  Homework with python Johnsonmfw 1 1,676 Sep-20-2020, 04:03 AM
Last Post: ndc85430
  Python Homework Help *Urgent GS31 2 2,564 Nov-24-2019, 01:41 PM
Last Post: ichabod801
  Python Homework Question OrcDroid123 1 2,362 Sep-01-2019, 08:44 AM
Last Post: buran
  Python homework / functions sunhyunshine 1 2,435 May-11-2019, 05:37 PM
Last Post: MrTheOne
  python homework help ASAP gk34332 1 2,957 Mar-13-2019, 07:27 PM
Last Post: ichabod801
  Python homework assigment makisha 3 3,262 Feb-28-2019, 10:21 PM
Last Post: Yoriz
  python homework MajaW 3 3,145 Dec-11-2018, 10:45 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020