Giả sử tôi có một danh sách có thể có một hoặc hai yếu tố:
mylist=["important", "comment"]
hoặc
mylist=["important"]
Sau đó, tôi muốn có một biến để làm việc như một cờ tùy thuộc vào giá trị thứ 2 này có hay không.
Cách tốt nhất để kiểm tra xem phần tử thứ 2 có tồn tại là gì?
Tôi đã làm nó bằng cách sử dụng
mylist=["important"]
9. Nếu nó là 2, nó là tốt. Nó hoạt động nhưng tôi muốn biết trường thứ 2 có chính xác là "bình luận" hay không.Sau đó tôi đã đến giải pháp này:
>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
Nhưng trông quá dài. Bạn có nghĩ rằng nó có thể được cải thiện? Tôi chắc chắn rằng nó có thể nhưng không thể quản lý để tìm ra một cách thích hợp từ tài liệu cấu trúc dữ liệu Python.
Hỏi ngày 6 tháng 8 năm 2013 lúc 15:21Aug 6, 2013 at 15:21
Fedorquifedorquifedorqui
262K99 Huy hiệu vàng529 Huy hiệu bạc582 Huy hiệu Đồng99 gold badges529 silver badges582 bronze badges
0
Bạn có thể sử dụng toán tử
>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
0:'comment' in mylist
Hoặc, nếu vị trí quan trọng, hãy sử dụng một lát:
mylist[1:] == ['comment']
Cái sau hoạt động cho các danh sách có kích thước một, hai hoặc dài hơn và chỉ là
>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
1 nếu danh sách có độ dài 2 và phần tử thứ hai bằng >>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
2:>>> test = lambda L: L[1:] == ['comment']
>>> test[['important']]
False
>>> test[['important', 'comment']]
True
>>> test[['important', 'comment', 'bar']]
False
Đã trả lời ngày 6 tháng 8 năm 2013 lúc 15:25Aug 6, 2013 at 15:25
Martijn Pieters ♦ Martijn Pieters♦Martijn Pieters
996K277 Huy hiệu vàng3920 Huy hiệu bạc3262 Huy hiệu Đồng277 gold badges3920 silver badges3262 bronze badges
0
Sử dụng toán tử
>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
0:>>> mylist=["important", "comment"]
>>> "comment" in mylist
True
Ah! Bỏ lỡ phần mà bạn đã nói, bạn chỉ muốn
>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
4 là yếu tố thứ 2. Vì điều đó bạn có thể sử dụng:len[mylist] == 2 and mylist[1] == "comment"
Đã trả lời ngày 6 tháng 8 năm 2013 lúc 15:23Aug 6, 2013 at 15:23
Rohit Jainrohit JainRohit Jain
206K45 Huy hiệu vàng401 Huy hiệu bạc516 Huy hiệu Đồng45 gold badges401 silver badges516 bronze badges
0
Thế còn:
len[mylist] == 2 and mylist[1] == "comment"
Ví dụ:
>>> mylist = ["important", "comment"]
>>> c = len[mylist] == 2 and mylist[1] == "comment"
>>> c
True
>>>
>>> mylist = ["important"]
>>> c = len[mylist] == 2 and mylist[1] == "comment"
>>> c
False
Đã trả lời ngày 6 tháng 8 năm 2013 lúc 15:24Aug 6, 2013 at 15:24
Arshajiiarshajiiarshajii
125K24 Huy hiệu vàng234 Huy hiệu bạc282 Huy hiệu Đồng24 gold badges234 silver badges282 bronze badges
0
mylist[1:] == ['comment']
3 >>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
96>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
97>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
98>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
6'comment' in mylist
00>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
8>>> test = lambda L: L[1:] == ['comment']
>>> test[['important']]
False
>>> test[['important', 'comment']]
True
>>> test[['important', 'comment', 'bar']]
False
3Example:
mylist=["important"]
0mylist=["important"]
1Đầu ra
Phương pháp 5: Sử dụng hàm bộ đếm []
Dưới đây là việc thực hiện: Check if an element exists in the list using the if-else statement
Python3
mylist=["important"]
88 'comment' in mylist
16mylist=["important"]
90 'comment' in mylist
18mylist[1:] == ['comment']
0>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
6mylist[1:] == ['comment']
2'comment' in mylist
34>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
6 'comment' in mylist
36mylist[1:] == ['comment']
7mylist[1:] == ['comment']
8mylist[1:] == ['comment']
9>>> test = lambda L: L[1:] == ['comment']
>>> test[['important']]
False
>>> test[['important', 'comment']]
True
>>> test[['important', 'comment', 'bar']]
False
0>>> test = lambda L: L[1:] == ['comment']
>>> test[['important']]
False
>>> test[['important', 'comment']]
True
>>> test[['important', 'comment', 'bar']]
False
1>>> test = lambda L: L[1:] == ['comment']
>>> test[['important']]
False
>>> test[['important', 'comment']]
True
>>> test[['important', 'comment', 'bar']]
False
2>>> test = lambda L: L[1:] == ['comment']
>>> test[['important']]
False
>>> test[['important', 'comment']]
True
>>> test[['important', 'comment', 'bar']]
False
3Danh sách này là một thùng chứa quan trọng trong Python vì nó lưu trữ các yếu tố của tất cả các loại dữ liệu dưới dạng bộ sưu tập. Kiến thức về các hoạt động danh sách nhất định là cần thiết cho lập trình hàng ngày. Bài viết này thảo luận về cách nhanh nhất để kiểm tra xem giá trị có tồn tại trong danh sách hay không sử dụng Python.
Phương pháp 1: Phương pháp ngây thơCheck if an element exists in the list using a loop
Python3
Trong phương pháp ngây thơ, người ta dễ dàng sử dụng một vòng lặp lặp qua tất cả các yếu tố để kiểm tra sự tồn tại của phần tử đích. Đây là cách đơn giản nhất để kiểm tra sự tồn tại của phần tử trong danh sách. Python là cách thông thường nhất để kiểm tra xem một yếu tố có tồn tại trong danh sách hay không. Cách cụ thể này trả về đúng nếu một phần tử tồn tại trong danh sách và sai nếu phần tử không tồn tại trong danh sách. Danh sách không cần phải được sắp xếp để thực hành phương pháp kiểm tra này.
Ví dụ 1: Kiểm tra xem một phần tử có tồn tại trong danh sách bằng cách sử dụng câu lệnh if-else không
Các
mylist[1:] == ['comment']
3 mylist[1:] == ['comment']
4>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
0 mylist[1:] == ['comment']
6Output:
mylist=["important"]
2
7mylist[1:] == ['comment']
8mylist[1:] == ['comment']
9mylist[1:] == ['comment']
7>>> test = lambda L: L[1:] == ['comment']
>>> test[['important']]
False
>>> test[['important', 'comment']]
True
>>> test[['important', 'comment', 'bar']]
False
1Check if an element exists in the list using “in” >>> test = lambda L: L[1:] == ['comment']
>>> test[['important']]
False
>>> test[['important', 'comment']]
True
>>> test[['important', 'comment', 'bar']]
False
Python3
Trong phương pháp ngây thơ, người ta dễ dàng sử dụng một vòng lặp lặp qua tất cả các yếu tố để kiểm tra sự tồn tại của phần tử đích. Đây là cách đơn giản nhất để kiểm tra sự tồn tại của phần tử trong danh sách. Python là cách thông thường nhất để kiểm tra xem một yếu tố có tồn tại trong danh sách hay không. Cách cụ thể này trả về đúng nếu một phần tử tồn tại trong danh sách và sai nếu phần tử không tồn tại trong danh sách. Danh sách không cần phải được sắp xếp để thực hành phương pháp kiểm tra này.
Ví dụ 1: Kiểm tra xem một phần tử có tồn tại trong danh sách bằng cách sử dụng câu lệnh if-else không
Các
Output:
mylist=["important"]
2
3 mylist[1:] == ['comment']
4mylist[1:] == ['comment']
0 >>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
6Check if an element exists in the list using any[] functionmylist[1:] == ['comment']
Python3
Trong phương pháp ngây thơ, người ta dễ dàng sử dụng một vòng lặp lặp qua tất cả các yếu tố để kiểm tra sự tồn tại của phần tử đích. Đây là cách đơn giản nhất để kiểm tra sự tồn tại của phần tử trong danh sách. Python là cách thông thường nhất để kiểm tra xem một yếu tố có tồn tại trong danh sách hay không. Cách cụ thể này trả về đúng nếu một phần tử tồn tại trong danh sách và sai nếu phần tử không tồn tại trong danh sách. Danh sách không cần phải được sắp xếp để thực hành phương pháp kiểm tra này.
Ví dụ 1: Kiểm tra xem một phần tử có tồn tại trong danh sách bằng cách sử dụng câu lệnh if-else không
Các
Output:
mylist=["important"]
4mylist[1:] == ['comment']
3 mylist[1:] == ['comment']
4>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
0 mylist[1:] == ['comment']
6count[]
mylist[1:] == ['comment']
mylist[1:] == ['comment']
>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
mylist[1:] == ['comment']
mylist[1:] == ['comment']
7mylist[1:] == ['comment']
8mylist[1:] == ['comment']
9>>> test = lambda L: L[1:] == ['comment']
>>> test[['important']]
False
>>> test[['important', 'comment']]
True
>>> test[['important', 'comment', 'bar']]
False
7>>> test = lambda L: L[1:] == ['comment']
>>> test[['important']]
False
>>> test[['important', 'comment']]
True
>>> test[['important', 'comment', 'bar']]
False
1Python3
>>> test = lambda L: L[1:] == ['comment']
>>> test[['important']]
False
>>> test[['important', 'comment']]
True
>>> test[['important', 'comment', 'bar']]
False
9>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
6 >>> mylist=["important", "comment"]
>>> "comment" in mylist
True
1mylist=["important"]
51>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
9mylist=["important"]
53>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
9mylist=["important"]
55>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
9mylist[1:] == ['comment']
2>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
9mylist=["important"]
59>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
9mylist=["important"]
61'comment' in mylist
9mylist[1:] == ['comment']
8mylist[1:] == ['comment']
9mylist=["important"]
65>>> test = lambda L: L[1:] == ['comment']
>>> test[['important']]
False
>>> test[['important', 'comment']]
True
>>> test[['important', 'comment', 'bar']]
False
1mylist=["important"]
67>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
6 mylist=["important"]
69mylist=["important"]
53>>> test = lambda L: L[1:] == ['comment']
>>> test[['important']]
False
>>> test[['important', 'comment']]
True
>>> test[['important', 'comment', 'bar']]
False
1mylist[1:] == ['comment']
3 mylist=["important"]
73mylist=["important"]
74>>> test = lambda L: L[1:] == ['comment']
>>> test[['important']]
False
>>> test[['important', 'comment']]
True
>>> test[['important', 'comment', 'bar']]
False
3mylist[1:] == ['comment']
7mylist[1:] == ['comment']
8mylist[1:] == ['comment']
9mylist=["important"]
79>>> test = lambda L: L[1:] == ['comment']
>>> test[['important']]
False
>>> test[['important', 'comment']]
True
>>> test[['important', 'comment', 'bar']]
False
1>>> test = lambda L: L[1:] == ['comment']
>>> test[['important']]
False
>>> test[['important', 'comment']]
True
>>> test[['important', 'comment', 'bar']]
False
2>>> test = lambda L: L[1:] == ['comment']
>>> test[['important']]
False
>>> test[['important', 'comment']]
True
>>> test[['important', 'comment', 'bar']]
False
3mylist[1:] == ['comment']
7mylist[1:] == ['comment']
8mylist[1:] == ['comment']
9mylist=["important"]
86>>> test = lambda L: L[1:] == ['comment']
>>> test[['important']]
False
>>> test[['important', 'comment']]
True
>>> test[['important', 'comment', 'bar']]
False
1Output:
mylist=["important"]
5Phương pháp 3: Kiểm tra xem một phần tử có tồn tại trong danh sách bằng cách sử dụng sort + bicect_left + set
Chuyển đổi danh sách thành tập hợp và sau đó sử dụng nó có thể hiệu quả hơn chỉ sử dụng nó. Nhưng có hiệu quả cho một điểm cộng cũng có những tiêu cực nhất định. Một trong số đó là thứ tự của danh sách không được bảo tồn và nếu bạn chọn lấy một danh sách mới cho nó, bạn sẽ yêu cầu sử dụng thêm không gian. Một nhược điểm khác là thiết lập không hoàn toàn trùng lặp và do đó các yếu tố trùng lặp sẽ bị xóa khỏi danh sách ban đầu. Trong cách tìm kiếm nhị phân thông thường về sự tồn tại của phần tử, do đó danh sách phải được sắp xếp trước và do đó không bảo tồn thứ tự phần tử. bisect_left [] trả về sự xuất hiện đầu tiên của phần tử được tìm thấy và đã hoạt động tương tự như Lower_bound [] trong C ++ STL.
LƯU Ý: Hàm Bisect sẽ chỉ nêu vị trí của nơi chèn phần tử nhưng không phải là chi tiết về việc có phần tử hay không.The bisect function will only state the position of where to insert the element but not the details about if the element is present or not.
Thể hiện để kiểm tra sự tồn tại của phần tử trong danh sách bằng set [] + in và sort [] + bisect_left []
Python3
mylist=["important"]
88 mylist=["important"]
89mylist=["important"]
90 mylist=["important"]
91mylist=["important"]
92>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
6 >>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
7>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
8>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
9____________>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
9'comment' in mylist
2>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
9'comment' in mylist
4>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
9'comment' in mylist
2>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
9'comment' in mylist
8 'comment' in mylist
9>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
07>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
6 >>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
7>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
8>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
9'comment' in mylist
0____29'comment' in mylist
2>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
9'comment' in mylist
4>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
9'comment' in mylist
2>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
9'comment' in mylist
8 'comment' in mylist
9mylist[1:] == ['comment']
8mylist[1:] == ['comment']
9>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
24>>> test = lambda L: L[1:] == ['comment']
>>> test[['important']]
False
>>> test[['important', 'comment']]
True
>>> test[['important', 'comment', 'bar']]
False
1mylist=["important"]
92>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
6 >>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
28>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
29mylist[1:] == ['comment']
3 'comment' in mylist
8 >>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
0 >>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
33mylist[1:] == ['comment']
7mylist[1:] == ['comment']
8 mylist[1:] == ['comment']
9len[mylist] == 2 and mylist[1] == "comment"
8>>> test = lambda L: L[1:] == ['comment']
>>> test[['important']]
False
>>> test[['important', 'comment']]
True
>>> test[['important', 'comment', 'bar']]
False
1mylist[1:] == ['comment']
8mylist[1:] == ['comment']
9>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
41>>> test = lambda L: L[1:] == ['comment']
>>> test[['important']]
False
>>> test[['important', 'comment']]
True
>>> test[['important', 'comment', 'bar']]
False
1>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
43mylist[1:] == ['comment']
3 >>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
45'comment' in mylist
8>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
47>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
6>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
49'comment' in mylist
8len[mylist] == 2 and mylist[1] == "comment"
4mylist[1:] == ['comment']
7mylist[1:] == ['comment']
8 mylist[1:] == ['comment']
9len[mylist] == 2 and mylist[1] == "comment"
8>>> test = lambda L: L[1:] == ['comment']
>>> test[['important']]
False
>>> test[['important', 'comment']]
True
>>> test[['important', 'comment', 'bar']]
False
1>>> test = lambda L: L[1:] == ['comment']
>>> test[['important']]
False
>>> test[['important', 'comment']]
True
>>> test[['important', 'comment', 'bar']]
False
2>>> test = lambda L: L[1:] == ['comment']
>>> test[['important']]
False
>>> test[['important', 'comment']]
True
>>> test[['important', 'comment', 'bar']]
False
3mylist[1:] == ['comment']
7mylist[1:] == ['comment']
8mylist[1:] == ['comment']
9>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
62>>> test = lambda L: L[1:] == ['comment']
>>> test[['important']]
False
>>> test[['important', 'comment']]
True
>>> test[['important', 'comment', 'bar']]
False
1Output:
mylist=["important"]
6mylist[1:] == ['comment']
8mylist[1:] == ['comment']
9>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
41>>> test = lambda L: L[1:] == ['comment']
>>> test[['important']]
False
>>> test[['important', 'comment']]
True
>>> test[['important', 'comment', 'bar']]
False
1
mylist[1:] == ['comment']
mylist[1:] == ['comment']
>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
>>> test = lambda L: L[1:] == ['comment']
>>> test[['important']]
False
>>> test[['important', 'comment']]
True
>>> test[['important', 'comment', 'bar']]
False
Python3
>>> test = lambda L: L[1:] == ['comment']
>>> test[['important']]
False
>>> test[['important', 'comment']]
True
>>> test[['important', 'comment', 'bar']]
False
9>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
6 >>> mylist=["important", "comment"]
>>> "comment" in mylist
True
1mylist=["important"]
51>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
9mylist=["important"]
53>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
9mylist=["important"]
55>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
9mylist[1:] == ['comment']
2>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
9mylist=["important"]
59>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
9mylist=["important"]
61'comment' in mylist
9mylist[1:] == ['comment']
3 >>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
45'comment' in mylist
8>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
47>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
6>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
49'comment' in mylist
8len[mylist] == 2 and mylist[1] == "comment"
4>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
83>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
6>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
85mylist[1:] == ['comment']
9>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
87mylist[1:] == ['comment']
9mylist=["important"]
44>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
90>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
91>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
6>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
93>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
94Phương pháp 4: Sử dụng phương thức Find []
mylist[1:] == ['comment']
7mylist[1:] == ['comment']
8mylist[1:] == ['comment']
9mylist=["important"]
79>>> test = lambda L: L[1:] == ['comment']
>>> test[['important']]
False
>>> test[['important', 'comment']]
True
>>> test[['important', 'comment', 'bar']]
False
1>>> test = lambda L: L[1:] == ['comment']
>>> test[['important']]
False
>>> test[['important', 'comment']]
True
>>> test[['important', 'comment', 'bar']]
False
2>>> test = lambda L: L[1:] == ['comment']
>>> test[['important']]
False
>>> test[['important', 'comment']]
True
>>> test[['important', 'comment', 'bar']]
False
3mylist[1:] == ['comment']
7mylist[1:] == ['comment']
8mylist[1:] == ['comment']
9mylist=["important"]
86>>> test = lambda L: L[1:] == ['comment']
>>> test[['important']]
False
>>> test[['important', 'comment']]
True
>>> test[['important', 'comment', 'bar']]
False
1
8mylist[1:] == ['comment']
9mylist[1:] == ['comment']
65mylist=["important"]
1>>> test = lambda L: L[1:] == ['comment']
>>> test[['important']]
False
>>> test[['important', 'comment']]
True
>>> test[['important', 'comment', 'bar']]
False
mylist=["important"]
5mylist[1:] == ['comment']
3 >>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
96>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
97>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
98>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
6'comment' in mylist
00>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
8>>> test = lambda L: L[1:] == ['comment']
>>> test[['important']]
False
>>> test[['important', 'comment']]
True
>>> test[['important', 'comment', 'bar']]
False
3
mylist[1:] == ['comment']
>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
'comment' in mylist
>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
>>> test = lambda L: L[1:] == ['comment']
>>> test[['important']]
False
>>> test[['important', 'comment']]
True
>>> test[['important', 'comment', 'bar']]
False
Đầu ra
Python3
Phương pháp 5: Sử dụng hàm bộ đếm []
>>> test = lambda L: L[1:] == ['comment']
>>> test[['important']]
False
>>> test[['important', 'comment']]
True
>>> test[['important', 'comment', 'bar']]
False
9>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
6 >>> mylist=["important", "comment"]
>>> "comment" in mylist
True
1mylist=["important"]
51>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
9mylist=["important"]
53>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
9mylist=["important"]
55>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
9mylist[1:] == ['comment']
2>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
9mylist=["important"]
59>>> try:
... c=a.index["comment"]
... except ValueError:
... print "no such value"
...
>>> if c:
... print "yeah"
...
yeah
9mylist=["important"]
61'comment' in mylist
9Dưới đây là việc thực hiện:
mylist[1:] == ['comment']
3'comment' in mylist
38mylist=["important"]
53'comment' in mylist
40mylist=["important"]
74len[mylist] == 2 and mylist[1] == "comment"
4mylist[1:] == ['comment']
7mylist[1:] == ['comment']
8mylist[1:] == ['comment']
9mylist=["important"]
79>>> test = lambda L: L[1:] == ['comment']
>>> test[['important']]
False
>>> test[['important', 'comment']]
True
>>> test[['important', 'comment', 'bar']]
False
1>>> test = lambda L: L[1:] == ['comment']
>>> test[['important']]
False
>>> test[['important', 'comment']]
True
>>> test[['important', 'comment', 'bar']]
False
2>>> test = lambda L: L[1:] == ['comment']
>>> test[['important']]
False
>>> test[['important', 'comment']]
True
>>> test[['important', 'comment', 'bar']]
False
3mylist[1:] == ['comment']
7mylist[1:] == ['comment']
8mylist[1:] == ['comment']
9mylist=["important"]
86>>> test = lambda L: L[1:] == ['comment']
>>> test[['important']]
False
>>> test[['important', 'comment']]
True
>>> test[['important', 'comment', 'bar']]
False
1
8mylist[1:] == ['comment']
9mylist[1:] == ['comment']
65mylist=["important"]
1>>> test = lambda L: L[1:] == ['comment']
>>> test[['important']]
False
>>> test[['important', 'comment']]
True
>>> test[['important', 'comment', 'bar']]
False
mylist=["important"]
8