Hướng dẫn how do you check if a list contains only numbers in python? - làm thế nào để bạn kiểm tra xem một danh sách chỉ chứa các số trong python?

Cách thông thường để kiểm tra xem có thể chuyển đổi thứ gì đó thành

for item in mylist:
    try:
        int_value = int(item)
    except ValueError:
        pass
    else:
        mynewlist.append(item) # or append(int_value) if you want numbers
2 hay không là
for item in mylist:
    try:
        int_value = int(item)
    except ValueError:
        pass
    else:
        mynewlist.append(item) # or append(int_value) if you want numbers
3 và xem, tuân theo nguyên tắc EAFP:

try:
    int_value = int(string_value)
except ValueError:
    # it wasn't an int, do something appropriate
else:
    # it was an int, do something appropriate

Vì vậy, trong trường hợp của bạn:

for item in mylist:
    try:
        int_value = int(item)
    except ValueError:
        pass
    else:
        mynewlist.append(item) # or append(int_value) if you want numbers

Trong hầu hết các trường hợp, một vòng lặp xung quanh một số mã tầm thường kết thúc bằng

for item in mylist:
    try:
        int_value = int(item)
    except ValueError:
        pass
    else:
        mynewlist.append(item) # or append(int_value) if you want numbers
4 có thể được biến thành một danh sách hiểu, biểu thức trình tạo hoặc gọi đến
for item in mylist:
    try:
        int_value = int(item)
    except ValueError:
        pass
    else:
        mynewlist.append(item) # or append(int_value) if you want numbers
5 hoặc
for item in mylist:
    try:
        int_value = int(item)
    except ValueError:
        pass
    else:
        mynewlist.append(item) # or append(int_value) if you want numbers
6. Nhưng ở đây, bạn không thể, bởi vì không có cách nào để đưa ____ 13/________ 18 vào một biểu thức.

Nhưng nếu bạn bọc nó trong một chức năng, bạn có thể:

def raises(func, *args, **kw):
    try:
        func(*args, **kw)
    except:
        return True
    else:
        return False

mynewlist = [item for item in mylist if not raises(int, item)]

… Hoặc, nếu bạn thích:

mynewlist = filter(partial(raises, int), item)

Nó sạch hơn khi sử dụng nó theo cách này:

def raises(exception_types, func, *args, **kw):
    try:
        func(*args, **kw)
    except exception_types:
        return True
    else:
        return False

Bằng cách này, bạn có thể vượt qua nó là ngoại lệ (hoặc tuple của các trường hợp ngoại lệ) mà bạn đang mong đợi và những điều đó sẽ trả lại

for item in mylist:
    try:
        int_value = int(item)
    except ValueError:
        pass
    else:
        mynewlist.append(item) # or append(int_value) if you want numbers
9, nhưng nếu có bất kỳ ngoại lệ bất ngờ nào được nêu ra, chúng sẽ lan truyền. Vì thế:

mynewlist = [item for item in mylist if not raises(ValueError, int, item)]

… Sẽ làm những gì bạn muốn, nhưng:

mynewlist = [item for item in mylist if not raises(ValueError, item, int)]

Càng sẽ tăng một

def raises(func, *args, **kw):
    try:
        func(*args, **kw)
    except:
        return True
    else:
        return False

mynewlist = [item for item in mylist if not raises(int, item)]
0, như nó nên.

Đưa ra một danh sách chỉ chứa các phần tử chuỗi Nhiệm vụ ở đây là viết một chương trình Python để kiểm tra xem tất cả chúng có phải là số hay không. Nếu tất cả đều trả về số đúng khác, hãy trả về sai.

Input : test_list = ["434", "823", "98", "74"]
Output : True
Explanation : All Strings are digits.
Input : test_list = ["434", "82e", "98", "74"]
Output : False
Explanation : e is not digit, hence verdict is False.

Phương pháp 1: Sử dụng tất cả (), isDigit () và biểu thức máy phátUsing all(), isdigit() and generator expression

