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

Kiểm tra bảng điều khiển đơn giản:

>>> if len[b] == 0: print "Ups!"
... 
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'b' is not defined
>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined

Ví dụ cho thấy cách kiểm tra xem danh sách có các yếu tố không:

alist = [1,2,3]
if alist: print "I'm here!"

Output: I'm here!

Otherwise:

alist = []
if not alist: print "Somebody here?"

Output: Somebody here?

Nếu bạn cần kiểm tra sự tồn tại/không tồn tại của danh sách/tuple, có thể điều này có thể giúp đỡ:

from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]

# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]

if "a_list" in globals[] and type[a_list] is ListType: 
    print "I'm a list, therefore I am an existing list! :]"

# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType: 
    print "I'm a tuple, therefore I am an existing tuple! :]"

Nếu chúng ta cần tránh trong Globals [] có lẽ chúng ta có thể sử dụng điều này:in globals[] maybe we can use this:

from types import ListType, TupleType
try:
    # for an existing/nonexisting list
    if type[ima_list] is ListType: 
        print "I'm a list, therefore I am an existing list! :]"

    # for an existing/nonexisting tuple
    if type[ima_tuple] is TupleType: 
        print "I'm a tuple, therefore I am an existing tuple! :]"
except Exception, e:
    print "%s" % e

Output:
    name 'ima_list' is not defined
    ---
    name 'ima_tuple' is not defined

Tài liệu tham khảo: 8.15. Các loại-Tên cho các loại tích hợp-Python v2.7.3 Tài liệu //docs.python.org/3/l Library/types.html 8.15. types — Names for built-in types — Python v2.7.3 documentation //docs.python.org/3/library/types.html

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.

Example:

list = test_list = [1, 6, 3, 5, 3, 4]
Input: 3  # Check if 3 exist or not.
Output: True
Input: 7  # Check if 7 exist or not.
Output: False

Phương pháp 1: Phương pháp ngây thơ

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 Check if an element exists in the list using the if-else statement

Python3

Các

alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
0
>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
6
alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
2

alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
3
alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
4
alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
5
alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
6

alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
7
alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
8
alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
9
from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]

# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]

if "a_list" in globals[] and type[a_list] is ListType: 
    print "I'm a list, therefore I am an existing list! :]"

# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType: 
    print "I'm a tuple, therefore I am an existing tuple! :]"
0
from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]

# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]

if "a_list" in globals[] and type[a_list] is ListType: 
    print "I'm a list, therefore I am an existing list! :]"

# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType: 
    print "I'm a tuple, therefore I am an existing tuple! :]"
1

from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]

# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]

if "a_list" in globals[] and type[a_list] is ListType: 
    print "I'm a list, therefore I am an existing list! :]"

# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType: 
    print "I'm a tuple, therefore I am an existing tuple! :]"
2
from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]

# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]

if "a_list" in globals[] and type[a_list] is ListType: 
    print "I'm a list, therefore I am an existing list! :]"

# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType: 
    print "I'm a tuple, therefore I am an existing tuple! :]"
3

alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
7
alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
8
alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
9
from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]

# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]

if "a_list" in globals[] and type[a_list] is ListType: 
    print "I'm a list, therefore I am an existing list! :]"

# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType: 
    print "I'm a tuple, therefore I am an existing tuple! :]"
7
from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]

# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]

if "a_list" in globals[] and type[a_list] is ListType: 
    print "I'm a list, therefore I am an existing list! :]"

# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType: 
    print "I'm a tuple, therefore I am an existing tuple! :]"
1

Ví dụ 2: Kiểm tra xem một phần tử có tồn tại trong danh sách bằng Loop & NBSP hay không;Check if an element exists in the list using a loop 

Python3

from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]

# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]

if "a_list" in globals[] and type[a_list] is ListType: 
    print "I'm a list, therefore I am an existing list! :]"

# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType: 
    print "I'm a tuple, therefore I am an existing tuple! :]"
