Bird
0
0

Given a list words = ['data', 'science', 'rocks'], which Python list comprehension correctly generates bigrams?

easy📝 Syntax Q3 of 15
NLP - Text Generation
Given a list words = ['data', 'science', 'rocks'], which Python list comprehension correctly generates bigrams?
A[(words[i], words[i+1]) for i in range(len(words)-1)]
B[(words[i], words[i+1]) for i in range(len(words))]
C[(words[i], words[i+2]) for i in range(len(words)-2)]
D[(words[i], words[i-1]) for i in range(1, len(words))]
Step-by-Step Solution
Solution:
  1. Step 1: Understand bigrams

    Bigrams are pairs of consecutive words, so indices must be adjacent.
  2. Step 2: Check list comprehension range

    To avoid index out of range, iterate up to len(words)-1.
  3. Step 3: Verify each option

    [(words[i], words[i+1]) for i in range(len(words)-1)] correctly pairs words[i] and words[i+1] for valid indices.
  4. Final Answer:

    [(words[i], words[i+1]) for i in range(len(words)-1)] -> Option A
  5. Quick Check:

    Range must prevent index overflow [OK]
Quick Trick: Use range(len(words)-1) for bigrams [OK]
Common Mistakes:
MISTAKES
  • Using range(len(words)) causes index error
  • Skipping the last bigram by incorrect range
  • Pairing non-adjacent words

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NLP Quizzes