Trong đó, chúng tôi kiểm tra các số từ isDigit (). Tất cả () được sử dụng để kiểm tra tất cả các chuỗi là số và lần lặp cho mỗi chuỗi được thực hiện bằng cách sử dụng biểu thức máy phát.

Example:

Python3

Các

mynewlist = filter(partial(raises, int), item)
2
mynewlist = filter(partial(raises, int), item)
3
mynewlist = filter(partial(raises, int), item)
4
mynewlist = filter(partial(raises, int), item)
5
mynewlist = filter(partial(raises, int), item)
6
mynewlist = filter(partial(raises, int), item)
7

mynewlist = filter(partial(raises, int), item)
8
def raises(func, *args, **kw):
    try:
        func(*args, **kw)
    except:
        return True
    else:
        return False

mynewlist = [item for item in mylist if not raises(int, item)]
2
def raises(exception_types, func, *args, **kw):
    try:
        func(*args, **kw)
    except exception_types:
        return True
    else:
        return False
0
def raises(exception_types, func, *args, **kw):
    try:
        func(*args, **kw)
    except exception_types:
        return True
    else:
        return False
1
def raises(exception_types, func, *args, **kw):
    try:
        func(*args, **kw)
    except exception_types:
        return True
    else:
        return False
2
def raises(exception_types, func, *args, **kw):
    try:
        func(*args, **kw)
    except exception_types:
        return True
    else:
        return False
3
def raises(exception_types, func, *args, **kw):
    try:
        func(*args, **kw)
    except exception_types:
        return True
    else:
        return False
4
def raises(exception_types, func, *args, **kw):
    try:
        func(*args, **kw)
    except exception_types:
        return True
    else:
        return False
5

mynewlist = filter(partial(raises, int), item)
2
mynewlist = filter(partial(raises, int), item)
3
def raises(exception_types, func, *args, **kw):
    try:
        func(*args, **kw)
    except exception_types:
        return True
    else:
        return False
8
mynewlist = filter(partial(raises, int), item)
5
mynewlist = filter(partial(raises, int), item)
6
mynewlist = [item for item in mylist if not raises(ValueError, int, item)]
1

Đầu ra

The original list is : ['434', '823', '98', '74']
Are all strings digits ? : True

Độ phức tạp về thời gian: O (n) O(n)

Độ phức tạp không gian: O (n)O(n)

Phương pháp 2: Sử dụng & nbsp; all (), isDigit () và map () Using  all(), isdigit() and map()

Trong đó, chúng tôi mở rộng logic kiểm tra cho từng chuỗi bằng bản đồ (), thay vì biểu thức của trình tạo. Nghỉ ngơi tất cả các chức năng được thực hiện tương tự như phương thức trên.

Example:

Python3

Các

mynewlist = filter(partial(raises, int), item)
2
mynewlist = filter(partial(raises, int), item)
3
mynewlist = filter(partial(raises, int), item)
4
mynewlist = filter(partial(raises, int), item)
5
mynewlist = filter(partial(raises, int), item)
6
mynewlist = filter(partial(raises, int), item)
7

mynewlist = filter(partial(raises, int), item)
8
def raises(func, *args, **kw):
    try:
        func(*args, **kw)
    except:
        return True
    else:
        return False

mynewlist = [item for item in mylist if not raises(int, item)]
2
def raises(exception_types, func, *args, **kw):
    try:
        func(*args, **kw)
    except exception_types:
        return True
    else:
        return False
0
def raises(exception_types, func, *args, **kw):
    try:
        func(*args, **kw)
    except exception_types:
        return True
    else:
        return False
1
def raises(exception_types, func, *args, **kw):
    try:
        func(*args, **kw)
    except exception_types:
        return True
    else:
        return False
2
def raises(exception_types, func, *args, **kw):
    try:
        func(*args, **kw)
    except exception_types:
        return True
    else:
        return False
3
def raises(exception_types, func, *args, **kw):
    try:
        func(*args, **kw)
    except exception_types:
        return True
    else:
        return False