9
>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
6
from types import ListType, TupleType
try:
    # for an existing/nonexisting list
    if type[ima_list] is ListType: 
        print "I'm a list, therefore I am an existing list! :]"

    # for an existing/nonexisting tuple
    if type[ima_tuple] is TupleType: 
        print "I'm a tuple, therefore I am an existing tuple! :]"
except Exception, e:
    print "%s" % e

Output:
    name 'ima_list' is not defined
    ---
    name 'ima_tuple' is not defined
1
>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
8
>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
9
alist = [1,2,3]
if alist: print "I'm here!"

Output: I'm here!
0
>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
9__22222219

list = test_list = [1, 6, 3, 5, 3, 4]
Input: 3  # Check if 3 exist or not.
Output: True
4
alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
4
alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
5
list = test_list = [1, 6, 3, 5, 3, 4]
Input: 3  # Check if 3 exist or not.
Output: True
7

alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
7
alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
3
Input: 7  # Check if 7 exist or not.
Output: False
0
>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
6
>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
6
alist = [1,2,3]
if alist: print "I'm here!"

Output: I'm here!
8
Input: 7  # Check if 7 exist or not.
Output: False
4

Output:

Element Exists

Ví dụ 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 trên mạng trong & NBSP hay không;Check if an element exists in the list using “in” 

Python3

from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]

# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]

if "a_list" in globals[] and type[a_list] is ListType: 
    print "I'm a list, therefore I am an existing list! :]"

# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType: 
    print "I'm a tuple, therefore I am an existing tuple! :]"
9
>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
6
from types import ListType, TupleType
try:
    # for an existing/nonexisting list
    if type[ima_list] is ListType: 
        print "I'm a list, therefore I am an existing list! :]"

    # for an existing/nonexisting tuple
    if type[ima_tuple] is TupleType: 
        print "I'm a tuple, therefore I am an existing tuple! :]"
except Exception, e:
    print "%s" % e

Output:
    name 'ima_list' is not defined
    ---
    name 'ima_tuple' is not defined
1
>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
8
>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
9
alist = [1,2,3]
if alist: print "I'm here!"

Output: I'm here!
0
>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
9__22222219

list = test_list = [1, 6, 3, 5, 3, 4]
Input: 3  # Check if 3 exist or not.
Output: True
4
alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
4
alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
5
list = test_list = [1, 6, 3, 5, 3, 4]
Input: 3  # Check if 3 exist or not.
Output: True
7

alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
7
alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
3
Input: 7  # Check if 7 exist or not.
Output: False
0
>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
6
>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
6
alist = [1,2,3]
if alist: print "I'm here!"

Output: I'm here!
8
Input: 7  # Check if 7 exist or not.
Output: False
4

Output:

Element Exists

Check if an element exists in the list using any[] function

Python3

from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]

# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]

if "a_list" in globals[] and type[a_list] is ListType: 
    print "I'm a list, therefore I am an existing list! :]"

# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType: 
    print "I'm a tuple, therefore I am an existing tuple! :]"
9
>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
6
from types import ListType, TupleType
try:
    # for an existing/nonexisting list
    if type[ima_list] is ListType: 
        print "I'm a list, therefore I am an existing list! :]"

    # for an existing/nonexisting tuple
    if type[ima_tuple] is TupleType: 
        print "I'm a tuple, therefore I am an existing tuple! :]"
except Exception, e:
    print "%s" % e

Output:
    name 'ima_list' is not defined
    ---
    name 'ima_tuple' is not defined
1
>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
8
>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
9
alist = [1,2,3]
if alist: print "I'm here!"

Output: I'm here!
0
>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
9__22222219

list = test_list = [1, 6, 3, 5, 3, 4]
Input: 3  # Check if 3 exist or not.
Output: True
4
alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
4
alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
5
list = test_list = [1, 6, 3, 5, 3, 4]
Input: 3  # Check if 3 exist or not.
Output: True
7

alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
7
alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
3
Input: 7  # Check if 7 exist or not.
Output: False
0
>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
6
>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
6
alist = [1,2,3]
if alist: print "I'm here!"

Output: I'm here!
8
Input: 7  # Check if 7 exist or not.
Output: False
4

