เริ่มต้นใช้งานเริ่มต้นใช้งานได้ฟรี

Extracting tuples

In the previous exercise, you used two list comprehensions to create lists lengths and words that, respectively, refer to the lengths of the constituent lists in wlist and the longest words stored in those lists. In this exercise, you'll create them in a slightly different way. First, you'll need to put the same calculations into one list comprehension, which should result in a list of tuples. Second, apply the unzip operation to generate two distinct tuples, resembling lengths and words from the previous exercise.

The list wlist and the function get_longest_word() are already available in your workspace.

แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร

Practicing Coding Interview Questions in Python

ดูคอร์ส

คำแนะนำการฝึกหัด

  • Create a list of tuples each containing the length and the longest word of each item in wlist.
  • Unwrap the created list and create two distinct tuples.

แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ

ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์

# Create a list of tuples with lengths and longest words
result = [
    (____, ____) for item in ____
]

# Unzip the result    
lengths, words = ____

for item in zip(wlist, lengths, words):
    print(item)
แก้ไขและรันโค้ด