4
def raises(exception_types, func, *args, **kw):
    try:
        func(*args, **kw)
    except exception_types:
        return True
    else:
        return False
5

mynewlist = filter(partial(raises, int), item)
2
mynewlist = filter(partial(raises, int), item)
3
def raises(exception_types, func, *args, **kw):
    try:
        func(*args, **kw)
    except exception_types:
        return True
    else:
        return False
8
mynewlist = filter(partial(raises, int), item)
5
mynewlist = filter(partial(raises, int), item)
6
mynewlist = [item for item in mylist if not raises(ValueError, int, item)]
1

Đầu ra

The original list is : ['434', '823', '98', '74']
Are all strings digits ? : True

Độ phức tạp về thời gian: O (n)O(n)

Độ phức tạp không gian: O (n)O(n)

Phương pháp 2: Sử dụng & nbsp; all (), isDigit () và map ()Using isnumeric() and len() methods

Python3

Trong đó, chúng tôi mở rộng logic kiểm tra cho từng chuỗi bằng bản đồ (), thay vì biểu thức của trình tạo. Nghỉ ngơi tất cả các chức năng được thực hiện tương tự như phương thức trên.

mynewlist = filter(partial(raises, int), item)
2
mynewlist = filter(partial(raises, int), item)
3
mynewlist = filter(partial(raises, int), item)
4
mynewlist = filter(partial(raises, int), item)
5
mynewlist = filter(partial(raises, int), item)
6
mynewlist = filter(partial(raises, int), item)
7

for item in mylist:
    try:
        int_value = int(item)
    except ValueError:
        pass
    else:
        mynewlist.append(item) # or append(int_value) if you want numbers
00
def raises(func, *args, **kw):
    try:
        func(*args, **kw)
    except:
        return True
    else:
        return False

mynewlist = [item for item in mylist if not raises(int, item)]
2
for item in mylist:
    try:
        int_value = int(item)
    except ValueError:
        pass
    else:
        mynewlist.append(item) # or append(int_value) if you want numbers
02

mynewlist = filter(partial(raises, int), item)
8
def raises(func, *args, **kw):
    try:
        func(*args, **kw)
    except:
        return True
    else:
        return False

mynewlist = [item for item in mylist if not raises(int, item)]
2
def raises(exception_types, func, *args, **kw):
    try:
        func(*args, **kw)
    except exception_types:
        return True
    else:
        return False
0
def raises(exception_types, func, *args, **kw):
    try:
        func(*args, **kw)
    except exception_types:
        return True
    else:
        return False
1
def raises(exception_types, func, *args, **kw):
    try:
        func(*args, **kw)
    except exception_types:
        return True
    else:
        return False
2
def raises(exception_types, func, *args, **kw):
    try:
        func(*args, **kw)
    except exception_types:
        return True
    else:
        return False
3
def raises(exception_types, func, *args, **kw):
    try:
        func(*args, **kw)
    except exception_types:
        return True
    else:
        return False
4
def raises(exception_types, func, *args, **kw):
    try:
        func(*args, **kw)
    except exception_types:
        return True
    else:
        return False
5

mynewlist = filter(partial(raises, int), item)
2
mynewlist = filter(partial(raises, int), item)
3
def raises(exception_types, func, *args, **kw):
    try:
        func(*args, **kw)
    except exception_types:
        return True
    else:
        return False
8
mynewlist = filter(partial(raises, int), item)
5
mynewlist = filter(partial(raises, int), item)
6
mynewlist = [item for item in mylist if not raises(ValueError, int, item)]
1

for item in mylist:
    try:
        int_value = int(item)
    except ValueError:
        pass
    else:
        mynewlist.append(item) # or append(int_value) if you want numbers
10
for item in mylist:
    try:
        int_value = int(item)
    except ValueError:
        pass
    else:
        mynewlist.append(item) # or append(int_value) if you want numbers
00
mynewlist = filter(partial(raises, int), item)
5
def raises(func, *args, **kw):
    try:
        func(*args, **kw)
    except:
        return True
    else:
        return False