Output:

>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
0

count[]

Ví dụ 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 trên mạng trong & NBSP hay không;

Python3

alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
3
alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
9
alist = [1,2,3]
if alist: print "I'm here!"

Output: I'm here!
8
alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
5
Element Exists
9

alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
8
alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
9
>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
55
from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]

# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]

if "a_list" in globals[] and type[a_list] is ListType: 
    print "I'm a list, therefore I am an existing list! :]"

# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType: 
    print "I'm a tuple, therefore I am an existing tuple! :]"
1

alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
7
alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
8
alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
9
Input: 7  # Check if 7 exist or not.
Output: False
8
from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]

# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]

if "a_list" in globals[] and type[a_list] is ListType: 
    print "I'm a list, therefore I am an existing list! :]"

# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType: 
    print "I'm a tuple, therefore I am an existing tuple! :]"
1

Ví dụ 4: 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 hàm bất kỳ [] nào không

alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
7
alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
8
alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
9
>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
69
from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]

# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]

if "a_list" in globals[] and type[a_list] is ListType: 
    print "I'm a list, therefore I am an existing list! :]"

# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType: 
    print "I'm a tuple, therefore I am an existing tuple! :]"
1

from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]

# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]

if "a_list" in globals[] and type[a_list] is ListType: 
    print "I'm a list, therefore I am an existing list! :]"

# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType: 
    print "I'm a tuple, therefore I am an existing tuple! :]"
2
from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]

# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]

if "a_list" in globals[] and type[a_list] is ListType: 
    print "I'm a list, therefore I am an existing list! :]"

# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType: 
    print "I'm a tuple, therefore I am an existing tuple! :]"
3

alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
7
alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
8
alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
9
>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
76
from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]

# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]

if "a_list" in globals[] and type[a_list] is ListType: 
    print "I'm a list, therefore I am an existing list! :]"

# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType: 
    print "I'm a tuple, therefore I am an existing tuple! :]"
1

Output:

>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
1

>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
20
>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
6
>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
222
>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
23
alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
5

alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
8
alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
9
>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
32
>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
33
>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
34
alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
9
>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
36
>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
37

Phương pháp 2: 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 Count []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.

Chúng ta có thể sử dụng phương thức danh sách Python được xây dựng, Count [], để kiểm tra xem phần tử được truyền có tồn tại trong danh sách không. Nếu phần tử được truyền tồn tại trong danh sách, phương thức đếm [] sẽ hiển thị số lần nó xảy ra trong toàn bộ danh sách. Nếu đó là một số dương khác không, nó có nghĩa là một yếu tố tồn tại trong danh sách. Thể hiện để kiểm tra sự tồn tại của các yếu tố trong danh sách bằng cách sử dụng Count [].

Python3

from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]

# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]

if "a_list" in globals[] and type[a_list] is ListType: 
    print "I'm a list, therefore I am an existing list! :]"

# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType: 
    print "I'm a tuple, therefore I am an existing tuple! :]"
9
>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
6
from types import ListType, TupleType
try:
    # for an existing/nonexisting list
    if type[ima_list] is ListType: 
        print "I'm a list, therefore I am an existing list! :]"

    # for an existing/nonexisting tuple
    if type[ima_tuple] is TupleType: 
        print "I'm a tuple, therefore I am an existing tuple! :]"
except Exception, e:
    print "%s" % e

Output:
    name 'ima_list' is not defined
    ---
    name 'ima_tuple' is not defined
1
>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
41
>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
9
>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
43__

>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
57
>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
6
>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
59
>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
43
from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]

# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]

if "a_list" in globals[] and type[a_list] is ListType: 
    print "I'm a list, therefore I am an existing list! :]"

# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType: 
    print "I'm a tuple, therefore I am an existing tuple! :]"
1

>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
97
>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
6
>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
7
>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
8
>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
9
alist = [1,2,3]
if alist: print "I'm here!"

Output: I'm here!
0
>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
9
alist = [1,2,3]
if alist: print "I'm here!"

Output: I'm here!
222

alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
8
alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
9
alist = [1,2,3]
if alist: print "I'm here!"

Output: I'm here!
14
from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]

# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]

if "a_list" in globals[] and type[a_list] is ListType: 
    print "I'm a list, therefore I am an existing list! :]"

# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType: 
    print "I'm a tuple, therefore I am an existing tuple! :]"
1

>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
82
>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
6
alist = [1,2,3]
if alist: print "I'm here!"

Output: I'm here!
18
alist = [1,2,3]
if alist: print "I'm here!"

Output: I'm here!
19

alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
3
alist = [1,2,3]
if alist: print "I'm here!"

Output: I'm here!
8
alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
5
alist = [1,2,3]
if alist: print "I'm here!"

Output: I'm here!
23

alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
7
alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
8
alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
9
Input: 7  # Check if 7 exist or not.
Output: False
8
from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]

# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]

if "a_list" in globals[] and type[a_list] is ListType: 
    print "I'm a list, therefore I am an existing list! :]"

# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType: 
    print "I'm a tuple, therefore I am an existing tuple! :]"
1

alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
8
alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
9
alist = [1,2,3]
if alist: print "I'm here!"

Output: I'm here!
31
from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]

# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]

if "a_list" in globals[] and type[a_list] is ListType: 
    print "I'm a list, therefore I am an existing list! :]"

# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType: 
    print "I'm a tuple, therefore I am an existing tuple! :]"
1

alist = [1,2,3]
if alist: print "I'm here!"

Output: I'm here!
33

alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
3
alist = [1,2,3]
if alist: print "I'm here!"

Output: I'm here!
35
alist = [1,2,3]
if alist: print "I'm here!"

Output: I'm here!
8
alist = [1,2,3]
if alist: print "I'm here!"

Output: I'm here!
37
>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
6
alist = [1,2,3]
if alist: print "I'm here!"

Output: I'm here!
39
alist = [1,2,3]
if alist: print "I'm here!"

Output: I'm here!
8
Input: 7  # Check if 7 exist or not.
Output: False
4

alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
7
alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
8
alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
9
Input: 7  # Check if 7 exist or not.
Output: False
8
from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]

# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]

if "a_list" in globals[] and type[a_list] is ListType: 
    print "I'm a list, therefore I am an existing list! :]"

# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType: 
    print "I'm a tuple, therefore I am an existing tuple! :]"
1

from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]

# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]

if "a_list" in globals[] and type[a_list] is ListType: 
    print "I'm a list, therefore I am an existing list! :]"

# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType: 
    print "I'm a tuple, therefore I am an existing tuple! :]"
2
from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]

# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]

if "a_list" in globals[] and type[a_list] is ListType: 
    print "I'm a list, therefore I am an existing list! :]"

# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType: 
    print "I'm a tuple, therefore I am an existing tuple! :]"
3

alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
7
alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
8
alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
9
alist = [1,2,3]
if alist: print "I'm here!"

Output: I'm here!
52
from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]

# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]

if "a_list" in globals[] and type[a_list] is ListType: 
    print "I'm a list, therefore I am an existing list! :]"

# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType: 
    print "I'm a tuple, therefore I am an existing tuple! :]"
1

Output:

>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
2

alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
8
alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
9
alist = [1,2,3]
if alist: print "I'm here!"

Output: I'm here!
31
from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]

# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]

if "a_list" in globals[] and type[a_list] is ListType: 
    print "I'm a list, therefore I am an existing list! :]"

# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType: 
    print "I'm a tuple, therefore I am an existing tuple! :]"
1

Python3

alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
3
alist = [1,2,3]
if alist: print "I'm here!"

Output: I'm here!
35
alist = [1,2,3]
if alist: print "I'm here!"

Output: I'm here!
8
alist = [1,2,3]
if alist: print "I'm here!"

Output: I'm here!
37
>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
6
alist = [1,2,3]
if alist: print "I'm here!"

Output: I'm here!
39
alist = [1,2,3]
if alist: print "I'm here!"

Output: I'm here!
8
Input: 7  # Check if 7 exist or not.
Output: False
4

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

