Hướng dẫn how to select a word from a list in python - cách chọn một từ từ danh sách trong python

people = [  # a list of lists
  ["Joe", "painter", "single"],
  ["Jane", "writer", "divorced"],
  ["Mario", "plumber", "married"]
]

for person_info in people:
  print "Name:", person_info[0], "job:", person_info[1], "is", person_info[2]
0 là một vòng lặp. Cơ thể của nó [phần thụt lề] sẽ thực thi một lần cho mỗi yếu tố của
people = [  # a list of lists
  ["Joe", "painter", "single"],
  ["Jane", "writer", "divorced"],
  ["Mario", "plumber", "married"]
]

for person_info in people:
  print "Name:", person_info[0], "job:", person_info[1], "is", person_info[2]
1.

Bạn có thể truy cập các yếu tố

people = [  # a list of lists
  ["Joe", "painter", "single"],
  ["Jane", "writer", "divorced"],
  ["Mario", "plumber", "married"]
]

for person_info in people:
  print "Name:", person_info[0], "job:", person_info[1], "is", person_info[2]
1 theo chỉ mục, như các câu trả lời khác đề xuất. Bạn cũng có thể giải nén danh sách thành các biến có tên hợp lý:

name, job, marital_status = info # the unpacking: 3 variables for 3 list items
print "My name is", name, "my job is", job, "I am", marital_status

Vòng lặp có ích khi bạn có một danh sách mọi người để xử lý:

people = [  # a list of lists
  ["Joe", "painter", "single"],
  ["Jane", "writer", "divorced"],
  ["Mario", "plumber", "married"]
]

for person_info in people:
  print "Name:", person_info[0], "job:", person_info[1], "is", person_info[2]

Nó là thành ngữ để giải nén các mục lồng nhau ngay trong vòng lặp:

