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
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]
4people = [ # 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]
6people = [ # 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]
7people = [ # 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]
8people = [ # 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]
9people = [ # 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]
8for name, job, marital_status in people:
print "Person named", name, "works as a", job, "and is", marital_status
1people = [ # 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]
8for name, job, marital_status in people:
print "Person named", name, "works as a", job, "and is", marital_status
3for name, job, marital_status in people:
print "Person named", name, "works as a", job, "and is", marital_status
4for name, job, marital_status in people:
print "Person named", name, "works as a", job, "and is", marital_status
5for name, job, marital_status in people:
print "Person named", name, "works as a", job, "and is", marital_status
6for 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
9The 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
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]
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']
6The 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']
8The original list is : ['Gfg best for', 'All geeks', 'It is for', 'all CS professionals']
The Nth words in list are : ['best', 'geeks', 'is', 'CS']
9The 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
5for name, job, marital_status in people:
print "Person named", name, "works as a", job, "and is", marital_status
6Input : '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
9Input : '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]
4people = [ # 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]
6people = [ # 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]
7people = [ # 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]
8people = [ # 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]
9people = [ # 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]
8for name, job, marital_status in people:
print "Person named", name, "works as a", job, "and is", marital_status
1people = [ # 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]
8for name, job, marital_status in people:
print "Person named", name, "works as a", job, "and is", marital_status
3for name, job, marital_status in people:
print "Person named", name, "works as a", job, "and is", marital_status
4for name, job, marital_status in people:
print "Person named", name, "works as a", job, "and is", marital_status
5for name, job, marital_status in people:
print "Person named", name, "works as a", job, "and is", marital_status
6for 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
9The 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
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]
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']
6The 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']
8The original list is : ['Gfg best for', 'All geeks', 'It is for', 'all CS professionals']
The Nth words in list are : ['best', 'geeks', 'is', 'CS']
9The 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
5people = [ # 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]
06Output:
['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]
08people = [ # 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]
09people = [ # 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]
11The 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]
13The 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]
18for name, job, marital_status in people:
print "Person named", name, "works as a", job, "and is", marital_status
5people = [ # 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]
20Output:
['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]
08people = [ # 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]
09people = [ # 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]
11The 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]
13The 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]
18Phươ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]
09people = [ # 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]
25Python3
________ 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]
28for name, job, marital_status in people:
print "Person named", name, "works as a", job, "and is", marital_status
5people = [ # 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]
20Phươ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
5people = [ # 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]
43Output:
['This', 'is', 'geeksforgeeks']