alist = [1,2,3]
if alist: print "I'm here!"

Output: I'm here!
73
>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
6
alist = [1,2,3]
if alist: print "I'm here!"

Output: I'm here!
75
alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
9
alist = [1,2,3]
if alist: print "I'm here!"

Output: I'm here!
77
alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
9
>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
34
alist = [1,2,3]
if alist: print "I'm here!"

Output: I'm here!
80

alist = [1,2,3]
if alist: print "I'm here!"

Output: I'm here!
81
>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
6
alist = [1,2,3]
if alist: print "I'm here!"

Output: I'm here!
83
alist = [1,2,3]
if alist: print "I'm here!"

Output: I'm here!
84

from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]

# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]

if "a_list" in globals[] and type[a_list] is ListType: 
    print "I'm a list, therefore I am an existing list! :]"

# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType: 
    print "I'm a tuple, therefore I am an existing tuple! :]"
9
>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
6
from types import ListType, TupleType
try:
    # for an existing/nonexisting list
    if type[ima_list] is ListType: 
        print "I'm a list, therefore I am an existing list! :]"

    # for an existing/nonexisting tuple
    if type[ima_tuple] is TupleType: 
        print "I'm a tuple, therefore I am an existing tuple! :]"
except Exception, e:
    print "%s" % e

Output:
    name 'ima_list' is not defined
    ---
    name 'ima_tuple' is not defined
1
>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
41
>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
9
>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
43__

alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
7
alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
8
alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
9
>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
69
from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]

# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]

if "a_list" in globals[] and type[a_list] is ListType: 
    print "I'm a list, therefore I am an existing list! :]"

# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType: 
    print "I'm a tuple, therefore I am an existing tuple! :]"
1

from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]

# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]

if "a_list" in globals[] and type[a_list] is ListType: 
    print "I'm a list, therefore I am an existing list! :]"

# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType: 
    print "I'm a tuple, therefore I am an existing tuple! :]"
2
from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]

# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]

if "a_list" in globals[] and type[a_list] is ListType: 
    print "I'm a list, therefore I am an existing list! :]"

# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType: 
    print "I'm a tuple, therefore I am an existing tuple! :]"
3

alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
7
alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
8
alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
9
>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
76
from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]

# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]

if "a_list" in globals[] and type[a_list] is ListType: 
    print "I'm a list, therefore I am an existing list! :]"

# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType: 
    print "I'm a tuple, therefore I am an existing tuple! :]"
1

alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
8
alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
9
>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
55
from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]

# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]

if "a_list" in globals[] and type[a_list] is ListType: 
    print "I'm a list, therefore I am an existing list! :]"

# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType: 
    print "I'm a tuple, therefore I am an existing tuple! :]"
1

>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
1

alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
3
alist = [1,2,3]
if alist: print "I'm here!"

Output: I'm here!
86
alist = [1,2,3]
if alist: print "I'm here!"

Output: I'm here!
87
alist = [1,2,3]
if alist: print "I'm here!"

Output: I'm here!
88
>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
6
alist = [1,2,3]
if alist: print "I'm here!"

Output: I'm here!
90
>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
8
from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]

# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]

if "a_list" in globals[] and type[a_list] is ListType: 
    print "I'm a list, therefore I am an existing list! :]"

# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType: 
    print "I'm a tuple, therefore I am an existing tuple! :]"
3

Đầu ra

Python3

Phương pháp 5: Sử dụng hàm bộ đếm []

alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
3
alist = [1,2,3]
if alist: print "I'm here!"

Output: I'm here!
35
alist = [1,2,3]
if alist: print "I'm here!"

Output: I'm here!
8
alist = [1,2,3]
if alist: print "I'm here!"

Output: I'm here!
37
>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
6
alist = [1,2,3]
if alist: print "I'm here!"

Output: I'm here!
39
alist = [1,2,3]
if alist: print "I'm here!"

Output: I'm here!
8
Input: 7  # Check if 7 exist or not.
Output: False
4

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

alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
3
alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
28
>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
43
alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
30
>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
64
Input: 7  # Check if 7 exist or not.
Output: False
4

alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
7
alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
8
alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
9
>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
69
from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]

# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]

if "a_list" in globals[] and type[a_list] is ListType: 
    print "I'm a list, therefore I am an existing list! :]"

# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType: 
    print "I'm a tuple, therefore I am an existing tuple! :]"
1

from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]

# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]

if "a_list" in globals[] and type[a_list] is ListType: 
    print "I'm a list, therefore I am an existing list! :]"

# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType: 
    print "I'm a tuple, therefore I am an existing tuple! :]"
2
from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]

# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]

if "a_list" in globals[] and type[a_list] is ListType: 
    print "I'm a list, therefore I am an existing list! :]"

# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType: 
    print "I'm a tuple, therefore I am an existing tuple! :]"
3

alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
7
alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
8
alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
9
>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
76
from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]

# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]

if "a_list" in globals[] and type[a_list] is ListType: 
    print "I'm a list, therefore I am an existing list! :]"

# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType: 
    print "I'm a tuple, therefore I am an existing tuple! :]"
1

alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
8
alist = []
if not alist: print "Somebody here?"

Output: Somebody here?
9
>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
55
from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]

# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]

if "a_list" in globals[] and type[a_list] is ListType: 
    print "I'm a list, therefore I am an existing list! :]"

# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType: 
    print "I'm a tuple, therefore I am an existing tuple! :]"
1

>>> try:
...     len[b]
... except Exception as e:
...     print e
... 
name 'b' is not defined
4


Làm thế nào để bạn kiểm tra xem một danh sách có tồn tại trong Python không?

Đếm [] để kiểm tra xem danh sách có chứa không. Một phương thức tích hợp khác trong Python, Count [] trả về số lần phần tử được truyền xảy ra trong danh sách. Nếu phần tử không có trong danh sách thì số lượng [] sẽ trả về 0. Nếu nó trả về một số nguyên dương lớn hơn 0, điều đó có nghĩa là danh sách chứa phần tử. to check if the list contains. Another built-in method in Python, count[] returns the number of times the passed element occurs in the list. If the element is not there in the list then the count[] will return 0. If it returns a positive integer greater than 0, it means the list contains the element.

Làm thế nào để bạn kiểm tra xem một danh sách có tồn tại trong danh sách các danh sách Python không?

Để kiểm tra xem mục có tồn tại trong danh sách hay không, hãy sử dụng Python, trong toán tử.Ví dụ: chúng ta có thể sử dụng toán tử trong các toán tử trong điều kiện IF và nếu mục tồn tại trong danh sách, thì điều kiện sẽ trả về đúng và nếu không, thì nó sẽ trả về sai.use Python “in operator”. For example, we can use the “in” operator with the if condition, and if the item exists in the list, then the condition returns True, and if not, then it returns False.

Làm thế nào để bạn kiểm tra xem một danh sách có mặt trong một danh sách khác trong Python?

Đưa ra hai danh sách A và B, hãy viết một chương trình Python để kiểm tra xem Danh sách A có chứa trong danh sách B mà không phá vỡ thứ tự của A.Một cách tiếp cận hiệu quả hơn là sử dụng danh sách hiểu.Trước tiên chúng tôi khởi tạo 'n' với độ dài của A. Bây giờ sử dụng A cho vòng lặp cho đến Len [B] -n và kiểm tra từng lần lặp nếu a == B [i: i+n] hoặc không.use List comprehension. We first initialize 'n' with length of A. Now use a for loop till len[B]-n and check in each iteration if A == B[i:i+n] or not.

Làm cách nào để kiểm tra nếu một danh sách isempty?

Phương pháp 1: Sử dụng không vận hành..
Tạo một biến để lưu trữ danh sách trống đầu vào ..
Kiểm tra xem danh sách có trống hay không bằng cách sử dụng nếu điều kiện và không vận hành ..
Nếu danh sách trống thì [không phải danh sách] trả về đúng ..
Nếu [không phải danh sách] trả về sai thì danh sách không trống ..

Bài Viết Liên Quan

Chủ Đề