Bạn có thể làm điều này trong O [n] bằng cách sử dụng
>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
1:In [3]: from collections import defaultdict
In [4]: d = defaultdict[list]
In [5]: for num in A:
...: if num < 0:
...: d['neg'].append[num]
...: else: # This will also append zero to the positive list, you can change the behavior by modifying the conditions
...: d['pos'].append[num]
...:
In [6]: d
Out[6]: defaultdict[, {'neg': [-3, -2, -5, -7], 'pos': [1, 8, 4, 6]}]
Một cách khác là sử dụng hai bộ hiểu danh sách riêng biệt [không được khuyến nghị cho danh sách dài]:
>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
Hoặc như một cách tiếp cận hoàn toàn chức năng, bạn cũng có thể sử dụng
>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
2 như sau:In [19]: list[filter[[0].__lt__,A]]
Out[19]: [1, 8, 4, 6]
In [20]: list[filter[[0].__gt__,A]]
Out[20]: [-3, -2, -5, -7]
Đưa ra một danh sách các số, hãy viết một chương trình Python để đếm các số dương và âm trong danh sách. & NBSP;
Example:
Input: list1 = [2, -7, 5, -64, -14] Output: pos = 2, neg = 3 Input: list2 = [-12, 14, 95, 3] Output: pos = 3, neg = 1
Ví dụ #1: Đếm các số dương và số âm từ danh sách đã cho bằng cách sử dụng vòng lặp lặp từng phần tử trong danh sách bằng cách sử dụng vòng lặp và kiểm tra Num> = 0, điều kiện để kiểm tra các số dương. Nếu điều kiện thỏa mãn, thì hãy tăng POS_Count khác tăng NEG_COUNT. & NBSP; Count positive and negative numbers from given list using for loop Iterate each element in the list using for loop and check if num >= 0, the condition to check positive numbers. If the condition satisfies, then increase pos_count else increase neg_count.
Python3
>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
3>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
4 >>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
5>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
6>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
7>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
8__19Input: list1 = [2, -7, 5, -64, -14] Output: pos = 2, neg = 3 Input: list2 = [-12, 14, 95, 3] Output: pos = 3, neg = 13
>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
4 Input: list1 = [2, -7, 5, -64, -14] Output: pos = 2, neg = 3 Input: list2 = [-12, 14, 95, 3] Output: pos = 3, neg = 15
>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
7Input: list1 = [2, -7, 5, -64, -14] Output: pos = 2, neg = 3 Input: list2 = [-12, 14, 95, 3] Output: pos = 3, neg = 15
Input: list1 = [2, -7, 5, -64, -14] Output: pos = 2, neg = 3 Input: list2 = [-12, 14, 95, 3] Output: pos = 3, neg = 18
Input: list1 = [2, -7, 5, -64, -14] Output: pos = 2, neg = 3 Input: list2 = [-12, 14, 95, 3] Output: pos = 3, neg = 19
Positive numbers in the list: 4 Negative numbers in the list: 30
Positive numbers in the list: 4 Negative numbers in the list: 31
Positive numbers in the list: 4 Negative numbers in the list: 32
Positive numbers in the list: 4 Negative numbers in the list: 33
Positive numbers in the list: 4 Negative numbers in the list: 34
>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
4 Input: list1 = [2, -7, 5, -64, -14] Output: pos = 2, neg = 3 Input: list2 = [-12, 14, 95, 3] Output: pos = 3, neg = 15
Positive numbers in the list: 4 Negative numbers in the list: 37
Positive numbers in the list: 4 Negative numbers in the list: 38
Positive numbers in the list: 4 Negative numbers in the list: 39
Positive numbers in the list: 2 Negative numbers in the list: 50
>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
4 Input: list1 = [2, -7, 5, -64, -14] Output: pos = 2, neg = 3 Input: list2 = [-12, 14, 95, 3] Output: pos = 3, neg = 11
Positive numbers in the list: 4 Negative numbers in the list: 32
Positive numbers in the list: 2 Negative numbers in the list: 54
Positive numbers in the list: 4 Negative numbers in the list: 37
Positive numbers in the list: 4 Negative numbers in the list: 38
Positive numbers in the list: 2 Negative numbers in the list: 57
Positive numbers in the list: 2 Negative numbers in the list: 50
>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
4 Input: list1 = [2, -7, 5, -64, -14] Output: pos = 2, neg = 3 Input: list2 = [-12, 14, 95, 3] Output: pos = 3, neg = 11
Positive numbers in the list: 4 Negative numbers in the list: 31
Positive numbers in the list: 4 Negative numbers in the list: 32
Positive numbers in the list: 4 Negative numbers in the list: 33
Positive numbers in the list: 4 Negative numbers in the list: 34
Positive numbers in the list: 4 Negative numbers in the list: 31
Positive numbers in the list: 4 Negative numbers in the list: 32
Positive numbers in the list: 4 Negative numbers in the list: 37
Positive numbers in the list: 4 Negative numbers in the list: 38
Output:
Positive numbers in the list: 4 Negative numbers in the list: 3
& nbsp; Ví dụ #2: Sử dụng trong khi Loop & NBSP;Example #2: Using while loop
Python3
>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
3>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
4 >>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
5>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
8>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
6>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
7>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
8__Input: list1 = [2, -7, 5, -64, -14] Output: pos = 2, neg = 3 Input: list2 = [-12, 14, 95, 3] Output: pos = 3, neg = 13
>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
4 Input: list1 = [2, -7, 5, -64, -14] Output: pos = 2, neg = 3 Input: list2 = [-12, 14, 95, 3] Output: pos = 3, neg = 15
>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
7Input: list1 = [2, -7, 5, -64, -14] Output: pos = 2, neg = 3 Input: list2 = [-12, 14, 95, 3] Output: pos = 3, neg = 15
Input: list1 = [2, -7, 5, -64, -14] Output: pos = 2, neg = 3 Input: list2 = [-12, 14, 95, 3] Output: pos = 3, neg = 18
Input: list1 = [2, -7, 5, -64, -14] Output: pos = 2, neg = 3 Input: list2 = [-12, 14, 95, 3] Output: pos = 3, neg = 19
Positive numbers in the list: 4 Negative numbers in the list: 30
Positive numbers in the list: 4 Negative numbers in the list: 31
Positive numbers in the list: 4 Negative numbers in the list: 32
Positive numbers in the list: 4 Negative numbers in the list: 33
Positive numbers in the list: 4 Negative numbers in the list: 34
>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
4 Input: list1 = [2, -7, 5, -64, -14] Output: pos = 2, neg = 3 Input: list2 = [-12, 14, 95, 3] Output: pos = 3, neg = 15
Positive numbers in the list: 4 Negative numbers in the list: 37
Positive numbers in the list: 4 Negative numbers in the list: 38
Positive numbers in the list: 4 Negative numbers in the list: 39
Positive numbers in the list: 2 Negative numbers in the list: 50
>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
4 Input: list1 = [2, -7, 5, -64, -14] Output: pos = 2, neg = 3 Input: list2 = [-12, 14, 95, 3] Output: pos = 3, neg = 11
Positive numbers in the list: 4 Negative numbers in the list: 38
Positive numbers in the list: 4 Negative numbers in the list: 39
Positive numbers in the list: 2 Negative numbers in the list: 50
>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
4 Input: list1 = [2, -7, 5, -64, -14] Output: pos = 2, neg = 3 Input: list2 = [-12, 14, 95, 3] Output: pos = 3, neg = 11
Positive numbers in the list: 4 Negative numbers in the list: 32
Positive numbers in the list: 2 Negative numbers in the list: 54
Positive numbers in the list: 4 Negative numbers in the list: 37
Positive numbers in the list: 4 Negative numbers in the list: 38
Positive numbers in the list: 2 Negative numbers in the list: 57
Positive numbers in the list: 2 Negative numbers in the list: 50
>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
4 Input: list1 = [2, -7, 5, -64, -14] Output: pos = 2, neg = 3 Input: list2 = [-12, 14, 95, 3] Output: pos = 3, neg = 11
Positive numbers in the list: 4 Negative numbers in the list: 31
Positive numbers in the list: 4 Negative numbers in the list: 32
Positive numbers in the list: 4 Negative numbers in the list: 33
Positive numbers in the list: 4 Negative numbers in the list: 34
Positive numbers in the list: 4 Negative numbers in the list: 31
Positive numbers in the list: 4 Negative numbers in the list: 32
Positive numbers in the list: 4 Negative numbers in the list: 33
Positive numbers in the list: 4 Negative numbers in the list: 34
Positive numbers in the list: 4 Negative numbers in the list: 31
Positive numbers in the list: 4 Negative numbers in the list: 32
Positive numbers in the list: 4 Negative numbers in the list: 37
Positive numbers in the list: 4 Negative numbers in the list: 38
Output:
Positive numbers in the list: 2 Negative numbers in the list: 5
& nbsp; Ví dụ #2: Sử dụng trong khi Loop & NBSP; Using Python Lambda Expressions
Python3
>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
3>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
4 >>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
5>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
8>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
6>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
7>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
8__Input: list1 = [2, -7, 5, -64, -14] Output: pos = 2, neg = 3 Input: list2 = [-12, 14, 95, 3] Output: pos = 3, neg = 19
>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
4 Input: list1 = [2, -7, 5, -64, -14] Output: pos = 2, neg = 3 Input: list2 = [-12, 14, 95, 3] Output: pos = 3, neg = 15
Positive numbers in the list: 4 Negative numbers in the list: 39
>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
00>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
01>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
02Positive numbers in the list: 4 Negative numbers in the list: 31
Positive numbers in the list: 4 Negative numbers in the list: 32
Positive numbers in the list: 4 Negative numbers in the list: 33
Positive numbers in the list: 4 Negative numbers in the list: 34
Positive numbers in the list: 4 Negative numbers in the list: 31
Positive numbers in the list: 4 Negative numbers in the list: 32
Positive numbers in the list: 4 Negative numbers in the list: 37
Positive numbers in the list: 4 Negative numbers in the list: 38
Output:
Positive numbers in the list: 4 Negative numbers in the list: 3
Positive numbers in the list: 4
Negative numbers in the list: 3
2Positive numbers in the list: 4
Negative numbers in the list: 3
3
05>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
4 >>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
Input: list1 = [2, -7, 5, -64, -14]
Output: pos = 2, neg = 3
Input: list2 = [-12, 14, 95, 3]
Output: pos = 3, neg = 1
5Positive numbers in the list: 4
Negative numbers in the list: 3
7Using List Comprehension
Python3
Positive numbers in the list: 4 Negative numbers in the list: 32
Input: list1 = [2, -7, 5, -64, -14] Output: pos = 2, neg = 3 Input: list2 = [-12, 14, 95, 3] Output: pos = 3, neg = 19
Positive numbers in the list: 2 Negative numbers in the list: 50
>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
4 Input: list1 = [2, -7, 5, -64, -14] Output: pos = 2, neg = 3 Input: list2 = [-12, 14, 95, 3] Output: pos = 3, neg = 11
Ví dụ #3: Sử dụng Python Lambda Expressions & NBSP;
>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
3>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
4 >>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
5>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
6>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
7>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
8__19Positive numbers in the list: 4 Negative numbers in the list: 31
Positive numbers in the list: 4 Negative numbers in the list: 32
Positive numbers in the list: 4 Negative numbers in the list: 33
Positive numbers in the list: 4 Negative numbers in the list: 34
Is
Output:
Positive numbers in the list: 4 Negative numbers in the list: 39
>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
4 >>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
01Positive numbers in the list: 4 Negative numbers in the list: 32
>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
59__62Ví dụ #4: Sử dụng danh sách hiểu & nbsp;
Python3
In [19]: list[filter[[0].__lt__,A]]
Out[19]: [1, 8, 4, 6]
In [20]: list[filter[[0].__gt__,A]]
Out[20]: [-3, -2, -5, -7]
39>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
4>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
5In [19]: list[filter[[0].__lt__,A]]
Out[19]: [1, 8, 4, 6]
In [20]: list[filter[[0].__gt__,A]]
Out[20]: [-3, -2, -5, -7]
42>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
7>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
8In [19]: list[filter[[0].__lt__,A]]
Out[19]: [1, 8, 4, 6]
In [20]: list[filter[[0].__gt__,A]]
Out[20]: [-3, -2, -5, -7]
45>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
7In [19]: list[filter[[0].__lt__,A]]
Out[19]: [1, 8, 4, 6]
In [20]: list[filter[[0].__gt__,A]]
Out[20]: [-3, -2, -5, -7]
47>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
7In [19]: list[filter[[0].__lt__,A]]
Out[19]: [1, 8, 4, 6]
In [20]: list[filter[[0].__gt__,A]]
Out[20]: [-3, -2, -5, -7]
49>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
7>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
8In [19]: list[filter[[0].__lt__,A]]
Out[19]: [1, 8, 4, 6]
In [20]: list[filter[[0].__gt__,A]]
Out[20]: [-3, -2, -5, -7]
52In [19]: list[filter[[0].__lt__,A]]
Out[19]: [1, 8, 4, 6]
In [20]: list[filter[[0].__gt__,A]]
Out[20]: [-3, -2, -5, -7]
53>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
4Input: list1 = [2, -7, 5, -64, -14] Output: pos = 2, neg = 3 Input: list2 = [-12, 14, 95, 3] Output: pos = 3, neg = 15
>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
3>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
4 >>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
5>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
8>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
6>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
7>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
8__Positive numbers in the list: 4 Negative numbers in the list: 31
Positive numbers in the list: 4 Negative numbers in the list: 32
In [19]: list[filter[[0].__lt__,A]]
Out[19]: [1, 8, 4, 6]
In [20]: list[filter[[0].__gt__,A]]
Out[20]: [-3, -2, -5, -7]
71>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
7>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
01In [19]: list[filter[[0].__lt__,A]]
Out[19]: [1, 8, 4, 6]
In [20]: list[filter[[0].__gt__,A]]
Out[20]: [-3, -2, -5, -7]
74Positive numbers in the list: 4 Negative numbers in the list: 31
Positive numbers in the list: 4 Negative numbers in the list: 32
In [19]: list[filter[[0].__lt__,A]]
Out[19]: [1, 8, 4, 6]
In [20]: list[filter[[0].__gt__,A]]
Out[20]: [-3, -2, -5, -7]
77>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
7>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
01In [19]: list[filter[[0].__lt__,A]]
Out[19]: [1, 8, 4, 6]
In [20]: list[filter[[0].__gt__,A]]
Out[20]: [-3, -2, -5, -7]
80>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
8>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
01In [19]: list[filter[[0].__lt__,A]]
Out[19]: [1, 8, 4, 6]
In [20]: list[filter[[0].__gt__,A]]
Out[20]: [-3, -2, -5, -7]
74
11In [19]: list[filter[[0].__lt__,A]]
Out[19]: [1, 8, 4, 6]
In [20]: list[filter[[0].__gt__,A]]
Out[20]: [-3, -2, -5, -7]
4 >>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
13In [19]: list[filter[[0].__lt__,A]]
Out[19]: [1, 8, 4, 6]
In [20]: list[filter[[0].__gt__,A]]
Out[20]: [-3, -2, -5, -7]
Input: list1 = [2, -7, 5, -64, -14]
Output: pos = 2, neg = 3
Input: list2 = [-12, 14, 95, 3]
Output: pos = 3, neg = 1
8 Input: list1 = [2, -7, 5, -64, -14]
Output: pos = 2, neg = 3
Input: list2 = [-12, 14, 95, 3]
Output: pos = 3, neg = 1
9Positive numbers in the list: 4
Negative numbers in the list: 3
0
3__>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
Length of Positive numbers is: 3 Length of Nagetive numbers is: 2
Positive numbers in the list: 4
Negative numbers in the list: 3
9>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
4 >>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
01In [19]: list[filter[[0].__lt__,A]]
Out[19]: [1, 8, 4, 6]
In [20]: list[filter[[0].__gt__,A]]
Out[20]: [-3, -2, -5, -7]
26
>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
In [19]: list[filter[[0].__lt__,A]]
Out[19]: [1, 8, 4, 6]
In [20]: list[filter[[0].__gt__,A]]
Out[20]: [-3, -2, -5, -7]
Python3
Positive numbers in the list: 4 Negative numbers in the list: 31
Positive numbers in the list: 4 Negative numbers in the list: 32
Positive numbers in the list: 4 Negative numbers in the list: 37
>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
7>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
01In [19]: list[filter[[0].__lt__,A]]
Out[19]: [1, 8, 4, 6]
In [20]: list[filter[[0].__gt__,A]]
Out[20]: [-3, -2, -5, -7]
36>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
8 In [19]: list[filter[[0].__lt__,A]]
Out[19]: [1, 8, 4, 6]
In [20]: list[filter[[0].__gt__,A]]
Out[20]: [-3, -2, -5, -7]
38Input: list1 = [2, -7, 5, -64, -14] Output: pos = 2, neg = 3 Input: list2 = [-12, 14, 95, 3] Output: pos = 3, neg = 13
>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
4 Input: list1 = [2, -7, 5, -64, -14] Output: pos = 2, neg = 3 Input: list2 = [-12, 14, 95, 3] Output: pos = 3, neg = 15
>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
7Input: list1 = [2, -7, 5, -64, -14] Output: pos = 2, neg = 3 Input: list2 = [-12, 14, 95, 3] Output: pos = 3, neg = 15
Input: list1 = [2, -7, 5, -64, -14] Output: pos = 2, neg = 3 Input: list2 = [-12, 14, 95, 3] Output: pos = 3, neg = 111
>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
4>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
59Positive numbers in the list: 4 Negative numbers in the list: 32
Input: list1 = [2, -7, 5, -64, -14] Output: pos = 2, neg = 3 Input: list2 = [-12, 14, 95, 3] Output: pos = 3, neg = 115
Positive numbers in the list: 4 Negative numbers in the list: 32
Input: list1 = [2, -7, 5, -64, -14] Output: pos = 2, neg = 3 Input: list2 = [-12, 14, 95, 3] Output: pos = 3, neg = 117
Input: list1 = [2, -7, 5, -64, -14] Output: pos = 2, neg = 3 Input: list2 = [-12, 14, 95, 3] Output: pos = 3, neg = 118
Input: list1 = [2, -7, 5, -64, -14] Output: pos = 2, neg = 3 Input: list2 = [-12, 14, 95, 3] Output: pos = 3, neg = 18
Input: list1 = [2, -7, 5, -64, -14] Output: pos = 2, neg = 3 Input: list2 = [-12, 14, 95, 3] Output: pos = 3, neg = 19
Positive numbers in the list: 4 Negative numbers in the list: 30
Positive numbers in the list: 4 Negative numbers in the list: 31
Positive numbers in the list: 4 Negative numbers in the list: 32
Positive numbers in the list: 4 Negative numbers in the list: 33
Positive numbers in the list: 4 Negative numbers in the list: 34
>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
4 Input: list1 = [2, -7, 5, -64, -14] Output: pos = 2, neg = 3 Input: list2 = [-12, 14, 95, 3] Output: pos = 3, neg = 15
Positive numbers in the list: 4 Negative numbers in the list: 37
Positive numbers in the list: 4 Negative numbers in the list: 38
Positive numbers in the list: 2 Negative numbers in the list: 57
Positive numbers in the list: 2 Negative numbers in the list: 50
>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
4 Input: list1 = [2, -7, 5, -64, -14] Output: pos = 2, neg = 3 Input: list2 = [-12, 14, 95, 3] Output: pos = 3, neg = 11
Positive numbers in the list: 4 Negative numbers in the list: 32
Input: list1 = [2, -7, 5, -64, -14] Output: pos = 2, neg = 3 Input: list2 = [-12, 14, 95, 3] Output: pos = 3, neg = 134
Input: list1 = [2, -7, 5, -64, -14] Output: pos = 2, neg = 3 Input: list2 = [-12, 14, 95, 3] Output: pos = 3, neg = 135
>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
4Input: list1 = [2, -7, 5, -64, -14] Output: pos = 2, neg = 3 Input: list2 = [-12, 14, 95, 3] Output: pos = 3, neg = 137
Input: list1 = [2, -7, 5, -64, -14] Output: pos = 2, neg = 3 Input: list2 = [-12, 14, 95, 3] Output: pos = 3, neg = 127
Positive numbers in the list: 4 Negative numbers in the list: 31
Positive numbers in the list: 4 Negative numbers in the list: 32
Positive numbers in the list: 4 Negative numbers in the list: 33
Positive numbers in the list: 4 Negative numbers in the list: 34
Input: list1 = [2, -7, 5, -64, -14] Output: pos = 2, neg = 3 Input: list2 = [-12, 14, 95, 3] Output: pos = 3, neg = 146
Input: list1 = [2, -7, 5, -64, -14] Output: pos = 2, neg = 3 Input: list2 = [-12, 14, 95, 3] Output: pos = 3, neg = 147
Positive numbers in the list: 2 Negative numbers in the list: 50
>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
4Input: list1 = [2, -7, 5, -64, -14] Output: pos = 2, neg = 3 Input: list2 = [-12, 14, 95, 3] Output: pos = 3, neg = 11
Positive numbers in the list: 4 Negative numbers in the list: 31
Positive numbers in the list: 4 Negative numbers in the list: 32
Positive numbers in the list: 4 Negative numbers in the list: 33
Positive numbers in the list: 4 Negative numbers in the list: 34
Positive numbers in the list: 4 Negative numbers in the list: 31
Positive numbers in the list: 4 Negative numbers in the list: 32
Positive numbers in the list: 4 Negative numbers in the list: 37
Positive numbers in the list: 4 Negative numbers in the list: 38
Đầu ra
Positive numbers in the list: 4 Negative numbers in the list: 3
Phương thức: Sử dụng phương thức Sum []
Python3
Input: list1 = [2, -7, 5, -64, -14] Output: pos = 2, neg = 3 Input: list2 = [-12, 14, 95, 3] Output: pos = 3, neg = 159
>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
4 >>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
5In [19]: list[filter[[0].__lt__,A]]
Out[19]: [1, 8, 4, 6]
In [20]: list[filter[[0].__gt__,A]]
Out[20]: [-3, -2, -5, -7]
42>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
7>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
8In [19]: list[filter[[0].__lt__,A]]
Out[19]: [1, 8, 4, 6]
In [20]: list[filter[[0].__gt__,A]]
Out[20]: [-3, -2, -5, -7]
45>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
7In [19]: list[filter[[0].__lt__,A]]
Out[19]: [1, 8, 4, 6]
In [20]: list[filter[[0].__gt__,A]]
Out[20]: [-3, -2, -5, -7]
47__17Input: list1 = [2, -7, 5, -64, -14] Output: pos = 2, neg = 3 Input: list2 = [-12, 14, 95, 3] Output: pos = 3, neg = 174
>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
4 Input: list1 = [2, -7, 5, -64, -14] Output: pos = 2, neg = 3 Input: list2 = [-12, 14, 95, 3] Output: pos = 3, neg = 176
Positive numbers in the list: 4 Negative numbers in the list: 32
Input: list1 = [2, -7, 5, -64, -14] Output: pos = 2, neg = 3 Input: list2 = [-12, 14, 95, 3] Output: pos = 3, neg = 11
Input: list1 = [2, -7, 5, -64, -14] Output: pos = 2, neg = 3 Input: list2 = [-12, 14, 95, 3] Output: pos = 3, neg = 18
Input: list1 = [2, -7, 5, -64, -14] Output: pos = 2, neg = 3 Input: list2 = [-12, 14, 95, 3] Output: pos = 3, neg = 180__
Positive numbers in the list: 4 Negative numbers in the list: 31
Positive numbers in the list: 4 Negative numbers in the list: 32
In [19]: list[filter[[0].__lt__,A]]
Out[19]: [1, 8, 4, 6]
In [20]: list[filter[[0].__gt__,A]]
Out[20]: [-3, -2, -5, -7]
71Input: list1 = [2, -7, 5, -64, -14] Output: pos = 2, neg = 3 Input: list2 = [-12, 14, 95, 3] Output: pos = 3, neg = 191
Positive numbers in the list: 4 Negative numbers in the list: 31
Positive numbers in the list: 4 Negative numbers in the list: 32
In [19]: list[filter[[0].__lt__,A]]
Out[19]: [1, 8, 4, 6]
In [20]: list[filter[[0].__gt__,A]]
Out[20]: [-3, -2, -5, -7]
77>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
7>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
01In [19]: list[filter[[0].__lt__,A]]
Out[19]: [1, 8, 4, 6]
In [20]: list[filter[[0].__gt__,A]]
Out[20]: [-3, -2, -5, -7]
80>>> B,C=[i for i in A if i0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
8Input: list1 = [2, -7, 5, -64, -14] Output: pos = 2, neg = 3 Input: list2 = [-12, 14, 95, 3] Output: pos = 3, neg = 199
Output:
Length of Positive numbers is: 3 Length of Nagetive numbers is: 2