Hướng dẫn python all values in list - python tất cả các giá trị trong danh sách

Người chiến thắng chung cuộc giữa việc sử dụng NP.SUM, NP.min và tất cả dường như là NP.min về tốc độ cho các mảng lớn:

N = 1000000
def func_sum[x]:
    my_list = np.random.randn[N]
    return np.sum[my_list < x ]==0

def func_min[x]:
    my_list = np.random.randn[N]
    return np.min[my_list] >= x

def func_all[x]:
    my_list = np.random.randn[N]
    return all[i >= x for i in my_list]

.

Hiệu suất của "tất cả" phụ thuộc rất nhiều vào khi phần tử đầu tiên không đáp ứng các tiêu chí được tìm thấy, np.sum cần thực hiện một chút hoạt động, NP.min là nhẹ nhất về các tính toán trong trường hợp chung .

Khi các tiêu chí gần như được đáp ứng ngay lập tức và tất cả các vòng lặp thoát nhanh, tất cả các chức năng chỉ chiến thắng một chút so với NP.min:

>>> %timeit func_sum[10]
10 loops, best of 3: 36.1 ms per loop

>>> %timeit func_min[10]
10 loops, best of 3: 35.1 ms per loop

>>> %timeit func_all[10]
10 loops, best of 3: 35 ms per loop

Nhưng khi "tất cả" cần phải trải qua tất cả các điểm, nó chắc chắn tồi tệ hơn nhiều và NP.min thắng:

>>> %timeit func_sum[-10]
10 loops, best of 3: 36.2 ms per loop

>>> %timeit func_min[-10]
10 loops, best of 3: 35.2 ms per loop

>>> %timeit func_all[-10]
10 loops, best of 3: 230 ms per loop

Nhưng sử dụng

