Public Group active 3 weeks ago

PUG – Python User’s Group

Python User’ Group (or PUG for short) is an open and informal collaborative space for experimentation and exploration with the Python programming language. It is an opportunity for those interested in Python to work together virtually and find support. Whether you are looking for advice or assistance with new or current projects, looking to discuss and learn new skills using Python tools, or to join us to play around with our collection of sample datasets, PUG is your place!

PUG is open to people of all skill levels, disciplines, and backgrounds. Complete beginners to Python will find a place here. Come, and let’s learn together.

Join PUG Slack here: https://join.slack.com/t/pug-world/shared_invite/zt-iube7uch-nVkvtIyIbpaqtQSZcMB2Ig

PUG is cosponsored by the MA in Digital Humanities / MS in Data Analytics and Visualization programs and the Mina Rees Library.

To learn more, visit http://cuny.is/pug

Admins:

seeking Help for Comp Ling Methods Homework Assignment: Bigrams

  • Hi, group.
    I would greatly appreciate some debugging help, related to a homework assignment in the Comp Ling Methods I course.
    The assigned task was to define a function to list bigrams of a list of strings.
    Feeling stuck in the assignment, I decided to try something simpler:  rather than defining a function, I tried to create a list of bigrams for a specific list.
    For the following list: LSD = [“Lucy”, “in”, “the”, “sky”, “with”, “diamonds”]
    I want to produce code to give me a list of word pairs:
    [[“Lucy”, “in”], [“in”, “the”], [“the”, “sky”], [“sky”, “with”], [“with”, “diamonds”]]

    My attempted code gives me only the strings in the original list, setting them within individual lists, but not as pairs.  I’ve attached a PDF of my Jupyter notebook, and will also paste the failed code here:

    LSD = [“Lucy”, “in”, “the”, “sky”, “with”, “diamonds”]
    #produce list of bigrams

    pairs = []
    for x in range(len(LSD)):
    pair = LSD[x:x +1]
    pairs += [pair]

    print(pairs)

    The result I get is:
    [['Lucy'], ['in'], ['the'], ['sky'], ['with'], ['diamonds']]

    Thanks,
    Shani

     

    Attachments:
    You must be logged in to view attached files.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Hi Shani,
    You’re close. Take a look at this line:
    pair = LSD[x:x +1]
    You want to grab the next word on the list, but x + 1 won’t do that. you have to refer to the list and get the index of the word you want. And, in order to refer to the index in a for loop, you have to use ‘enumerate’. So here’s what I got:

    pairs = []
    for index, word in enumerate(LSD):
         try:
              next_word = LSD[index+1]
              pairs.append([word, next_word])
         except IndexError:
              pass
    print(pairs)

    I added the ‘try’ clause because otherwise you get an error at the end of the list, when it tried to look for the next word and there isn’t one.

    Hi Shani,

     

    I don’t want to give you too many hints, since I think you’re pretty close to a solution here. But this REPL session might give you an idea of what’s happening:

    https://gist.githubusercontent.com/smythp/f3aa9b9fa8cabd4560a1dc4625c34658/raw/7d609d7e80535e8f37b29275378d3212ec44793a/gistfile1.txt

    Hope that helps a little.

     

    Patrick

    Thanks for replies!  Sorry I did not respond sooner.  I became overwhelmed and pre-occupied with some personal stuff– including the birth of my first granddaughter last week.  I ended up submitting my incomplete code for the assignment. I am trying to reboot my studies now, and hope to try your advice in the coming days.
    In any case, it was really helpful to get support when I was feeling stuck.  And you both seemed to have hit the right register– of advising and guiding, rather than just feeding me an answer.  What a great forum.

    Congratulations on the birth of your granddaughter!

    Congratulations, Shani, on a new grandchild! And glad that you could find
    the help here. It was a fun challenge to watch you all work through. Hoping
    to see more of them!!!!
    Best,
    Lisa

    ~~~~~~~~~~~~~~~~~
    Lisa Rhody, Ph.D.
    Deputy Director of Digital Initiatives
    Director, Digital Fellowship Programs
    Director, Digital Humanities Research Institutes
    The Graduate Center, CUNY
    lrhody@gc.cuny.edu | @lmrhody

Viewing 5 replies - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.