mynewlist = [item for item in mylist if not raises(int, item)]
2
for item in mylist:
    try:
        int_value = int(item)
    except ValueError:
        pass
    else:
        mynewlist.append(item) # or append(int_value) if you want numbers
14

Đầu ra

for item in mylist:
    try:
        int_value = int(item)
    except ValueError:
        pass
    else:
        mynewlist.append(item) # or append(int_value) if you want numbers
08
for item in mylist:
    try:
        int_value = int(item)
    except ValueError:
        pass
    else:
        mynewlist.append(item) # or append(int_value) if you want numbers
19
def raises(func, *args, **kw):
    try:
        func(*args, **kw)
    except:
        return True
    else:
        return False

mynewlist = [item for item in mylist if not raises(int, item)]
2
def raises(func, *args, **kw):
    try:
        func(*args, **kw)
    except:
        return True
    else:
        return False

mynewlist = [item for item in mylist if not raises(int, item)]
2
for item in mylist:
    try:
        int_value = int(item)
    except ValueError:
        pass
    else:
        mynewlist.append(item) # or append(int_value) if you want numbers
22
for item in mylist:
    try:
        int_value = int(item)
    except ValueError:
        pass
    else:
        mynewlist.append(item) # or append(int_value) if you want numbers
23

for item in mylist:
    try:
        int_value = int(item)
    except ValueError:
        pass
    else:
        mynewlist.append(item) # or append(int_value) if you want numbers
07
for item in mylist:
    try:
        int_value = int(item)
    except ValueError:
        pass
    else:
        mynewlist.append(item) # or append(int_value) if you want numbers
15
def raises(func, *args, **kw):
    try:
        func(*args, **kw)
    except:
        return True
    else:
        return False

mynewlist = [item for item in mylist if not raises(int, item)]
2
for item in mylist:
    try:
        int_value = int(item)
    except ValueError:
        pass
    else:
        mynewlist.append(item) # or append(int_value) if you want numbers
9

mynewlist = filter(partial(raises, int), item)
2
mynewlist = filter(partial(raises, int), item)
3
def raises(exception_types, func, *args, **kw):
    try:
        func(*args, **kw)
    except exception_types:
        return True
    else:
        return False
8
mynewlist = filter(partial(raises, int), item)
5
mynewlist = filter(partial(raises, int), item)
6
mynewlist = [item for item in mylist if not raises(ValueError, int, item)]
1

Đầu ra

for item in mylist:
    try:
        int_value = int(item)
    except ValueError:
        pass
    else:
        mynewlist.append(item) # or append(int_value) if you want numbers
1

Độ phức tạp về thời gian: O (n)O(n)

Độ phức tạp không gian: O (n)O(n)


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

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.use the in-built python List method, count(), to check if the passed element exists in the List. If the passed element exists in the List, the count() method will show the number of times it occurs in the entire list. If it is a non-zero positive number, it means an element exists in the List.

Làm thế nào để tôi chỉ nhận được số trong danh sách Python?

Nhận các giá trị số nguyên từ danh sách bằng hàm isDigit () và danh sách hiểu (nếu tất cả các giá trị ở định dạng chuỗi) hàm isDigit () tích hợp của python có thể được sử dụng để kiểm tra xem chuỗi có phải là số nguyên không.Hàm này được sử dụng cùng với một danh sách hiểu để có được danh sách tất cả các số nguyên trong một danh sách nhất định.using isdigit() function and list comprehension (If all values are in string format) Python's built-in isdigit() function can be used to check if a string is an integer. This function is used in conjunction with a list comprehension to get a list of all integers in a given list.

Làm thế nào bạn có thể kiểm tra xem một chuỗi đã cho chỉ chứa các chữ cái và số trong Python?

Khoa học dữ liệu thực tế bằng cách sử dụng Python để xác minh rằng chuỗi chỉ chứa các chữ cái, số, dấu gạch dưới và dấu gạch ngang, chúng ta có thể sử dụng regex sau: "^[a-za-z0-9 _-]*$"."^[A-Za-z0-9_-]*$".