Hướng dẫn how to separate negative and positive numbers in python - cách tách số âm và số dương trong python

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 i<0 ],[j for j in A if j>0]
>>> 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 i<0 ],[j for j in A if j>0]
>>> 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 i<0 ],[j for j in A if j>0]
>>> 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 i<0 ],[j for j in A if j>0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
3
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
4
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
5
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
6
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
7
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
8__19

Input: list1 = [2, -7, 5, -64, -14]
Output: pos = 2, neg = 3

Input: list2 = [-12, 14, 95, 3]
Output: pos = 3, neg = 1
3
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> 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 = 1
5
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
7
Input: list1 = [2, -7, 5, -64, -14]
Output: pos = 2, neg = 3

Input: list2 = [-12, 14, 95, 3]
Output: pos = 3, neg = 1
5

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
9
Positive numbers in the list:  4
Negative numbers in the list:  3
0
Positive numbers in the list:  4
Negative numbers in the list:  3
1

Positive numbers in the list:  4
Negative numbers in the list:  3
2
Positive numbers in the list:  4
Negative numbers in the list:  3
3
Positive numbers in the list:  4
Negative numbers in the list:  3
4
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> 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 = 1
5
Positive numbers in the list:  4
Negative numbers in the list:  3
7

Positive numbers in the list:  4
Negative numbers in the list:  3
8
Positive numbers in the list:  4
Negative numbers in the list:  3
9
Positive numbers in the list:  2
Negative numbers in the list:  5
0
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> 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 = 1
1

Positive numbers in the list:  4
Negative numbers in the list:  3
2
Positive numbers in the list:  2
Negative numbers in the list:  5
4
Positive numbers in the list:  4
Negative numbers in the list:  3
7

Positive numbers in the list:  4
Negative numbers in the list:  3
8
Positive numbers in the list:  2
Negative numbers in the list:  5
7
Positive numbers in the list:  2
Negative numbers in the list:  5
0
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> 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 = 1
1

Positive numbers in the list:  4
Negative numbers in the list:  3
1
Positive numbers in the list:  4
Negative numbers in the list:  3
2
Positive numbers in the list:  4
Negative numbers in the list:  3
3
Positive numbers in the list:  4
Negative numbers in the list:  3
4

Positive numbers in the list:  4
Negative numbers in the list:  3
1
Positive numbers in the list:  4
Negative numbers in the list:  3
2
Positive numbers in the list:  4
Negative numbers in the list:  3
7
Positive numbers in the list:  4
Negative numbers in the list:  3
8

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 i<0 ],[j for j in A if j>0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
3
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
4
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
5
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
8
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
6
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
7
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> 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 = 1
3
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> 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 = 1
5
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
7
Input: list1 = [2, -7, 5, -64, -14]
Output: pos = 2, neg = 3

Input: list2 = [-12, 14, 95, 3]
Output: pos = 3, neg = 1
5

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
9
Positive numbers in the list:  4
Negative numbers in the list:  3
0
Positive numbers in the list:  4
Negative numbers in the list:  3
1

Positive numbers in the list:  4
Negative numbers in the list:  3
2
Positive numbers in the list:  4
Negative numbers in the list:  3
3
Positive numbers in the list:  4
Negative numbers in the list:  3
4
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> 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 = 1
5
Positive numbers in the list:  4
Negative numbers in the list:  3
7

Positive numbers in the list:  4
Negative numbers in the list:  3
8
Positive numbers in the list:  4
Negative numbers in the list:  3
9
Positive numbers in the list:  2
Negative numbers in the list:  5
0
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> 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 = 1
1

Positive numbers in the list:  4
Negative numbers in the list:  3
8
Positive numbers in the list:  4
Negative numbers in the list:  3
9
Positive numbers in the list:  2
Negative numbers in the list:  5
0
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> 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 = 1
1

Positive numbers in the list:  4
Negative numbers in the list:  3
2
Positive numbers in the list:  2
Negative numbers in the list:  5
4
Positive numbers in the list:  4
Negative numbers in the list:  3
7

Positive numbers in the list:  4
Negative numbers in the list:  3
8
Positive numbers in the list:  2
Negative numbers in the list:  5
7
Positive numbers in the list:  2
Negative numbers in the list:  5
0
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> 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 = 1
1

Positive numbers in the list:  4
Negative numbers in the list:  3
1
Positive numbers in the list:  4
Negative numbers in the list:  3
2
Positive numbers in the list:  4
Negative numbers in the list:  3
3
Positive numbers in the list:  4
Negative numbers in the list:  3
4

Positive numbers in the list:  4
Negative numbers in the list:  3
1
Positive numbers in the list:  4
Negative numbers in the list:  3
2
Positive numbers in the list:  4
Negative numbers in the list:  3
3
Positive numbers in the list:  4
Negative numbers in the list:  3
4

Positive numbers in the list:  4
Negative numbers in the list:  3
1
Positive numbers in the list:  4
Negative numbers in the list:  3
2
Positive numbers in the list:  4
Negative numbers in the list:  3
7
Positive numbers in the list:  4
Negative numbers in the list:  3
8

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 i<0 ],[j for j in A if j>0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
3
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
4
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
5
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
8
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
6
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
7
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> 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 = 1
9
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> 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 = 1
5

Positive numbers in the list:  4
Negative numbers in the list:  3
9
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
00
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
01
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
02

Positive numbers in the list:  4
Negative numbers in the list:  3
1
Positive numbers in the list:  4
Negative numbers in the list:  3
2
Positive numbers in the list:  4
Negative numbers in the list:  3
3
Positive numbers in the list:  4
Negative numbers in the list:  3
4

Positive numbers in the list:  4
Negative numbers in the list:  3
1
Positive numbers in the list:  4
Negative numbers in the list:  3
2
Positive numbers in the list:  4
Negative numbers in the list:  3
7
Positive numbers in the list:  4
Negative numbers in the list:  3
8

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
2
Positive numbers in the list:  4
Negative numbers in the list:  3
3
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
05
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> 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 = 1
5
Positive numbers in the list:  4
Negative numbers in the list:  3
7
Using List Comprehension 

Python3

Positive numbers in the list:  4
Negative numbers in the list:  3
2
Input: list1 = [2, -7, 5, -64, -14]
Output: pos = 2, neg = 3

Input: list2 = [-12, 14, 95, 3]
Output: pos = 3, neg = 1
9
Positive numbers in the list:  2
Negative numbers in the list:  5
0
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> 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 = 1
1

Ví dụ #3: Sử dụng Python Lambda Expressions & NBSP;

>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
3
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
4
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
5
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
6
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
7
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
8__19

Positive numbers in the list:  4
Negative numbers in the list:  3
1
Positive numbers in the list:  4
Negative numbers in the list:  3
2
Positive numbers in the list:  4
Negative numbers in the list:  3
3
Positive numbers in the list:  4
Negative numbers in the list:  3
4

Is

Output:

Positive numbers in the list:  4
Negative numbers in the list:  3
9
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
4
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
01
Positive numbers in the list:  4
Negative numbers in the list:  3
2
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
59__62

Ví 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 i<0 ],[j for j in A if j>0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
4
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
5
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]
42
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
7
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> 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]
45
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
7
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]
47
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
7
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]
49
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
7
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> 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]
52
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]
53
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> 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 = 1
5