for name, job, marital_status in people:
  print "Person named", name, "works as a", job, "and is", marital_status

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc

    Bàn luận
    The combination of above methods can be used to solve this problem. In this, we perform the task of getting Nth word using split and recreate list using list comprehension.

    Đôi khi, trong khi làm việc trong khi làm việc với danh sách Python, chúng ta có thể gặp vấn đề trong đó chúng ta cần thực hiện nhiệm vụ trích xuất từ ​​thứ n của mỗi chuỗi trong danh sách. Điều này có thể có ứng dụng trong miền phát triển web. Hãy thảo luận về những cách nhất định trong đó nhiệm vụ này có thể được thực hiện.

    Phương pháp số 1: Sử dụng danh sách hiểu +

    people = [  # a list of lists
      ["Joe", "painter", "single"],
      ["Jane", "writer", "divorced"],
      ["Mario", "plumber", "married"]
    ]
    
    for person_info in people:
      print "Name:", person_info[0], "job:", person_info[1], "is", person_info[2]
    
    3 Sự kết hợp của các phương thức trên có thể được sử dụng để giải quyết vấn đề này. Trong đó, chúng tôi thực hiện nhiệm vụ nhận từ thứ n bằng cách sử dụng danh sách chia và tạo lại danh sách bằng cách hiểu danh sách.

    people = [  # a list of lists
      ["Joe", "painter", "single"],
      ["Jane", "writer", "divorced"],
      ["Mario", "plumber", "married"]
    ]
    
    for person_info in people:
      print "Name:", person_info[0], "job:", person_info[1], "is", person_info[2]
    
    4
    people = [  # a list of lists
      ["Joe", "painter", "single"],
      ["Jane", "writer", "divorced"],
      ["Mario", "plumber", "married"]
    ]
    
    for person_info in people:
      print "Name:", person_info[0], "job:", person_info[1], "is", person_info[2]
    
    5
    people = [  # a list of lists
      ["Joe", "painter", "single"],
      ["Jane", "writer", "divorced"],
      ["Mario", "plumber", "married"]
    ]
    
    for person_info in people:
      print "Name:", person_info[0], "job:", person_info[1], "is", person_info[2]
    
    6
    people = [  # a list of lists
      ["Joe", "painter", "single"],
      ["Jane", "writer", "divorced"],
      ["Mario", "plumber", "married"]
    ]
    
    for person_info in people:
      print "Name:", person_info[0], "job:", person_info[1], "is", person_info[2]
    
    7
    people = [  # a list of lists
      ["Joe", "painter", "single"],
      ["Jane", "writer", "divorced"],
      ["Mario", "plumber", "married"]
    ]
    
    for person_info in people:
      print "Name:", person_info[0], "job:", person_info[1], "is", person_info[2]
    
    8
    people = [  # a list of lists
      ["Joe", "painter", "single"],
      ["Jane", "writer", "divorced"],
      ["Mario", "plumber", "married"]
    ]
    
    for person_info in people:
      print "Name:", person_info[0], "job:", person_info[1], "is", person_info[2]
    
    9
    people = [  # a list of lists
      ["Joe", "painter", "single"],
      ["Jane", "writer", "divorced"],
      ["Mario", "plumber", "married"]
    ]
    
    for person_info in people:
      print "Name:", person_info[0], "job:", person_info[1], "is", person_info[2]
    
    8
    for name, job, marital_status in people:
      print "Person named", name, "works as a", job, "and is", marital_status
    
    1
    people = [  # a list of lists
      ["Joe", "painter", "single"],
      ["Jane", "writer", "divorced"],
      ["Mario", "plumber", "married"]
    ]
    
    for person_info in people:
      print "Name:", person_info[0], "job:", person_info[1], "is", person_info[2]
    
    8
    for name, job, marital_status in people:
      print "Person named", name, "works as a", job, "and is", marital_status
    
    3
    for name, job, marital_status in people:
      print "Person named", name, "works as a", job, "and is", marital_status
    
    4

    for name, job, marital_status in people:
      print "Person named", name, "works as a", job, "and is", marital_status
    
    5
    for name, job, marital_status in people:
      print "Person named", name, "works as a", job, "and is", marital_status
    
    6
    for name, job, marital_status in people:
      print "Person named", name, "works as a", job, "and is", marital_status
    
    7
    for name, job, marital_status in people:
      print "Person named", name, "works as a", job, "and is", marital_status
    
    8
    for name, job, marital_status in people:
      print "Person named", name, "works as a", job, "and is", marital_status
    
    9
    The original list is : ['Gfg best for', 'All geeks', 'It is for', 'all CS professionals']
    The Nth words in list are : ['best', 'geeks', 'is', 'CS']
    
    0

    The original list is : ['Gfg best for', 'All geeks', 'It is for', 'all CS professionals']
    The Nth words in list are : ['best', 'geeks', 'is', 'CS']
    
    1
    people = [  # a list of lists
      ["Joe", "painter", "single"],
      ["Jane", "writer", "divorced"],
      ["Mario", "plumber", "married"]
    ]
    
    for person_info in people:
      print "Name:", person_info[0], "job:", person_info[1], "is", person_info[2]
    
    5
    The original list is : ['Gfg best for', 'All geeks', 'It is for', 'all CS professionals']
    The Nth words in list are : ['best', 'geeks', 'is', 'CS']
    
    3

    The original list is : ['Gfg best for', 'All geeks', 'It is for', 'all CS professionals']
    The Nth words in list are : ['best', 'geeks', 'is', 'CS']
    
    4
    people = [  # a list of lists
      ["Joe", "painter", "single"],
      ["Jane", "writer", "divorced"],
      ["Mario", "plumber", "married"]
    ]
    
    for person_info in people:
      print "Name:", person_info[0], "job:", person_info[1], "is", person_info[2]
    
    5
    The original list is : ['Gfg best for', 'All geeks', 'It is for', 'all CS professionals']
    The Nth words in list are : ['best', 'geeks', 'is', 'CS']
    
    6
    The original list is : ['Gfg best for', 'All geeks', 'It is for', 'all CS professionals']
    The Nth words in list are : ['best', 'geeks', 'is', 'CS']
    
    7
    The original list is : ['Gfg best for', 'All geeks', 'It is for', 'all CS professionals']
    The Nth words in list are : ['best', 'geeks', 'is', 'CS']
    
    8
    The original list is : ['Gfg best for', 'All geeks', 'It is for', 'all CS professionals']
    The Nth words in list are : ['best', 'geeks', 'is', 'CS']
    
    9
    The original list is : ['Gfg best for', 'All geeks', 'It is for', 'all CS professionals']
    The Nth words in list are : ['best', 'geeks', 'is', 'CS']
    
    0

    for name, job, marital_status in people:
      print "Person named", name, "works as a", job, "and is", marital_status
    
    5
    for name, job, marital_status in people:
      print "Person named", name, "works as a", job, "and is", marital_status
    
    6
    Input : 'Hello World'
    Output : ['Hello', 'world']
    1
    for name, job, marital_status in people:
      print "Person named", name, "works as a", job, "and is", marital_status
    
    8
    for name, job, marital_status in people:
      print "Person named", name, "works as a", job, "and is", marital_status
    
    9
    Input : 'Hello World'
    Output : ['Hello', 'world']
    4

    Đầu ra:
    The combination of above functions can be used to perform this task. In this, we use enumerate to check for the Nth word rather than split[].

    Đôi khi, trong khi làm việc trong khi làm việc với danh sách Python, chúng ta có thể gặp vấn đề trong đó chúng ta cần thực hiện nhiệm vụ trích xuất từ ​​thứ n của mỗi chuỗi trong danh sách. Điều này có thể có ứng dụng trong miền phát triển web. Hãy thảo luận về những cách nhất định trong đó nhiệm vụ này có thể được thực hiện.

    Phương pháp số 1: Sử dụng danh sách hiểu +

    people = [  # a list of lists
      ["Joe", "painter", "single"],
      ["Jane", "writer", "divorced"],
      ["Mario", "plumber", "married"]
    ]
    
    for person_info in people:
      print "Name:", person_info[0], "job:", person_info[1], "is", person_info[2]
    
    3 Sự kết hợp của các phương thức trên có thể được sử dụng để giải quyết vấn đề này. Trong đó, chúng tôi thực hiện nhiệm vụ nhận từ thứ n bằng cách sử dụng danh sách chia và tạo lại danh sách bằng cách hiểu danh sách.

    people = [  # a list of lists
      ["Joe", "painter", "single"],
      ["Jane", "writer", "divorced"],
      ["Mario", "plumber", "married"]
    ]
    
    for person_info in people:
      print "Name:", person_info[0], "job:", person_info[1], "is", person_info[2]
    
    4
    people = [  # a list of lists
      ["Joe", "painter", "single"],
      ["Jane", "writer", "divorced"],
      ["Mario", "plumber", "married"]
    ]
    
    for person_info in people:
      print "Name:", person_info[0], "job:", person_info[1], "is", person_info[2]
    
    5
    people = [  # a list of lists
      ["Joe", "painter", "single"],
      ["Jane", "writer", "divorced"],
      ["Mario", "plumber", "married"]
    ]
    
    for person_info in people:
      print "Name:", person_info[0], "job:", person_info[1], "is", person_info[2]
    
    6
    people = [  # a list of lists
      ["Joe", "painter", "single"],
      ["Jane", "writer", "divorced"],
      ["Mario", "plumber", "married"]
    ]
    
    for person_info in people:
      print "Name:", person_info[0], "job:", person_info[1], "is", person_info[2]
    
    7
    people = [  # a list of lists
      ["Joe", "painter", "single"],
      ["Jane", "writer", "divorced"],
      ["Mario", "plumber", "married"]
    ]
    
    for person_info in people:
      print "Name:", person_info[0], "job:", person_info[1], "is", person_info[2]
    
    8
    people = [  # a list of lists
      ["Joe", "painter", "single"],
      ["Jane", "writer", "divorced"],
      ["Mario", "plumber", "married"]
    ]
    
    for person_info in people:
      print "Name:", person_info[0], "job:", person_info[1], "is", person_info[2]
    
    9
    people = [  # a list of lists
      ["Joe", "painter", "single"],
      ["Jane", "writer", "divorced"],
      ["Mario", "plumber", "married"]
    ]
    
    for person_info in people:
      print "Name:", person_info[0], "job:", person_info[1], "is", person_info[2]
    
    8
    for name, job, marital_status in people:
      print "Person named", name, "works as a", job, "and is", marital_status
    
    1
    people = [  # a list of lists
      ["Joe", "painter", "single"],
      ["Jane", "writer", "divorced"],
      ["Mario", "plumber", "married"]
    ]
    
    for person_info in people:
      print "Name:", person_info[0], "job:", person_info[1], "is", person_info[2]
    
    8
    for name, job, marital_status in people:
      print "Person named", name, "works as a", job, "and is", marital_status
    
    3
    for name, job, marital_status in people:
      print "Person named", name, "works as a", job, "and is", marital_status
    
    4

    for name, job, marital_status in people:
      print "Person named", name, "works as a", job, "and is", marital_status
    
    5
    for name, job, marital_status in people:
      print "Person named", name, "works as a", job, "and is", marital_status
    
    6
    for name, job, marital_status in people:
      print "Person named", name, "works as a", job, "and is", marital_status
    
    7
    for name, job, marital_status in people:
      print "Person named", name, "works as a", job, "and is", marital_status
    
    8
    for name, job, marital_status in people:
      print "Person named", name, "works as a", job, "and is", marital_status
    
    9
    The original list is : ['Gfg best for', 'All geeks', 'It is for', 'all CS professionals']
    The Nth words in list are : ['best', 'geeks', 'is', 'CS']
    
    0

    The original list is : ['Gfg best for', 'All geeks', 'It is for', 'all CS professionals']
    The Nth words in list are : ['best', 'geeks', 'is', 'CS']
    
    1
    people = [  # a list of lists
      ["Joe", "painter", "single"],
      ["Jane", "writer", "divorced"],
      ["Mario", "plumber", "married"]
    ]
    
    for person_info in people:
      print "Name:", person_info[0], "job:", person_info[1], "is", person_info[2]
    
    5
    The original list is : ['Gfg best for', 'All geeks', 'It is for', 'all CS professionals']
    The Nth words in list are : ['best', 'geeks', 'is', 'CS']
    
    3

    The original list is : ['Gfg best for', 'All geeks', 'It is for', 'all CS professionals']
    The Nth words in list are : ['best', 'geeks', 'is', 'CS']
    
    4
    people = [  # a list of lists
      ["Joe", "painter", "single"],
      ["Jane", "writer", "divorced"],
      ["Mario", "plumber", "married"]
    ]
    
    for person_info in people:
      print "Name:", person_info[0], "job:", person_info[1], "is", person_info[2]
    
    5
    The original list is : ['Gfg best for', 'All geeks', 'It is for', 'all CS professionals']
    The Nth words in list are : ['best', 'geeks', 'is', 'CS']
    
    6
    The original list is : ['Gfg best for', 'All geeks', 'It is for', 'all CS professionals']
    The Nth words in list are : ['best', 'geeks', 'is', 'CS']
    
    7
    The original list is : ['Gfg best for', 'All geeks', 'It is for', 'all CS professionals']
    The Nth words in list are : ['best', 'geeks', 'is', 'CS']
    
    8
    The original list is : ['Gfg best for', 'All geeks', 'It is for', 'all CS professionals']
    The Nth words in list are : ['best', 'geeks', 'is', 'CS']
    
    9
    The original list is : ['Gfg best for', 'All geeks', 'It is for', 'all CS professionals']
    The Nth words in list are : ['best', 'geeks', 'is', 'CS']
    
    0

    The original list is : ['Gfg best for', 'All geeks', 'It is for', 'all CS professionals']
    The Nth words in list are : ['best', 'geeks', 'is', 'CS']
    


    Phương pháp 5: Sử dụng liệt kê ..

    Examples:  

    Input : 'Hello World'
    Output : ['Hello', 'world']

    Phương pháp 6: Sử dụng mảng numpy [].

    Làm thế nào để bạn trích xuất một chuỗi từ một danh sách trong Python?split[] method. This method split a string into a list where each word is a list item. We have alternative ways to use this function in order to achieve the required output.

    Python3

    Cho một ma trận, trích xuất tất cả các phần tử thuộc loại dữ liệu chuỗi. Đầu vào: test_list = [[5, 6, 3], ["gfg", 3], [9, "tốt nhất", 4]] đầu ra: ['gfg', 'tốt nhất'] Giải thích: Tất cả các chuỗi được trích xuất.

    for name, job, marital_status in people:
      print "Person named", name, "works as a", job, "and is", marital_status
    
    5
    people = [  # a list of lists
      ["Joe", "painter", "single"],
      ["Jane", "writer", "divorced"],
      ["Mario", "plumber", "married"]
    ]
    
    for person_info in people:
      print "Name:", person_info[0], "job:", person_info[1], "is", person_info[2]
    
    06

    Output:  

    ['Geeks', 'For', 'geeks']

    Phương pháp 2: Chia một câu thành một danh sách sử dụng cho Loop & NBSP;sing for loop 

    Chúng ta cũng có thể sử dụng một python cho vòng lặp để phân chia phần tử đầu tiên. Phương pháp này cũng có lợi nếu chúng ta có nhiều hơn một yếu tố. & Nbsp; & nbsp;

    Python3

    people = [  # a list of lists
      ["Joe", "painter", "single"],
      ["Jane", "writer", "divorced"],
      ["Mario", "plumber", "married"]
    ]
    
    for person_info in people:
      print "Name:", person_info[0], "job:", person_info[1], "is", person_info[2]
    
    07
    people = [  # a list of lists
      ["Joe", "painter", "single"],
      ["Jane", "writer", "divorced"],
      ["Mario", "plumber", "married"]
    ]
    
    for person_info in people:
      print "Name:", person_info[0], "job:", person_info[1], "is", person_info[2]
    
    08

    people = [  # a list of lists
      ["Joe", "painter", "single"],
      ["Jane", "writer", "divorced"],
      ["Mario", "plumber", "married"]
    ]
    
    for person_info in people:
      print "Name:", person_info[0], "job:", person_info[1], "is", person_info[2]
    
    09
    people = [  # a list of lists
      ["Joe", "painter", "single"],
      ["Jane", "writer", "divorced"],
      ["Mario", "plumber", "married"]
    ]
    
    for person_info in people:
      print "Name:", person_info[0], "job:", person_info[1], "is", person_info[2]
    
    10
    people = [  # a list of lists
      ["Joe", "painter", "single"],
      ["Jane", "writer", "divorced"],
      ["Mario", "plumber", "married"]
    ]
    
    for person_info in people:
      print "Name:", person_info[0], "job:", person_info[1], "is", person_info[2]
    
    11
    The original list is : ['Gfg best for', 'All geeks', 'It is for', 'all CS professionals']
    The Nth words in list are : ['best', 'geeks', 'is', 'CS']
    
    0
    people = [  # a list of lists
      ["Joe", "painter", "single"],
      ["Jane", "writer", "divorced"],
      ["Mario", "plumber", "married"]
    ]
    
    for person_info in people:
      print "Name:", person_info[0], "job:", person_info[1], "is", person_info[2]
    
    13
    The original list is : ['Gfg best for', 'All geeks', 'It is for', 'all CS professionals']
    The Nth words in list are : ['best', 'geeks', 'is', 'CS']
    
    2
    people = [  # a list of lists
      ["Joe", "painter", "single"],
      ["Jane", "writer", "divorced"],
      ["Mario", "plumber", "married"]
    ]
    
    for person_info in people:
      print "Name:", person_info[0], "job:", person_info[1], "is", person_info[2]
    
    15

    ________ 102 ________ 15 & nbsp;

    people = [  # a list of lists
      ["Joe", "painter", "single"],
      ["Jane", "writer", "divorced"],
      ["Mario", "plumber", "married"]
    ]
    
    for person_info in people:
      print "Name:", person_info[0], "job:", person_info[1], "is", person_info[2]
    
    18

    for name, job, marital_status in people:
      print "Person named", name, "works as a", job, "and is", marital_status
    
    5
    people = [  # a list of lists
      ["Joe", "painter", "single"],
      ["Jane", "writer", "divorced"],
      ["Mario", "plumber", "married"]
    ]
    
    for person_info in people:
      print "Name:", person_info[0], "job:", person_info[1], "is", person_info[2]
    
    20

    Output:  

    ['Geeksforgeeks', 'is', 'a', 'portal', 'for', 'geeks']

    Phương pháp 3: Chia một câu thành một danh sách bằng tham gia [] & nbsp;sing join[] 

    Chúng ta có thể phân chia danh sách đã cho và sau đó tham gia bằng hàm tham gia []. Chúng tôi cũng có thể sử dụng điều này khi bạn có một danh sách các chuỗi hoặc một chuỗi duy nhất trong danh sách. & Nbsp; & nbsp;

    Python3

    people = [  # a list of lists
      ["Joe", "painter", "single"],
      ["Jane", "writer", "divorced"],
      ["Mario", "plumber", "married"]
    ]
    
    for person_info in people:
      print "Name:", person_info[0], "job:", person_info[1], "is", person_info[2]
    
    07
    people = [  # a list of lists
      ["Joe", "painter", "single"],
      ["Jane", "writer", "divorced"],
      ["Mario", "plumber", "married"]
    ]
    
    for person_info in people:
      print "Name:", person_info[0], "job:", person_info[1], "is", person_info[2]
    
    08

    people = [  # a list of lists
      ["Joe", "painter", "single"],
      ["Jane", "writer", "divorced"],
      ["Mario", "plumber", "married"]
    ]
    
    for person_info in people:
      print "Name:", person_info[0], "job:", person_info[1], "is", person_info[2]
    
    09
    people = [  # a list of lists
      ["Joe", "painter", "single"],
      ["Jane", "writer", "divorced"],
      ["Mario", "plumber", "married"]
    ]
    
    for person_info in people:
      print "Name:", person_info[0], "job:", person_info[1], "is", person_info[2]
    
    10
    people = [  # a list of lists
      ["Joe", "painter", "single"],
      ["Jane", "writer", "divorced"],
      ["Mario", "plumber", "married"]
    ]
    
    for person_info in people:
      print "Name:", person_info[0], "job:", person_info[1], "is", person_info[2]
    
    11
    The original list is : ['Gfg best for', 'All geeks', 'It is for', 'all CS professionals']
    The Nth words in list are : ['best', 'geeks', 'is', 'CS']
    
    0
    people = [  # a list of lists
      ["Joe", "painter", "single"],
      ["Jane", "writer", "divorced"],
      ["Mario", "plumber", "married"]
    ]
    
    for person_info in people:
      print "Name:", person_info[0], "job:", person_info[1], "is", person_info[2]
    
    13
    The original list is : ['Gfg best for', 'All geeks', 'It is for', 'all CS professionals']
    The Nth words in list are : ['best', 'geeks', 'is', 'CS']
    
    2
    people = [  # a list of lists
      ["Joe", "painter", "single"],
      ["Jane", "writer", "divorced"],
      ["Mario", "plumber", "married"]
    ]
    
    for person_info in people:
      print "Name:", person_info[0], "job:", person_info[1], "is", person_info[2]
    
    15

    ________ 102 ________ 15 & nbsp;

    people = [  # a list of lists
      ["Joe", "painter", "single"],
      ["Jane", "writer", "divorced"],
      ["Mario", "plumber", "married"]
    ]
    
    for person_info in people:
      print "Name:", person_info[0], "job:", person_info[1], "is", person_info[2]
    
    18

    Phương pháp 3: Chia một câu thành một danh sách bằng tham gia [] & nbsp;

    Output:  

    ['Hello', 'Geeks', 'for', 'geeks']

    Chúng ta có thể phân chia danh sách đã cho và sau đó tham gia bằng hàm tham gia []. Chúng tôi cũng có thể sử dụng điều này khi bạn có một danh sách các chuỗi hoặc một chuỗi duy nhất trong danh sách. & Nbsp; & nbsp;

    people = [  # a list of lists
      ["Joe", "painter", "single"],
      ["Jane", "writer", "divorced"],
      ["Mario", "plumber", "married"]
    ]
    
    for person_info in people:
      print "Name:", person_info[0], "job:", person_info[1], "is", person_info[2]
    
    09
    people = [  # a list of lists
      ["Joe", "painter", "single"],
      ["Jane", "writer", "divorced"],
      ["Mario", "plumber", "married"]
    ]
    
    for person_info in people:
      print "Name:", person_info[0], "job:", person_info[1], "is", person_info[2]
    
    10
    people = [  # a list of lists
      ["Joe", "painter", "single"],
      ["Jane", "writer", "divorced"],
      ["Mario", "plumber", "married"]
    ]
    
    for person_info in people:
      print "Name:", person_info[0], "job:", person_info[1], "is", person_info[2]
    
    25

    Python3

    ________ 102 ________ 15 & nbsp;

    people = [  # a list of lists
      ["Joe", "painter", "single"],
      ["Jane", "writer", "divorced"],
      ["Mario", "plumber", "married"]
    ]
    
    for person_info in people:
      print "Name:", person_info[0], "job:", person_info[1], "is", person_info[2]
    
    28

    for name, job, marital_status in people:
      print "Person named", name, "works as a", job, "and is", marital_status
    
    5
    people = [  # a list of lists
      ["Joe", "painter", "single"],
      ["Jane", "writer", "divorced"],
      ["Mario", "plumber", "married"]
    ]
    
    for person_info in people:
      print "Name:", person_info[0], "job:", person_info[1], "is", person_info[2]
    
    20

    Phương pháp 4: Chia một câu thành danh sách bằng NLTK

    Đối với vấn đề cụ thể của chúng tôi, có thể sử dụng phương thức từ thư viện NLTK thư viện NLTK. Hàm này chia một chuỗi thành một số chuỗi con bằng cách lấy một chuỗi làm tham số đầu vào.

    for name, job, marital_status in people:
      print "Person named", name, "works as a", job, "and is", marital_status
    
    5
    people = [  # a list of lists
      ["Joe", "painter", "single"],
      ["Jane", "writer", "divorced"],
      ["Mario", "plumber", "married"]
    ]
    
    for person_info in people:
      print "Name:", person_info[0], "job:", person_info[1], "is", person_info[2]
    
    43

    Output:

    ['This', 'is', 'geeksforgeeks']

    Làm cách nào để trích xuất một từ từ một danh sách trong Python?

    Sử dụng các biểu thức chính quy để trích xuất bất kỳ từ cụ thể nào chúng ta có thể sử dụng phương thức search [] từ mô -đun RE để tìm sự xuất hiện đầu tiên của từ và sau đó chúng ta có thể lấy từ bằng cách cắt lát. Phương thức RE.Search [] sẽ lấy từ để được trích xuất ở dạng biểu thức thông thường và chuỗi làm đầu vào và trả về một RE.use search[] method from re module to find the first occurrence of the word and then we can obtain the word using slicing. re.search[] method will take the word to be extracted in regular expression form and the string as input and and returns a re.

    Làm cách nào để tìm một từ trong danh sách Python?

    Tìm chuỗi trong danh sách trong Python..
    Sử dụng vòng lặp For để tìm các phần tử từ danh sách có chứa một chuỗi con cụ thể trong Python ..
    Sử dụng hàm Filter [] để tìm các phần tử từ danh sách Python có chứa chuỗi con cụ thể ..
    Sử dụng các biểu thức thông thường để tìm các phần tử từ danh sách Python có chứa một chuỗi con cụ thể ..

    Làm cách nào để trích xuất một mục cụ thể từ một danh sách trong Python?

    6 cách dễ dàng để trích xuất các yếu tố từ danh sách Python..
    Xây dựng vấn đề và tổng quan về giải pháp ..
    Phương pháp 1: Sử dụng cắt ..
    Phương pháp 2: Sử dụng chỉ số danh sách ..
    Phương pháp 3: Sử dụng khả năng hiểu danh sách đơn giản ..
    Phương pháp 4: Sử dụng danh sách hiểu với điều kiện ..
    Phương pháp 5: Sử dụng liệt kê ..
    Phương pháp 6: Sử dụng mảng numpy [].
    Summary..

    Làm thế nào để bạn trích xuất một chuỗi từ một danh sách trong Python?

    Cho một ma trận, trích xuất tất cả các phần tử thuộc loại dữ liệu chuỗi.Đầu vào: test_list = [[5, 6, 3], ["gfg", 3], [9, "tốt nhất", 4]] đầu ra: ['gfg', 'tốt nhất'] Giải thích: Tất cả các chuỗi được trích xuất.. Input : test_list = [[5, 6, 3], ["Gfg", 3], [9, "best", 4]] Output : ['Gfg', 'best'] Explanation : All strings are extracted.

    Bài Viết Liên Quan

    Chủ Đề