np.sum[my_list>> %timeit func_sum[10]
10 loops, best of 3: 36.1 ms per loop

>>> %timeit func_min[10]
10 loops, best of 3: 35.1 ms per loop

>>> %timeit func_all[10]
10 loops, best of 3: 35 ms per loop
0
>>> %timeit func_sum[10]
10 loops, best of 3: 36.1 ms per loop

>>> %timeit func_min[10]
10 loops, best of 3: 35.1 ms per loop

>>> %timeit func_all[10]
10 loops, best of 3: 35 ms per loop
1

>>> %timeit func_sum[10]
10 loops, best of 3: 36.1 ms per loop

>>> %timeit func_min[10]
10 loops, best of 3: 35.1 ms per loop

>>> %timeit func_all[10]
10 loops, best of 3: 35 ms per loop
2
>>> %timeit func_sum[10]
10 loops, best of 3: 36.1 ms per loop

>>> %timeit func_min[10]
10 loops, best of 3: 35.1 ms per loop

>>> %timeit func_all[10]
10 loops, best of 3: 35 ms per loop
3
>>> %timeit func_sum[10]
10 loops, best of 3: 36.1 ms per loop

>>> %timeit func_min[10]
10 loops, best of 3: 35.1 ms per loop

>>> %timeit func_all[10]
10 loops, best of 3: 35 ms per loop
4
>>> %timeit func_sum[10]
10 loops, best of 3: 36.1 ms per loop

>>> %timeit func_min[10]
10 loops, best of 3: 35.1 ms per loop

>>> %timeit func_all[10]
10 loops, best of 3: 35 ms per loop
5
>>> %timeit func_sum[10]
10 loops, best of 3: 36.1 ms per loop

>>> %timeit func_min[10]
10 loops, best of 3: 35.1 ms per loop

>>> %timeit func_all[10]
10 loops, best of 3: 35 ms per loop
6

>>> %timeit func_sum[-10]
10 loops, best of 3: 36.2 ms per loop

>>> %timeit func_min[-10]
10 loops, best of 3: 35.2 ms per loop

>>> %timeit func_all[-10]
10 loops, best of 3: 230 ms per loop
3
>>> %timeit func_sum[10]
10 loops, best of 3: 36.1 ms per loop

>>> %timeit func_min[10]
10 loops, best of 3: 35.1 ms per loop

>>> %timeit func_all[10]
10 loops, best of 3: 35 ms per loop
5
>>> %timeit func_sum[-10]
10 loops, best of 3: 36.2 ms per loop

>>> %timeit func_min[-10]
10 loops, best of 3: 35.2 ms per loop

>>> %timeit func_all[-10]
10 loops, best of 3: 230 ms per loop
5
>>> %timeit func_sum[-10]
10 loops, best of 3: 36.2 ms per loop

>>> %timeit func_min[-10]
10 loops, best of 3: 35.2 ms per loop

>>> %timeit func_all[-10]
10 loops, best of 3: 230 ms per loop
6
>>> %timeit func_sum[-10]
10 loops, best of 3: 36.2 ms per loop

>>> %timeit func_min[-10]
10 loops, best of 3: 35.2 ms per loop

>>> %timeit func_all[-10]
10 loops, best of 3: 230 ms per loop
7
>>> %timeit func_sum[-10]
10 loops, best of 3: 36.2 ms per loop

>>> %timeit func_min[-10]
10 loops, best of 3: 35.2 ms per loop

>>> %timeit func_all[-10]
10 loops, best of 3: 230 ms per loop
8
>>> %timeit func_sum[-10]
10 loops, best of 3: 36.2 ms per loop

>>> %timeit func_min[-10]
10 loops, best of 3: 35.2 ms per loop

>>> %timeit func_all[-10]
10 loops, best of 3: 230 ms per loop
7
np.sum[my_list>> %timeit func_sum[-10]
10 loops, best of 3: 36.2 ms per loop

>>> %timeit func_min[-10]
10 loops, best of 3: 35.2 ms per loop

>>> %timeit func_all[-10]
10 loops, best of 3: 230 ms per loop
7
np.sum[my_list>> %timeit func_sum[-10]
10 loops, best of 3: 36.2 ms per loop

>>> %timeit func_min[-10]
10 loops, best of 3: 35.2 ms per loop

>>> %timeit func_all[-10]
10 loops, best of 3: 230 ms per loop
7
np.sum[my_list>> %timeit func_sum[-10]
10 loops, best of 3: 36.2 ms per loop

>>> %timeit func_min[-10]
10 loops, best of 3: 35.2 ms per loop

>>> %timeit func_all[-10]
10 loops, best of 3: 230 ms per loop
7
np.sum[my_list> %timeit func_sum[10]
10 loops, best of 3: 36.1 ms per loop

>>> %timeit func_min[10]
10 loops, best of 3: 35.1 ms per loop

>>> %timeit func_all[10]
10 loops, best of 3: 35 ms per loop
7
>>> %timeit func_sum[10]
10 loops, best of 3: 36.1 ms per loop

>>> %timeit func_min[10]
10 loops, best of 3: 35.1 ms per loop

>>> %timeit func_all[10]
10 loops, best of 3: 35 ms per loop
8
>>> %timeit func_sum[10]
10 loops, best of 3: 36.1 ms per loop

>>> %timeit func_min[10]
10 loops, best of 3: 35.1 ms per loop

>>> %timeit func_all[10]
10 loops, best of 3: 35 ms per loop
9

>>> %timeit func_sum[10]
10 loops, best of 3: 36.1 ms per loop

>>> %timeit func_min[10]
10 loops, best of 3: 35.1 ms per loop

>>> %timeit func_all[10]
10 loops, best of 3: 35 ms per loop
3
Input : list = [10, 20, 30, 40, 50] 
        given value = 20 
Output : No

Input : list = [10, 20, 30, 40, 50] 
        given value = 5 
Output : Yes
2

    

Input : list = [10, 20, 30, 40, 50] 
        given value = 20 
Output : No

Input : list = [10, 20, 30, 40, 50] 
        given value = 5 
Output : Yes
4
Input : list = [10, 20, 30, 40, 50] 
        given value = 20 
Output : No

Input : list = [10, 20, 30, 40, 50] 
        given value = 5 
Output : Yes
5

Input : list = [10, 20, 30, 40, 50] 
        given value = 20 
Output : No

Input : list = [10, 20, 30, 40, 50] 
        given value = 5 
Output : Yes
6
Input : list = [10, 20, 30, 40, 50] 
        given value = 20 
Output : No

Input : list = [10, 20, 30, 40, 50] 
        given value = 5 
Output : Yes
7

    

Input : list = [10, 20, 30, 40, 50] 
        given value = 20 
Output : No

Input : list = [10, 20, 30, 40, 50] 
        given value = 5 
Output : Yes
4def0

    

>>> %timeit func_sum[10]
10 loops, best of 3: 36.1 ms per loop

>>> %timeit func_min[10]
10 loops, best of 3: 35.1 ms per loop

>>> %timeit func_all[10]
10 loops, best of 3: 35 ms per loop
8
>>> %timeit func_sum[-10]
10 loops, best of 3: 36.2 ms per loop

>>> %timeit func_min[-10]
10 loops, best of 3: 35.2 ms per loop

>>> %timeit func_all[-10]
10 loops, best of 3: 230 ms per loop
2

>>> %timeit func_sum[10]
10 loops, best of 3: 36.1 ms per loop

>>> %timeit func_min[10]
10 loops, best of 3: 35.1 ms per loop

>>> %timeit func_all[10]
10 loops, best of 3: 35 ms per loop
3
Input : list = [10, 20, 30, 40, 50] 
        given value = 20 
Output : No

Input : list = [10, 20, 30, 40, 50] 
        given value = 5 
Output : Yes
2

    

Input : list = [10, 20, 30, 40, 50] 
        given value = 20 
Output : No

Input : list = [10, 20, 30, 40, 50] 
        given value = 5 
Output : Yes
4
Input : list = [10, 20, 30, 40, 50] 
        given value = 20 
Output : No

Input : list = [10, 20, 30, 40, 50] 
        given value = 5 
Output : Yes
5

Input : list = [10, 20, 30, 40, 50] 
        given value = 20 
Output : No

Input : list = [10, 20, 30, 40, 50] 
        given value = 5 
Output : Yes
6
Input : list = [10, 20, 30, 40, 50] 
        given value = 20 
Output : No

Input : list = [10, 20, 30, 40, 50] 
        given value = 5 
Output : Yes
7

    

Input : list = [10, 20, 30, 40, 50] 
        given value = 20 
Output : No

Input : list = [10, 20, 30, 40, 50] 
        given value = 5 
Output : Yes
4def0

np.sum[my_list>> %timeit func_sum[10]
10 loops, best of 3: 36.1 ms per loop

>>> %timeit func_min[10]
10 loops, best of 3: 35.1 ms per loop

>>> %timeit func_all[10]
10 loops, best of 3: 35 ms per loop
5
Input : list = [10, 20, 30, 40, 50] 
        given value = 20 
Output : No

Input : list = [10, 20, 30, 40, 50] 
        given value = 5 
Output : Yes
0

np.sum[my_list>> %timeit func_sum[10]
10 loops, best of 3: 36.1 ms per loop

>>> %timeit func_min[10]
10 loops, best of 3: 35.1 ms per loop

>>> %timeit func_all[10]
10 loops, best of 3: 35 ms per loop
5
>>> %timeit func_sum[-10]
10 loops, best of 3: 36.2 ms per loop

>>> %timeit func_min[-10]
10 loops, best of 3: 35.2 ms per loop

>>> %timeit func_all[-10]
10 loops, best of 3: 230 ms per loop
8

Implementation:

Phương pháp 1: Danh sách truyền tải

Bằng cách đi qua trong danh sách, chúng ta có thể so sánh mọi yếu tố và kiểm tra xem tất cả các phần tử trong danh sách đã cho có lớn hơn giá trị đã cho hay không. & NBSP;

Python

>>> %timeit func_sum[-10]
10 loops, best of 3: 36.2 ms per loop

>>> %timeit func_min[-10]
10 loops, best of 3: 35.2 ms per loop

>>> %timeit func_all[-10]
10 loops, best of 3: 230 ms per loop
3
>>> %timeit func_sum[10]
10 loops, best of 3: 36.1 ms per loop

>>> %timeit func_min[10]
10 loops, best of 3: 35.1 ms per loop

>>> %timeit func_all[10]
10 loops, best of 3: 35 ms per loop
5
>>> %timeit func_sum[-10]
10 loops, best of 3: 36.2 ms per loop

>>> %timeit func_min[-10]
10 loops, best of 3: 35.2 ms per loop

>>> %timeit func_all[-10]
10 loops, best of 3: 230 ms per loop
5
>>> %timeit func_sum[-10]
10 loops, best of 3: 36.2 ms per loop

>>> %timeit func_min[-10]
10 loops, best of 3: 35.2 ms per loop

>>> %timeit func_all[-10]
10 loops, best of 3: 230 ms per loop
6
>>> %timeit func_sum[-10]
10 loops, best of 3: 36.2 ms per loop

>>> %timeit func_min[-10]
10 loops, best of 3: 35.2 ms per loop

>>> %timeit func_all[-10]
10 loops, best of 3: 230 ms per loop
7
>>> %timeit func_sum[-10]
10 loops, best of 3: 36.2 ms per loop

>>> %timeit func_min[-10]
10 loops, best of 3: 35.2 ms per loop

>>> %timeit func_all[-10]
10 loops, best of 3: 230 ms per loop
8
>>> %timeit func_sum[-10]
10 loops, best of 3: 36.2 ms per loop

>>> %timeit func_min[-10]
10 loops, best of 3: 35.2 ms per loop

>>> %timeit func_all[-10]
10 loops, best of 3: 230 ms per loop
7
np.sum[my_list>> %timeit func_sum[-10]
10 loops, best of 3: 36.2 ms per loop

>>> %timeit func_min[-10]
10 loops, best of 3: 35.2 ms per loop

>>> %timeit func_all[-10]
10 loops, best of 3: 230 ms per loop
7
np.sum[my_list>> %timeit func_sum[-10]
10 loops, best of 3: 36.2 ms per loop

>>> %timeit func_min[-10]
10 loops, best of 3: 35.2 ms per loop

>>> %timeit func_all[-10]
10 loops, best of 3: 230 ms per loop
7
np.sum[my_list>> %timeit func_sum[-10]
10 loops, best of 3: 36.2 ms per loop

>>> %timeit func_min[-10]
10 loops, best of 3: 35.2 ms per loop

>>> %timeit func_all[-10]
10 loops, best of 3: 230 ms per loop
7
np.sum[my_list> %timeit func_sum[10]
10 loops, best of 3: 36.1 ms per loop

>>> %timeit func_min[10]
10 loops, best of 3: 35.1 ms per loop

>>> %timeit func_all[10]
10 loops, best of 3: 35 ms per loop
3
Input : list = [10, 20, 30, 40, 50] 
        given value = 20 
Output : No

Input : list = [10, 20, 30, 40, 50] 
        given value = 5 
Output : Yes
2

    

Input : list = [10, 20, 30, 40, 50] 
        given value = 20 
Output : No

Input : list = [10, 20, 30, 40, 50] 
        given value = 5 
Output : Yes
4
Input : list = [10, 20, 30, 40, 50] 
        given value = 20 
Output : No

Input : list = [10, 20, 30, 40, 50] 
        given value = 5 
Output : Yes
5

Input : list = [10, 20, 30, 40, 50] 
        given value = 20 
Output : No

Input : list = [10, 20, 30, 40, 50] 
        given value = 5 
Output : Yes
6
Input : list = [10, 20, 30, 40, 50] 
        given value = 20 
Output : No

Input : list = [10, 20, 30, 40, 50] 
        given value = 5 
Output : Yes
7

    

Input : list = [10, 20, 30, 40, 50] 
        given value = 20 
Output : No

Input : list = [10, 20, 30, 40, 50] 
        given value = 5 
Output : Yes
4def0

    

>>> %timeit func_sum[10]
10 loops, best of 3: 36.1 ms per loop

>>> %timeit func_min[10]
10 loops, best of 3: 35.1 ms per loop

>>> %timeit func_all[10]
10 loops, best of 3: 35 ms per loop
8
>>> %timeit func_sum[-10]
10 loops, best of 3: 36.2 ms per loop

>>> %timeit func_min[-10]
10 loops, best of 3: 35.2 ms per loop

>>> %timeit func_all[-10]
10 loops, best of 3: 230 ms per loop
2

np.sum[my_list>> %timeit func_sum[10]
10 loops, best of 3: 36.1 ms per loop

>>> %timeit func_min[10]
10 loops, best of 3: 35.1 ms per loop

>>> %timeit func_all[10]
10 loops, best of 3: 35 ms per loop
5
Input : list = [10, 20, 30, 40, 50] 
        given value = 20 
Output : No

Input : list = [10, 20, 30, 40, 50] 
        given value = 5 
Output : Yes
0

    

Input : list = [10, 20, 30, 40, 50] 
        given value = 20 
Output : No

Input : list = [10, 20, 30, 40, 50] 
        given value = 5 
Output : Yes
4
Input : list = [10, 20, 30, 40, 50] 
        given value = 20 
Output : No

Input : list = [10, 20, 30, 40, 50] 
        given value = 5 
Output : Yes
5

Input : list = [10, 20, 30, 40, 50] 
        given value = 20 
Output : No

Input : list = [10, 20, 30, 40, 50] 
        given value = 5 
Output : Yes
6
Input : list = [10, 20, 30, 40, 50] 
        given value = 20 
Output : No

Input : list = [10, 20, 30, 40, 50] 
        given value = 5 
Output : Yes
7

    

Input : list = [10, 20, 30, 40, 50] 
        given value = 20 
Output : No

Input : list = [10, 20, 30, 40, 50] 
        given value = 5 
Output : Yes
4def0


Làm cách nào để nhận được tất cả các giá trị của một danh sách trong Python?

Sử dụng: L = [[1,2,3], [1,2,3]] SUM [Bản đồ [LEN, L]] để lấy 6, nhưng đối với bạn, một mục là một số, nhưng một mục cho danh sáchchỉ là một mục có thể là một danh sách, số, nổi, đối tượng, v.v. ....
Hoạt động bạn đang tìm kiếm là "Flatten" danh sách.....
Có lẽ lặp lại là giải pháp dễ đọc nhất IMO ..

Làm cách nào để kiểm tra tất cả các giá trị trong một danh sách?

Sử dụng tất cả [] hàm chúng ta có thể kiểm tra xem tất cả các giá trị có nhỏ hơn bất kỳ giá trị nào trong một dòng không.Nó trả về đúng nếu điều kiện đã cho bên trong hàm tất cả [] là đúng đối với tất cả các giá trị, nếu không nó sẽ trả về sai. we can check if all values are less than any given value in a single line. It returns true if the given condition inside the all[] function is true for all values, else it returns false.

Bất kỳ [] và tất cả [] trong Python là gì?

Các chức năng Python bất kỳ [] và tất cả [] [] đánh giá các mục trong một danh sách để xem cái nào là đúng.Phương thức bất kỳ [] trả về đúng nếu bất kỳ mục nào trong danh sách là đúng và hàm tất cả [] trả về đúng nếu tất cả các mục danh sách là đúng.evaluate the items in a list to see which are true. The any[] method returns true if any of the list items are true, and the all[] function returns true if all the list items are true.

Bài Viết Liên Quan

Toplist mới

Bài mới nhất

Chủ Đề