>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
3
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
4
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
5
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
8
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
6
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
7
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
8__

Positive numbers in the list:  4
Negative numbers in the list:  3
1
Positive numbers in the list:  4
Negative numbers in the list:  3
2
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 i<0 ],[j for j in A if j>0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
7
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
01
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]
74

Positive numbers in the list:  4
Negative numbers in the list:  3
1
Positive numbers in the list:  4
Negative numbers in the list:  3
2
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 i<0 ],[j for j in A if j>0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
7
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
01
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]
80
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
8
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
01
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]
74

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]
11
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
4
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]
13
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
9
Positive numbers in the list:  4
Negative numbers in the list:  3
0
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
3__

Length of Positive numbers is: 3
Length of Nagetive numbers is: 2

Positive numbers in the list: 4 Negative numbers in the list: 39>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0] >>> B [-3, -2, -5, -7] >>> C [1, 8, 4, 6] 4 >>> B,C=[i for i in A if i<0 ],[j for j in A if j>0] >>> 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

Python3

Positive numbers in the list:  4
Negative numbers in the list:  3
1
Positive numbers in the list:  4
Negative numbers in the list:  3
2
Positive numbers in the list:  4
Negative numbers in the list:  3
7
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
7
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
01
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]
36
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> 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]
38

Input: list1 = [2, -7, 5, -64, -14]
Output: pos = 2, neg = 3

