Hướng dẫn python check if key exists in list - python kiểm tra xem khóa có tồn tại trong danh sách không

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 PietersMartijn 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
3

Example:

mylist=["important"]
0
mylist=["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
16
mylist=["important"]
90
'comment' in mylist
18

mylist[1:] == ['comment']
0
>>> try:
...      c=a.index["comment"]
... except ValueError:
...      print "no such value"
... 
>>> if c:
...   print "yeah"
... 
yeah
6
mylist[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
36

mylist[1:] == ['comment']
7
mylist[1:] == ['comment']
8
mylist[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
3

Danh 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']
6

Output:

mylist=["important"]
2

mylist[1:] == ['comment']
7
mylist[1:] == ['comment']
8
mylist[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
1
Check if an element exists in the list using “in” 

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

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']
6
Check if an element exists in the list using any[] function

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"]
4

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']
6
count[]

mylist[1:] == ['comment']
7
mylist[1:] == ['comment']
8
mylist[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
1

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
1
mylist=["important"]
51
>>> try:
...      c=a.index["comment"]
... except ValueError:
...      print "no such value"
... 
>>> if c:
...   print "yeah"
... 
yeah
9
mylist=["important"]
53
>>> try:
...      c=a.index["comment"]
... except ValueError:
...      print "no such value"
... 
>>> if c:
...   print "yeah"
... 
yeah
9
mylist=["important"]
55
>>> try:
...      c=a.index["comment"]
... except ValueError:
...      print "no such value"
... 
>>> if c:
...   print "yeah"
... 
yeah
9
mylist[1:] == ['comment']
2
>>> try:
...      c=a.index["comment"]
... except ValueError:
...      print "no such value"
... 
>>> if c:
...   print "yeah"
... 
yeah
9
mylist=["important"]
59
>>> try:
...      c=a.index["comment"]
... except ValueError:
...      print "no such value"
... 
>>> if c:
...   print "yeah"
... 
yeah
9
mylist=["important"]
61
'comment' in mylist
9

mylist[1:] == ['comment']
8
mylist[1:] == ['comment']
9
mylist=["important"]
65
>>> test = lambda L: L[1:] == ['comment']
>>> test[['important']]
False
>>> test[['important', 'comment']]
True
>>> test[['important', 'comment', 'bar']]
False
1

mylist=["important"]
67
>>> try:
...      c=a.index["comment"]
... except ValueError:
...      print "no such value"
... 
>>> if c:
...   print "yeah"
... 
yeah
6
mylist=["important"]
69
mylist=["important"]
53
>>> test = lambda L: L[1:] == ['comment']
>>> test[['important']]
False
>>> test[['important', 'comment']]
True
>>> test[['important', 'comment', 'bar']]
False
1

mylist[1:] == ['comment']
3
mylist=["important"]
73
mylist=["important"]
74
>>> test = lambda L: L[1:] == ['comment']
>>> test[['important']]
False
>>> test[['important', 'comment']]
True
>>> test[['important', 'comment', 'bar']]
False
3

mylist[1:] == ['comment']
7
mylist[1:] == ['comment']
8
mylist[1:] == ['comment']
9
mylist=["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
3

mylist[1:] == ['comment']
7
mylist[1:] == ['comment']
8
mylist[1:] == ['comment']
9
mylist=["important"]
86
>>> test = lambda L: L[1:] == ['comment']
>>> test[['important']]
False
>>> test[['important', 'comment']]
True
>>> test[['important', 'comment', 'bar']]
False
1

Output:

mylist=["important"]
5

Phươ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"]
89
mylist=["important"]
90
mylist=["important"]
91

mylist=["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
9

mylist[1:] == ['comment']
8
mylist[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
1

mylist=["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
29

mylist[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
33

mylist[1:] == ['comment']
7
mylist[1:] == ['comment']
8
mylist[1:] == ['comment']
9
len[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

mylist[1:] == ['comment']
8
mylist[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
43

mylist[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
8
len[mylist] == 2 and mylist[1] == "comment"
4

mylist[1:] == ['comment']
7
mylist[1:] == ['comment']
8
mylist[1:] == ['comment']
9
len[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
3

mylist[1:] == ['comment']
7
mylist[1:] == ['comment']
8
mylist[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
1

Output:

mylist=["important"]
6

mylist[1:] == ['comment']
8
mylist[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

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
1
mylist=["important"]
51
>>> try:
...      c=a.index["comment"]
... except ValueError:
...      print "no such value"
... 
>>> if c:
...   print "yeah"
... 
yeah
9
mylist=["important"]
53
>>> try:
...      c=a.index["comment"]
... except ValueError:
...      print "no such value"
... 
>>> if c:
...   print "yeah"
... 
yeah
9
mylist=["important"]
55
>>> try:
...      c=a.index["comment"]
... except ValueError:
...      print "no such value"
... 
>>> if c:
...   print "yeah"
... 
yeah
9
mylist[1:] == ['comment']
2
>>> try:
...      c=a.index["comment"]
... except ValueError:
...      print "no such value"
... 
>>> if c:
...   print "yeah"
... 
yeah
9
mylist=["important"]
59
>>> try:
...      c=a.index["comment"]
... except ValueError:
...      print "no such value"
... 
>>> if c:
...   print "yeah"
... 
yeah
9
mylist=["important"]
61
'comment' in mylist
9

mylist[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
8
len[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
85
mylist[1:] == ['comment']
9
>>> try:
...      c=a.index["comment"]
... except ValueError:
...      print "no such value"
... 
>>> if c:
...   print "yeah"
... 
yeah
87
mylist[1:] == ['comment']
9
mylist=["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
94

Phương pháp 4: Sử dụng phương thức Find []

mylist[1:] == ['comment']
7
mylist[1:] == ['comment']
8
mylist[1:] == ['comment']
9
mylist=["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
3

mylist[1:] == ['comment']
7
mylist[1:] == ['comment']
8
mylist[1:] == ['comment']
9
mylist=["important"]
86
>>> test = lambda L: L[1:] == ['comment']
>>> test[['important']]
False
>>> test[['important', 'comment']]
True
>>> test[['important', 'comment', 'bar']]
False
1

mylist[1:] == ['comment']
8
mylist[1:] == ['comment']
9
mylist=["important"]
65
>>> test = lambda L: L[1:] == ['comment']
>>> test[['important']]
False
>>> test[['important', 'comment']]
True
>>> test[['important', 'comment', 'bar']]
False
1

mylist=["important"]
5

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
3

Đầ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
1
mylist=["important"]
51
>>> try:
...      c=a.index["comment"]
... except ValueError:
...      print "no such value"
... 
>>> if c:
...   print "yeah"
... 
yeah
9
mylist=["important"]
53
>>> try:
...      c=a.index["comment"]
... except ValueError:
...      print "no such value"
... 
>>> if c:
...   print "yeah"
... 
yeah
9
mylist=["important"]
55
>>> try:
...      c=a.index["comment"]
... except ValueError:
...      print "no such value"
... 
>>> if c:
...   print "yeah"
... 
yeah
9
mylist[1:] == ['comment']
2
>>> try:
...      c=a.index["comment"]
... except ValueError:
...      print "no such value"
... 
>>> if c:
...   print "yeah"
... 
yeah
9
mylist=["important"]
59
>>> try:
...      c=a.index["comment"]
... except ValueError:
...      print "no such value"
... 
>>> if c:
...   print "yeah"
... 
yeah
9
mylist=["important"]
61
'comment' in mylist
9

Dưới đây là việc thực hiện:

mylist[1:] == ['comment']
3
'comment' in mylist
38
mylist=["important"]
53
'comment' in mylist
40
mylist=["important"]
74
len[mylist] == 2 and mylist[1] == "comment"
4

mylist[1:] == ['comment']
7
mylist[1:] == ['comment']
8
mylist[1:] == ['comment']
9
mylist=["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
3

mylist[1:] == ['comment']
7
mylist[1:] == ['comment']
8
mylist[1:] == ['comment']
9
mylist=["important"]
86
>>> test = lambda L: L[1:] == ['comment']
>>> test[['important']]
False
>>> test[['important', 'comment']]
True
>>> test[['important', 'comment', 'bar']]
False
1

mylist[1:] == ['comment']
8
mylist[1:] == ['comment']
9
mylist=["important"]
65
>>> test = lambda L: L[1:] == ['comment']
>>> test[['important']]
False
>>> test[['important', 'comment']]
True
>>> test[['important', 'comment', 'bar']]
False
1

mylist=["important"]
8


Bài Viết Liên Quan

Chủ Đề