Hướng dẫn how to find the first even number in a list python - cách tìm số chẵn đầu tiên trong python danh sách

Nếu có một số chẵn trong danh sách, hãy trả lại số đầu tiên và nếu không có số chẵn, hãy trả về -1. Ví dụ như thế này:

>>> first_even([5, 8, 3, 2])
8
>>> first_even([7, 1])
-1

Tôi đã thử một số chức năng có khả năng trả về đầu tiên nhưng không có ý kiến ​​cho -1. Xin mọi người có thể cho tôi một lời khuyên.

Hỏi ngày 17 tháng 10 năm 2015 lúc 16:28Oct 17, 2015 at 16:28

Hướng dẫn how to find the first even number in a list python - cách tìm số chẵn đầu tiên trong python danh sách

Bạn có thể sử dụng

def first_even(lst):
    return next((e for e in lst if e%2==0),-1)
0 cho điều này -

def first_even(lst):
    return next((e for e in lst if e%2==0),-1)

Ví dụ chạy -

>>> def first_even(lst):
...     return next((e for e in lst if e%2==0),-1)
...
>>> first_even([5, 8, 3, 2])
8
>>> first_even([7, 1])
-1

Đã trả lời ngày 17 tháng 10 năm 2015 lúc 16:31Oct 17, 2015 at 16:31

Hướng dẫn how to find the first even number in a list python - cách tìm số chẵn đầu tiên trong python danh sách

4

Bạn có thể sử dụng

def first_even(lst):
    return next((e for e in lst if e%2==0),-1)
1
def first_even(lst):
    return next((e for e in lst if e%2==0),-1)
2

>>> def first_even(x):
    for i in x:
        if i%2 == 0:
            return i
    else:
        return -1
>>> first_even([5, 8, 3, 2])
8
>>> first_even([7, 1])
-1

Đã trả lời ngày 17 tháng 10 năm 2015 lúc 16:32Oct 17, 2015 at 16:32

Hướng dẫn how to find the first even number in a list python - cách tìm số chẵn đầu tiên trong python danh sách

Avinash Rajavinash RajAvinash Raj

169K25 Huy hiệu vàng215 Huy hiệu bạc262 Huy hiệu Đồng25 gold badges215 silver badges262 bronze badges

3

Phương pháp số 1: Sử dụng vòng lặp Đây là cách vũ phu trong đó nhiệm vụ này có thể được thực hiện. Trong đó, chúng tôi lặp qua danh sách và khi số lần thứ 1 xảy ra, chúng tôi lưu trữ và phá vỡ vòng lặp.

>>> first_even([5, 8, 3, 2])
8
>>> first_even([7, 1])
-1

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

Xem ví dụ này:.Oct 17, 2015 at 16:28

Hướng dẫn how to find the first even number in a list python - cách tìm số chẵn đầu tiên trong python danh sách

num = int (input ("nhập một số:")).

def first_even(lst):
    return next((e for e in lst if e%2==0),-1)

if (num % 2) == 0:.

>>> def first_even(lst):
...     return next((e for e in lst if e%2==0),-1)
...
>>> first_even([5, 8, 3, 2])
8
>>> first_even([7, 1])
-1

in ("{0} là số chẵn". Định dạng (num)).Oct 17, 2015 at 16:31

4

in ("{0} là số lẻ". Định dạng (num)).

>>> def first_even(x):
    for i in x:
        if i%2 == 0:
            return i
    else:
        return -1
>>> first_even([5, 8, 3, 2])
8
>>> first_even([7, 1])
-1

Làm thế nào để bạn chọn số thậm chí trong một danh sách trong Python?Oct 17, 2015 at 16:32

Hướng dẫn how to find the first even number in a list python - cách tìm số chẵn đầu tiên trong python danh sách

Để lọc các số thậm chí từ danh sách trong Python, hãy sử dụng hàm tích hợp Filter (). Vượt qua hàm trả về đúng cho một số chẵn và danh sách các số, làm đối số cho hàm lọc ().Avinash Raj