Input: list2 = [-12, 14, 95, 3]
Output: pos = 3, neg = 1
3
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> 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 = 1
5
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
7
Input: list1 = [2, -7, 5, -64, -14]
Output: pos = 2, neg = 3

Input: list2 = [-12, 14, 95, 3]
Output: pos = 3, neg = 1
5

Input: list1 = [2, -7, 5, -64, -14]
Output: pos = 2, neg = 3

Input: list2 = [-12, 14, 95, 3]
Output: pos = 3, neg = 1
11
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
4
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
59
Positive numbers in the list:  4
Negative numbers in the list:  3
2
Input: list1 = [2, -7, 5, -64, -14]
Output: pos = 2, neg = 3

Input: list2 = [-12, 14, 95, 3]
Output: pos = 3, neg = 1
15
Positive numbers in the list:  4
Negative numbers in the list:  3
2
Input: list1 = [2, -7, 5, -64, -14]
Output: pos = 2, neg = 3

Input: list2 = [-12, 14, 95, 3]
Output: pos = 3, neg = 1
17
Input: list1 = [2, -7, 5, -64, -14]
Output: pos = 2, neg = 3

Input: list2 = [-12, 14, 95, 3]
Output: pos = 3, neg = 1
18

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
9
Positive numbers in the list:  4
Negative numbers in the list:  3
0
Positive numbers in the list:  4
Negative numbers in the list:  3
1

Positive numbers in the list:  4
Negative numbers in the list:  3
2
Positive numbers in the list:  4
Negative numbers in the list:  3
3
Positive numbers in the list:  4
Negative numbers in the list:  3
4
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> 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 = 1
5
Positive numbers in the list:  4
Negative numbers in the list:  3
7

Positive numbers in the list:  4
Negative numbers in the list:  3
8
Positive numbers in the list:  2
Negative numbers in the list:  5
7
Positive numbers in the list:  2
Negative numbers in the list:  5
0
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> 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 = 1
1

Positive numbers in the list:  4
Negative numbers in the list:  3
2
Input: list1 = [2, -7, 5, -64, -14]
Output: pos = 2, neg = 3

Input: list2 = [-12, 14, 95, 3]
Output: pos = 3, neg = 1
34
Input: list1 = [2, -7, 5, -64, -14]
Output: pos = 2, neg = 3

Input: list2 = [-12, 14, 95, 3]
Output: pos = 3, neg = 1
35
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> 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 = 1
37
Input: list1 = [2, -7, 5, -64, -14]
Output: pos = 2, neg = 3

Input: list2 = [-12, 14, 95, 3]
Output: pos = 3, neg = 1
27

Positive numbers in the list:  4
Negative numbers in the list:  3
1
Positive numbers in the list:  4
Negative numbers in the list:  3
2
Positive numbers in the list:  4
Negative numbers in the list:  3
3
Positive numbers in the list:  4
Negative numbers in the list:  3
4

Input: list1 = [2, -7, 5, -64, -14]
Output: pos = 2, neg = 3

Input: list2 = [-12, 14, 95, 3]
Output: pos = 3, neg = 1
46
Input: list1 = [2, -7, 5, -64, -14]
Output: pos = 2, neg = 3

Input: list2 = [-12, 14, 95, 3]
Output: pos = 3, neg = 1
47
Positive numbers in the list:  2
Negative numbers in the list:  5
0
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> 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 = 1
1

