Hướng dẫn python count elements in array with condition - python đếm phần tử trong mảng có điều kiện

sum(x.b == 1 for x in L)

Một boolean (kết quả từ các so sánh như x.b == 1) cũng là một int, với giá trị 0 cho False, 1 cho

$ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'len([x for x in L if x==1])'
10000 loops, best of 3: 56.6 usec per loop
$ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'sum(x==1 for x in L)'
10000 loops, best of 3: 87.7 usec per loop
0, do đó, số học như tổng hợp hoạt động tốt.

Show

    Đây là mã đơn giản nhất, nhưng có lẽ không phải là nhanh nhất (chỉ

    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'len([x for x in L if x==1])'
    10000 loops, best of 3: 56.6 usec per loop
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'sum(x==1 for x in L)'
    10000 loops, best of 3: 87.7 usec per loop
    
    1 có thể cho bạn biết chắc chắn ;-). Xem xét (trường hợp đơn giản hóa để phù hợp với các dòng lệnh, nhưng tương đương):

    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'len([x for x in L if x==1])'
    10000 loops, best of 3: 56.6 usec per loop
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'sum(x==1 for x in L)'
    10000 loops, best of 3: 87.7 usec per loop
    

    Vì vậy, đối với trường hợp này, cách tiếp cận "bộ nhớ lãng phí" để tạo ra một danh sách tạm thời thêm và kiểm tra độ dài của nó thực sự nhanh hơn so với phương pháp đơn giản, ngắn hơn, bộ nhớ mà tôi có xu hướng thích. Các hỗn hợp khác của các giá trị danh sách, triển khai Python, tính khả dụng của bộ nhớ để "đầu tư" vào tốc độ này, v.v., có thể ảnh hưởng đến hiệu suất chính xác, tất nhiên.

    Phương pháp số 3: Sử dụng Chức năng giảm giảm () + lambda thực hiện nhiệm vụ tích lũy làm hàm tổng trong các phương thức được sử dụng ở trên. Lambda được sử dụng để thực hiện điều kiện mà kết quả cần phải được kiểm tra. & nbsp; & nbsp;

    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'len([x for x in L if x==1])'
    10000 loops, best of 3: 56.6 usec per loop
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'sum(x==1 for x in L)'
    10000 loops, best of 3: 87.7 usec per loop
    
    2
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'len([x for x in L if x==1])'
    10000 loops, best of 3: 56.6 usec per loop
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'sum(x==1 for x in L)'
    10000 loops, best of 3: 87.7 usec per loop
    
    3
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'len([x for x in L if x==1])'
    10000 loops, best of 3: 56.6 usec per loop
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'sum(x==1 for x in L)'
    10000 loops, best of 3: 87.7 usec per loop
    
    4
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'len([x for x in L if x==1])'
    10000 loops, best of 3: 56.6 usec per loop
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'sum(x==1 for x in L)'
    10000 loops, best of 3: 87.7 usec per loop
    
    5
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'len([x for x in L if x==1])'
    10000 loops, best of 3: 56.6 usec per loop
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'sum(x==1 for x in L)'
    10000 loops, best of 3: 87.7 usec per loop
    
    6
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'len([x for x in L if x==1])'
    10000 loops, best of 3: 56.6 usec per loop
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'sum(x==1 for x in L)'
    10000 loops, best of 3: 87.7 usec per loop
    
    7
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'len([x for x in L if x==1])'
    10000 loops, best of 3: 56.6 usec per loop
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'sum(x==1 for x in L)'
    10000 loops, best of 3: 87.7 usec per loop
    
    61
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'len([x for x in L if x==1])'
    10000 loops, best of 3: 56.6 usec per loop
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'sum(x==1 for x in L)'
    10000 loops, best of 3: 87.7 usec per loop
    
    6
    The original list is : [3, 5, 1, 6, 7, 9]
    The number of odd elements: 5
    1__

    The original list is : [3, 5, 1, 6, 7, 9]
    The number of odd elements: 5
    5
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'len([x for x in L if x==1])'
    10000 loops, best of 3: 56.6 usec per loop
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'sum(x==1 for x in L)'
    10000 loops, best of 3: 87.7 usec per loop
    
    3
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'len([x for x in L if x==1])'
    10000 loops, best of 3: 56.6 usec per loop
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'sum(x==1 for x in L)'
    10000 loops, best of 3: 87.7 usec per loop
    
    24
    The original list is : [3, 5, 1, 6, 7, 9]
    The number of odd elements: 5
    8False6
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'len([x for x in L if x==1])'
    10000 loops, best of 3: 56.6 usec per loop
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'sum(x==1 for x in L)'
    10000 loops, best of 3: 87.7 usec per loop
    
    27
    The original list is : [3, 5, 1, 6, 7, 9]
    The number of odd elements: 5
    2
    Method #1 : Using sum() + generator expression This method uses the trick of adding 1 to the sum whenever the generator expression returns true. By the time list gets exhausted, summation of count of numbers matching a condition is returned. 
     

    Python3

    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'len([x for x in L if x==1])'
    10000 loops, best of 3: 56.6 usec per loop
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'sum(x==1 for x in L)'
    10000 loops, best of 3: 87.7 usec per loop
    
    2
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'len([x for x in L if x==1])'
    10000 loops, best of 3: 56.6 usec per loop
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'sum(x==1 for x in L)'
    10000 loops, best of 3: 87.7 usec per loop
    
    3
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'len([x for x in L if x==1])'
    10000 loops, best of 3: 56.6 usec per loop
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'sum(x==1 for x in L)'
    10000 loops, best of 3: 87.7 usec per loop
    
    4
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'len([x for x in L if x==1])'
    10000 loops, best of 3: 56.6 usec per loop
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'sum(x==1 for x in L)'
    10000 loops, best of 3: 87.7 usec per loop
    
    5
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'len([x for x in L if x==1])'
    10000 loops, best of 3: 56.6 usec per loop
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'sum(x==1 for x in L)'
    10000 loops, best of 3: 87.7 usec per loop
    
    6
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'len([x for x in L if x==1])'
    10000 loops, best of 3: 56.6 usec per loop
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'sum(x==1 for x in L)'
    10000 loops, best of 3: 87.7 usec per loop
    
    7
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'len([x for x in L if x==1])'
    10000 loops, best of 3: 56.6 usec per loop
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'sum(x==1 for x in L)'
    10000 loops, best of 3: 87.7 usec per loop
    
    61
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'len([x for x in L if x==1])'
    10000 loops, best of 3: 56.6 usec per loop
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'sum(x==1 for x in L)'
    10000 loops, best of 3: 87.7 usec per loop
    
    6
    The original list is : [3, 5, 1, 6, 7, 9]
    The number of odd elements: 5
    1__

    The original list is : [3, 5, 1, 6, 7, 9]
    The number of odd elements: 5
    7
    The original list is : [3, 5, 1, 6, 7, 9]
    The number of odd elements: 5
    8
    The original list is : [3, 5, 1, 6, 7, 9]
    The number of odd elements: 5
    9
    The original list is : [3, 5, 1, 6, 7, 9]
    The number of odd elements: 5
    0
    The original list is : [3, 5, 1, 6, 7, 9]
    The number of odd elements: 5
    1
    The original list is : [3, 5, 1, 6, 7, 9]
    The number of odd elements: 5
    2
    The original list is : [3, 5, 1, 6, 7, 9]
    The number of odd elements: 5
    3
    The original list is : [3, 5, 1, 6, 7, 9]
    The number of odd elements: 5
    4

    The original list is : [3, 5, 1, 6, 7, 9]
    The number of odd elements: 5
    5
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'len([x for x in L if x==1])'
    10000 loops, best of 3: 56.6 usec per loop
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'sum(x==1 for x in L)'
    10000 loops, best of 3: 87.7 usec per loop
    
    3
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'len([x for x in L if x==1])'
    10000 loops, best of 3: 56.6 usec per loop
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'sum(x==1 for x in L)'
    10000 loops, best of 3: 87.7 usec per loop
    
    24
    The original list is : [3, 5, 1, 6, 7, 9]
    The number of odd elements: 5
    8False6
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'len([x for x in L if x==1])'
    10000 loops, best of 3: 56.6 usec per loop
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'sum(x==1 for x in L)'
    10000 loops, best of 3: 87.7 usec per loop
    
    27
    The original list is : [3, 5, 1, 6, 7, 9]
    The number of odd elements: 5
    2

    The original list is : [3, 5, 1, 6, 7, 9]
    The number of odd elements: 5
    7 x.b == 13
    The original list is : [3, 5, 1, 6, 7, 9]
    The number of odd elements: 5
    2
    The original list is : [3, 5, 1, 6, 7, 9]
    The number of odd elements: 5
    3x.b == 16

    Đầu ra:

    The original list is : [3, 5, 1, 6, 7, 9]
    The number of odd elements: 5

    Phương pháp số 2: Sử dụng SUM () + MAP () Bản đồ () Nhiệm vụ gần như tương tự như biểu thức của trình tạo, sự khác biệt chỉ là cấu trúc dữ liệu nội bộ được sử dụng bởi nó khác nhau do đó hiệu quả hơn. & NBSP; & nbsp; map() does the task almost similar to the generator expression, difference is just the internal data structure employed by it is different hence more efficient. 
     

    Python3

    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'len([x for x in L if x==1])'
    10000 loops, best of 3: 56.6 usec per loop
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'sum(x==1 for x in L)'
    10000 loops, best of 3: 87.7 usec per loop
    
    2
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'len([x for x in L if x==1])'
    10000 loops, best of 3: 56.6 usec per loop
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'sum(x==1 for x in L)'
    10000 loops, best of 3: 87.7 usec per loop
    
    3
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'len([x for x in L if x==1])'
    10000 loops, best of 3: 56.6 usec per loop
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'sum(x==1 for x in L)'
    10000 loops, best of 3: 87.7 usec per loop
    
    4
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'len([x for x in L if x==1])'
    10000 loops, best of 3: 56.6 usec per loop
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'sum(x==1 for x in L)'
    10000 loops, best of 3: 87.7 usec per loop
    
    5
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'len([x for x in L if x==1])'
    10000 loops, best of 3: 56.6 usec per loop
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'sum(x==1 for x in L)'
    10000 loops, best of 3: 87.7 usec per loop
    
    6
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'len([x for x in L if x==1])'
    10000 loops, best of 3: 56.6 usec per loop
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'sum(x==1 for x in L)'
    10000 loops, best of 3: 87.7 usec per loop
    
    7
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'len([x for x in L if x==1])'
    10000 loops, best of 3: 56.6 usec per loop
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'sum(x==1 for x in L)'
    10000 loops, best of 3: 87.7 usec per loop
    
    61
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'len([x for x in L if x==1])'
    10000 loops, best of 3: 56.6 usec per loop
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'sum(x==1 for x in L)'
    10000 loops, best of 3: 87.7 usec per loop
    
    6
    The original list is : [3, 5, 1, 6, 7, 9]
    The number of odd elements: 5
    1__

    The original list is : [3, 5, 1, 6, 7, 9]
    The number of odd elements: 5
    7
    The original list is : [3, 5, 1, 6, 7, 9]
    The number of odd elements: 5
    8
    The original list is : [3, 5, 1, 6, 7, 9]
    The number of odd elements: 5
    9
    The original list is : [3, 5, 1, 6, 7, 9]
    The number of odd elements: 5
    0
    The original list is : [3, 5, 1, 6, 7, 9]
    The number of odd elements: 5
    1
    The original list is : [3, 5, 1, 6, 7, 9]
    The number of odd elements: 5
    2
    The original list is : [3, 5, 1, 6, 7, 9]
    The number of odd elements: 5
    3
    The original list is : [3, 5, 1, 6, 7, 9]
    The number of odd elements: 5
    4

    The original list is : [3, 5, 1, 6, 7, 9]
    The number of odd elements: 5
    5
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'len([x for x in L if x==1])'
    10000 loops, best of 3: 56.6 usec per loop
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'sum(x==1 for x in L)'
    10000 loops, best of 3: 87.7 usec per loop
    
    3
    The original list is : [3, 5, 1, 6, 7, 9]
    The number of odd elements: 5
    7
    The original list is : [3, 5, 1, 6, 7, 9]
    The number of odd elements: 5
    8False4
    The original list is : [3, 5, 1, 6, 7, 9]
    The number of odd elements: 5
    8__

    The original list is : [3, 5, 1, 6, 7, 9]
    The number of odd elements: 5
    7 x.b == 13
    The original list is : [3, 5, 1, 6, 7, 9]
    The number of odd elements: 5
    2
    The original list is : [3, 5, 1, 6, 7, 9]
    The number of odd elements: 5
    3x.b == 16

    Đầu ra:

    The original list is : [3, 5, 1, 6, 7, 9]
    The number of odd elements: 5

    Phương pháp số 3: Sử dụng Chức năng giảm giảm () + lambda thực hiện nhiệm vụ tích lũy làm hàm tổng trong các phương thức được sử dụng ở trên. Lambda được sử dụng để thực hiện điều kiện mà kết quả cần phải được kiểm tra. & nbsp; & nbsp; reduce function does the task of accumulation as the sum function in the above used methods. lambda is used to perform the condition against which result needs to be checked. 
     

    Python3

    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'len([x for x in L if x==1])'
    10000 loops, best of 3: 56.6 usec per loop
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'sum(x==1 for x in L)'
    10000 loops, best of 3: 87.7 usec per loop
    
    2
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'len([x for x in L if x==1])'
    10000 loops, best of 3: 56.6 usec per loop
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'sum(x==1 for x in L)'
    10000 loops, best of 3: 87.7 usec per loop
    
    3
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'len([x for x in L if x==1])'
    10000 loops, best of 3: 56.6 usec per loop
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'sum(x==1 for x in L)'
    10000 loops, best of 3: 87.7 usec per loop
    
    4
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'len([x for x in L if x==1])'
    10000 loops, best of 3: 56.6 usec per loop
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'sum(x==1 for x in L)'
    10000 loops, best of 3: 87.7 usec per loop
    
    5
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'len([x for x in L if x==1])'
    10000 loops, best of 3: 56.6 usec per loop
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'sum(x==1 for x in L)'
    10000 loops, best of 3: 87.7 usec per loop
    
    6
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'len([x for x in L if x==1])'
    10000 loops, best of 3: 56.6 usec per loop
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'sum(x==1 for x in L)'
    10000 loops, best of 3: 87.7 usec per loop
    
    7
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'len([x for x in L if x==1])'
    10000 loops, best of 3: 56.6 usec per loop
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'sum(x==1 for x in L)'
    10000 loops, best of 3: 87.7 usec per loop
    
    61
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'len([x for x in L if x==1])'
    10000 loops, best of 3: 56.6 usec per loop
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'sum(x==1 for x in L)'
    10000 loops, best of 3: 87.7 usec per loop
    
    6
    The original list is : [3, 5, 1, 6, 7, 9]
    The number of odd elements: 5
    1__

    The original list is : [3, 5, 1, 6, 7, 9]
    The number of odd elements: 5
    7
    The original list is : [3, 5, 1, 6, 7, 9]
    The number of odd elements: 5
    8
    The original list is : [3, 5, 1, 6, 7, 9]
    The number of odd elements: 5
    9
    The original list is : [3, 5, 1, 6, 7, 9]
    The number of odd elements: 5
    0
    The original list is : [3, 5, 1, 6, 7, 9]
    The number of odd elements: 5
    1
    The original list is : [3, 5, 1, 6, 7, 9]
    The number of odd elements: 5
    2
    The original list is : [3, 5, 1, 6, 7, 9]
    The number of odd elements: 5
    3
    The original list is : [3, 5, 1, 6, 7, 9]
    The number of odd elements: 5
    4

    The original list is : [3, 5, 1, 6, 7, 9]
    The number of odd elements: 5
    5
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'len([x for x in L if x==1])'
    10000 loops, best of 3: 56.6 usec per loop
    $ py26 -mtimeit -s'L=[1,2,1,3,1]*100' 'sum(x==1 for x in L)'
    10000 loops, best of 3: 87.7 usec per loop
    
    3
    The original list is : [3, 5, 1, 6, 7, 9]
    The number of odd elements: 5
    7
    The original list is : [3, 5, 1, 6, 7, 9]
    The number of odd elements: 5
    8False4
    The original list is : [3, 5, 1, 6, 7, 9]
    The number of odd elements: 5
    8__

    The original list is : [3, 5, 1, 6, 7, 9]
    The number of odd elements: 5
    7 x.b == 13
    The original list is : [3, 5, 1, 6, 7, 9]
    The number of odd elements: 5
    2
    The original list is : [3, 5, 1, 6, 7, 9]
    The number of odd elements: 5
    3x.b == 16

    Đầu ra:

    The original list is : [3, 5, 1, 6, 7, 9]
    The number of odd elements: 5