Làm thế nào để bạn tìm thấy yếu tố chẵn trong Python?25 gold badges215 silver badges262 bronze badges

3

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc

    Bàn luận
    This is brute force way in which this task can be performed. In this, we loop through the list and when 1st time even number occurs, we store and break the loop.

    Đôi khi, trong khi làm việc với các danh sách, chúng ta có thể gặp vấn đề trong đó chúng ta cần trích xuất một số số xảy ra lần đầu tiên. Điều này cũng có thể là số chẵn. Loại vấn đề này là phổ biến trong chương trình hàng ngày và cạnh tranh. Hãy thảo luận về những cách nhất định trong đó nhiệm vụ này có thể được thực hiện.

    Phương pháp số 1: Sử dụng loopthis là cách vũ phu trong đó nhiệm vụ này có thể được thực hiện. Trong đó, chúng tôi lặp qua danh sách và khi số lần thứ 1 xảy ra, chúng tôi lưu trữ và phá vỡ vòng lặp.

    def first_even(lst):
        return next((e for e in lst if e%2==0),-1)
    
    6
    def first_even(lst):
        return next((e for e in lst if e%2==0),-1)
    
    7
    def first_even(lst):
        return next((e for e in lst if e%2==0),-1)
    
    8
    def first_even(lst):
        return next((e for e in lst if e%2==0),-1)
    
    9
    >>> def first_even(lst):
    ...     return next((e for e in lst if e%2==0),-1)
    ...
    >>> first_even([5, 8, 3, 2])
    8
    >>> first_even([7, 1])
    -1
    
    0
    >>> def first_even(lst):
    ...     return next((e for e in lst if e%2==0),-1)
    ...
    >>> first_even([5, 8, 3, 2])
    8
    >>> first_even([7, 1])
    -1
    
    1______

    >>> def first_even(x):
        for i in x:
            if i%2 == 0:
                return i
        else:
            return -1
    >>> first_even([5, 8, 3, 2])
    8
    >>> first_even([7, 1])
    -1
    
    1
    >>> def first_even(x):
        for i in x:
            if i%2 == 0:
                return i
        else:
            return -1
    >>> first_even([5, 8, 3, 2])
    8
    >>> first_even([7, 1])
    -1
    
    2
    >>> def first_even(x):
        for i in x:
            if i%2 == 0:
                return i
        else:
            return -1
    >>> first_even([5, 8, 3, 2])
    8
    >>> first_even([7, 1])
    -1
    
    3
    >>> def first_even(x):
        for i in x:
            if i%2 == 0:
                return i
        else:
            return -1
    >>> first_even([5, 8, 3, 2])
    8
    >>> first_even([7, 1])
    -1
    
    4
    >>> def first_even(x):
        for i in x:
            if i%2 == 0:
                return i
        else:
            return -1
    >>> first_even([5, 8, 3, 2])
    8
    >>> first_even([7, 1])
    -1
    
    5
    >>> def first_even(x):
        for i in x:
            if i%2 == 0:
                return i
        else:
            return -1
    >>> first_even([5, 8, 3, 2])
    8
    >>> first_even([7, 1])
    -1
    
    6

    >>> def first_even(x):
        for i in x:
            if i%2 == 0:
                return i
        else:
            return -1
    >>> first_even([5, 8, 3, 2])
    8
    >>> first_even([7, 1])
    -1
    
    7
    def first_even(lst):
        return next((e for e in lst if e%2==0),-1)
    
    7
    >>> def first_even(x):
        for i in x:
            if i%2 == 0:
                return i
        else:
            return -1
    >>> first_even([5, 8, 3, 2])
    8
    >>> first_even([7, 1])
    -1
    
    9

    def first_even(lst):
        return next((e for e in lst if e%2==0),-1)
    
    1
    >>> first_even([5, 8, 3, 2])
    8
    >>> first_even([7, 1])
    -1
    
    1
    >>> first_even([5, 8, 3, 2])
    8
    >>> first_even([7, 1])
    -1
    
    2
    >>> first_even([5, 8, 3, 2])
    8
    >>> first_even([7, 1])
    -1
    
    3

    def first_even(lst):
        return next((e for e in lst if e%2==0),-1)
    
    1
    def first_even(lst):
        return next((e for e in lst if e%2==0),-1)
    
    6

    >>> first_even([5, 8, 3, 2])
    8
    >>> first_even([7, 1])
    -1
    
    4
    >>> first_even([5, 8, 3, 2])
    8
    >>> first_even([7, 1])
    -1
    
    5
    >>> first_even([5, 8, 3, 2])
    8
    >>> first_even([7, 1])
    -1
    
    6
    >>> first_even([5, 8, 3, 2])
    8
    >>> first_even([7, 1])
    -1
    
    1
    >>> first_even([5, 8, 3, 2])
    8
    >>> first_even([7, 1])
    -1
    
    8
    >>> first_even([5, 8, 3, 2])
    8
    >>> first_even([7, 1])
    -1
    
    9
    def first_even(lst):
        return next((e for e in lst if e%2==0),-1)
    
    0

    def first_even(lst):
        return next((e for e in lst if e%2==0),-1)
    
    1
    >>> def first_even(x):
        for i in x:
            if i%2 == 0:
                return i
        else:
            return -1
    >>> first_even([5, 8, 3, 2])
    8
    >>> first_even([7, 1])
    -1
    
    7
    def first_even(lst):
        return next((e for e in lst if e%2==0),-1)
    
    7
    def first_even(lst):
        return next((e for e in lst if e%2==0),-1)
    
    4

    The original list is : [43, 9, 6, 72, 8, 11]
    The first even element in list is : 6
    

    >>> def first_even(x):
        for i in x:
            if i%2 == 0:
                return i
        else:
            return -1
    >>> first_even([5, 8, 3, 2])
    8
    >>> first_even([7, 1])
    -1
    
    1
    >>> def first_even(x):
        for i in x:
            if i%2 == 0:
                return i
        else:
            return -1
    >>> first_even([5, 8, 3, 2])
    8
    >>> first_even([7, 1])
    -1
    
    2
    def first_even(lst):
        return next((e for e in lst if e%2==0),-1)
    
    9
    >>> def first_even(x):
        for i in x:
            if i%2 == 0:
                return i
        else:
            return -1
    >>> first_even([5, 8, 3, 2])
    8
    >>> first_even([7, 1])
    -1
    
    4
    >>> def first_even(x):
        for i in x:
            if i%2 == 0:
                return i
        else:
            return -1
    >>> first_even([5, 8, 3, 2])
    8
    >>> first_even([7, 1])
    -1
    
    5
    >>> def first_even(lst):
    ...     return next((e for e in lst if e%2==0),-1)
    ...
    >>> first_even([5, 8, 3, 2])
    8
    >>> first_even([7, 1])
    -1
    
    2

    The binary search can also be employed on list when we have sorted list. In this, we keep looking for smaller even term when we find one and move to rear end in case we don’t find one.

    Đầu ra:

    >>> def first_even(x):
        for i in x:
            if i%2 == 0:
                return i
        else:
            return -1
    >>> first_even([5, 8, 3, 2])
    8
    >>> first_even([7, 1])
    -1
    
    1
    >>> def first_even(x):
        for i in x:
            if i%2 == 0:
                return i
        else:
            return -1
    >>> first_even([5, 8, 3, 2])
    8
    >>> first_even([7, 1])
    -1
    
    2
    >>> def first_even(x):
        for i in x:
            if i%2 == 0:
                return i
        else:
            return -1
    >>> first_even([5, 8, 3, 2])
    8
    >>> first_even([7, 1])
    -1
    
    3
    >>> def first_even(x):
        for i in x:
            if i%2 == 0:
                return i
        else:
            return -1
    >>> first_even([5, 8, 3, 2])
    8
    >>> first_even([7, 1])
    -1
    
    4
    >>> def first_even(x):
        for i in x:
            if i%2 == 0:
                return i
        else:
            return -1
    >>> first_even([5, 8, 3, 2])
    8
    >>> first_even([7, 1])
    -1
    
    5
    >>> def first_even(x):
        for i in x:
            if i%2 == 0:
                return i
        else:
            return -1
    >>> first_even([5, 8, 3, 2])
    8
    >>> first_even([7, 1])
    -1
    
    6

    >>> def first_even(x):
        for i in x:
            if i%2 == 0:
                return i
        else:
            return -1
    >>> first_even([5, 8, 3, 2])
    8
    >>> first_even([7, 1])
    -1
    
    7
    def first_even(lst):
        return next((e for e in lst if e%2==0),-1)
    
    7
    The original list is : [43, 9, 6, 72, 8, 11]
    The first even element in list is : 6
    
    6
    The original list is : [43, 9, 6, 72, 8, 11]
    The first even element in list is : 6
    
    7

    The original list is : [43, 9, 6, 72, 8, 11]
    The first even element in list is : 6
    
    8
    def first_even(lst):
        return next((e for e in lst if e%2==0),-1)
    
    7
    The original list is : [3, 7, 8, 9, 10, 15]
    The first even element in list is : 8
    
    0

    The original list is : [3, 7, 8, 9, 10, 15]
    The first even element in list is : 8
    
    1
    def first_even(lst):
        return next((e for e in lst if e%2==0),-1)
    
    7
    The original list is : [43, 9, 6, 72, 8, 11]
    The first even element in list is : 6
    
    6
    The original list is : [3, 7, 8, 9, 10, 15]
    The first even element in list is : 8
    
    4
    The original list is : [3, 7, 8, 9, 10, 15]
    The first even element in list is : 8
    
    5
    The original list is : [3, 7, 8, 9, 10, 15]
    The first even element in list is : 8
    
    6

    The original list is : [3, 7, 8, 9, 10, 15]
    The first even element in list is : 8
    
    7
    The original list is : [3, 7, 8, 9, 10, 15]
    The first even element in list is : 8
    
    8
    def first_even(lst):
        return next((e for e in lst if e%2==0),-1)
    
    7
    def first_even(lst):
        return next((e for e in lst if e%2==0),-1)
    
    00

    >>> first_even([5, 8, 3, 2])
    8
    >>> first_even([7, 1])
    -1
    
    4
    def first_even(lst):
        return next((e for e in lst if e%2==0),-1)
    
    022

    >>> first_even([5, 8, 3, 2])
    8
    >>> first_even([7, 1])
    -1
    
    4
    >>> first_even([5, 8, 3, 2])
    8
    >>> first_even([7, 1])
    -1
    
    5
    def first_even(lst):
        return next((e for e in lst if e%2==0),-1)
    
    12
    >>> first_even([5, 8, 3, 2])
    8
    >>> first_even([7, 1])
    -1
    
    8
    >>> first_even([5, 8, 3, 2])
    8
    >>> first_even([7, 1])
    -1
    
    9
    def first_even(lst):
        return next((e for e in lst if e%2==0),-1)
    
    0

    def first_even(lst):
        return next((e for e in lst if e%2==0),-1)
    
    1
    The original list is : [43, 9, 6, 72, 8, 11]
    The first even element in list is : 6
    
    8
    def first_even(lst):
        return next((e for e in lst if e%2==0),-1)
    
    7
    def first_even(lst):
        return next((e for e in lst if e%2==0),-1)
    
    02
    >>> def first_even(x):
        for i in x:
            if i%2 == 0:
                return i
        else:
            return -1
    >>> first_even([5, 8, 3, 2])
    8
    >>> first_even([7, 1])
    -1
    
    4
    The original list is : [3, 7, 8, 9, 10, 15]
    The first even element in list is : 8
    
    6

    >>> first_even([5, 8, 3, 2])
    8
    >>> first_even([7, 1])
    -1
    
    4
    def first_even(lst):
        return next((e for e in lst if e%2==0),-1)
    
    2
    def first_even(lst):
        return next((e for e in lst if e%2==0),-1)
    
    0

    def first_even(lst):
        return next((e for e in lst if e%2==0),-1)
    
    1
    >>> def first_even(x):
        for i in x:
            if i%2 == 0:
                return i
        else:
            return -1
    >>> first_even([5, 8, 3, 2])
    8
    >>> first_even([7, 1])
    -1
    
    7
    def first_even(lst):
        return next((e for e in lst if e%2==0),-1)
    
    7
    def first_even(lst):
        return next((e for e in lst if e%2==0),-1)
    
    28

    def first_even(lst):
        return next((e for e in lst if e%2==0),-1)
    
    1
    The original list is : [3, 7, 8, 9, 10, 15]
    The first even element in list is : 8
    
    1
    def first_even(lst):
        return next((e for e in lst if e%2==0),-1)
    
    7
    def first_even(lst):
        return next((e for e in lst if e%2==0),-1)
    
    02
    The original list is : [3, 7, 8, 9, 10, 15]
    The first even element in list is : 8
    
    5
    The original list is : [3, 7, 8, 9, 10, 15]
    The first even element in list is : 8
    
    6

    >>> def first_even(x):
        for i in x:
            if i%2 == 0:
                return i
        else:
            return -1
    >>> first_even([5, 8, 3, 2])
    8
    >>> first_even([7, 1])
    -1
    
    1
    >>> def first_even(x):
        for i in x:
            if i%2 == 0:
                return i
        else:
            return -1
    >>> first_even([5, 8, 3, 2])
    8
    >>> first_even([7, 1])
    -1
    
    2
    def first_even(lst):
        return next((e for e in lst if e%2==0),-1)
    
    9
    >>> def first_even(x):
        for i in x:
            if i%2 == 0:
                return i
        else:
            return -1
    >>> first_even([5, 8, 3, 2])
    8
    >>> first_even([7, 1])
    -1
    
    4
    >>> def first_even(x):
        for i in x:
            if i%2 == 0:
                return i
        else:
            return -1
    >>> first_even([5, 8, 3, 2])
    8
    >>> first_even([7, 1])
    -1
    
    5
    >>> def first_even(lst):
    ...     return next((e for e in lst if e%2==0),-1)
    ...
    >>> first_even([5, 8, 3, 2])
    8
    >>> first_even([7, 1])
    -1
    
    2

    Đầu ra:

    The original list is : [3, 7, 8, 9, 10, 15]
    The first even element in list is : 8
    


    Làm thế nào để bạn tìm thấy số chẵn đầu tiên trong Python?

    Phương pháp số 1: Sử dụng vòng lặp Đây là cách vũ phu trong đó nhiệm vụ này có thể được thực hiện. Trong đó, chúng tôi lặp qua danh sách và khi số lần thứ 1 xảy ra, chúng tôi lưu trữ và phá vỡ vòng lặp.Using loop This is brute force way in which this task can be performed. In this, we loop through the list and when 1st time even number occurs, we store and break the loop.

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

    Xem ví dụ này:..
    num = int (input ("nhập một số:")).
    if (num % 2) == 0:.
    in ("{0} là số chẵn". Định dạng (num)).
    in ("{0} là số lẻ". Định dạng (num)).

    Làm thế nào để bạn chọn số thậm chí trong một danh sách trong Python?

    Để lọc các số thậm chí từ danh sách trong Python, hãy sử dụng hàm tích hợp Filter ().Vượt qua hàm trả về đúng cho một số chẵn và danh sách các số, làm đối số cho hàm lọc ().use filter() builtin function. Pass the function that returns True for an even number, and the list of numbers, as arguments to filter() function.

    Làm thế nào để bạn tìm thấy yếu tố chẵn trong Python?

    Bước 1: Tạo danh sách đầu vào của người dùng.Bước 2: Lấy hai danh sách trống một cho ODD và một cho chẵn.Bước 3: Sau đó, qua mỗi phần tử trong danh sách chính.Bước 4: Mỗi phần tử được chia cho 2, nếu phần còn lại là 0 thì nó thậm chí là số và thêm vào danh sách chẵn, nếu không thì số lẻ của nó và thêm vào danh sách lẻ.