Positive numbers in the list:  4
Negative numbers in the list:  3
1
Positive numbers in the list:  4
Negative numbers in the list:  3
2
Positive numbers in the list:  4
Negative numbers in the list:  3
3
Positive numbers in the list:  4
Negative numbers in the list:  3
4

Positive numbers in the list:  4
Negative numbers in the list:  3
1
Positive numbers in the list:  4
Negative numbers in the list:  3
2
Positive numbers in the list:  4
Negative numbers in the list:  3
7
Positive numbers in the list:  4
Negative numbers in the list:  3
8

Đầ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 = 1
59
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
4
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
5
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]
42
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
7
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> 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]
45
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
7
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]
47__17

Input: list1 = [2, -7, 5, -64, -14]
Output: pos = 2, neg = 3

Input: list2 = [-12, 14, 95, 3]
Output: pos = 3, neg = 1
74
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> 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 = 1
76
Positive numbers in the list:  4
Negative numbers in the list:  3
2
Input: list1 = [2, -7, 5, -64, -14]
Output: pos = 2, neg = 3

Input: list2 = [-12, 14, 95, 3]
Output: pos = 3, neg = 1
1
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
80__

Positive numbers in the list:  4
Negative numbers in the list:  3
1
Positive numbers in the list:  4
Negative numbers in the list:  3
2
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
Input: list1 = [2, -7, 5, -64, -14]
Output: pos = 2, neg = 3

Input: list2 = [-12, 14, 95, 3]
Output: pos = 3, neg = 1
91

Positive numbers in the list:  4
Negative numbers in the list:  3
1
Positive numbers in the list:  4
Negative numbers in the list:  3
2
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 i<0 ],[j for j in A if j>0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
7
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]
01
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]
80
>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> 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 = 1
99

Output:

Length of Positive numbers is: 3
Length of Nagetive numbers is: 2

Làm thế nào để bạn phân tách các số âm và tích cực trong Python?

Bên trong vòng lặp Python, chúng tôi đang sử dụng câu lệnh IF để kiểm tra xem mục danh sách là dương hay âm. Dựa trên kết quả, chúng tôi đang thêm mục đó vào danh sách tích cực hoặc danh sách tiêu cực. if (Numlist [1]> = 0) => if (-34> = 0)-điều kiện là sai. Vì vậy, nó đi vào khối khác.if(NumList[1] >= 0) => if(-34 >= 0) – Condition is False. So, it enters into the Else block.

Làm thế nào để bạn chia số âm trong Python?

Câu trả lời là Python luôn làm tròn kết quả của sự phân chia số nguyên đối với Minus Infinity, đó là số âm nhỏ nhất có thể.Điều này có nghĩa là nó kéo kết quả của phân chia số nguyên thành số nhỏ nhất có thể, 1 nhỏ hơn 1,5 nhưng -2 nhỏ hơn -1,5.Python always rounds the result of integer division towards minus infinity, which is the smallest negative number possible. This means it pulls the result of the integer division to the smallest possible number, 1 is smaller than 1.5 but -2 is smaller than -1.5.

Làm thế nào để bạn tách một số dương và âm trong một mảng?

Algorithm..
Khai báo một mảng và nhập các phần tử mảng ..
Bắt đầu đi qua mảng và nếu phần tử hiện tại là dương, hãy đi qua phần tử tiếp theo trong mảng ..
Nếu phần tử hiện tại là âm, hãy thay đổi các phần tử dương theo một vị trí và chèn số âm trong mảng chuỗi [0 sang N-1] ..

Làm thế nào để tôi làm cho một số âm dương trong Python?

Khi ABS () được sử dụng, nó chuyển đổi số âm thành dương.Tuy nhiên, khi -abs () được sử dụng, thì một số dương có thể được thay đổi thành số âm.Một phương pháp khác là trừ đi số dương đã cho bằng 0.abs () is used, it converts negative numbers to positive. However, when -abs () is used, then a positive number can be changed to a negative number. Another method is to subtract the given positive number by zero.