新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Python程序:統(tǒng)計(jì)集合中的正數(shù)和負(fù)數(shù)
創(chuàng)新互聯(lián)python教程:

寫一個(gè) Python 程序來計(jì)算集合中的正數(shù)和負(fù)數(shù)。for 循環(huán)(用于 pongtSet 中的 eoVal)迭代所有集合項(xiàng)。if 條件(if(eoVal >= 0))檢查 Set 項(xiàng)是否大于或等于零。如果為真,我們在正集合計(jì)數(shù)上加一;否則,將負(fù)集計(jì)數(shù)值加 1。
# Count of Set Positive and Negative Numbers
pongtSet = {6, -33, 99, -28, -56, 11, -45, -43}
print("Positive and Negative Set Items = ", pongtSet)
sPositiveCount = sNegativeCount = 0
for eoVal in pongtSet:
if(eoVal >= 0):
sPositiveCount = sPositiveCount + 1
else:
sNegativeCount = sNegativeCount + 1
print("The Count of Positive Numbers in pongtSet = ", sPositiveCount)
print("The Count of Negative Numbers in pongtSet = ", sNegativeCount)
Python 程序計(jì)算集合中的正數(shù)和負(fù)數(shù)
該 Python 正數(shù)和負(fù)數(shù)示例允許使用 for 循環(huán)范圍輸入設(shè)置項(xiàng)。
# Count of Set Positive and Negative Numbers
pongtSet = set()
number = int(input("Enter the Total Positive Negative Set Items = "))
for i in range(1, number + 1):
value = int(input("Enter the %d Set Item = " %i))
pongtSet.add(value)
print("Positive and Negative Set Items = ", pongtSet)
sPositiveCount = sNegativeCount = 0
for eoVal in pongtSet:
if(eoVal >= 0):
sPositiveCount = sPositiveCount + 1
else:
sNegativeCount = sNegativeCount + 1
print("The Count of Positive Numbers in pongtSet = ", sPositiveCount)
print("The Count of Negative Numbers in pongtSet = ", sNegativeCount)Python 計(jì)數(shù)正負(fù)集合數(shù)輸出
Enter the Total Positive Negative Set Items = 4
Enter the 1 Set Item = -22
Enter the 2 Set Item = -99
Enter the 3 Set Item = 8
Enter the 4 Set Item = -67
Positive and Negative Set Items = {8, -22, -99, -67}
The Count of Positive Numbers in pongtSet = 1
The Count of Negative Numbers in pongtSet = 3在這個(gè) Python 集的例子中,我們創(chuàng)建了一個(gè) countofsetpositiveandneblenumbers 函數(shù),該函數(shù)返回正數(shù)和負(fù)數(shù)的計(jì)數(shù)。
# Count of Set Positive and Negative Numbers
def CountOfSetPositiveandNegativeNumbers(pongtSet):
sPositiveCount = sNegativeCount = 0
for eoVal in pongtSet:
if(eoVal >= 0):
sPositiveCount = sPositiveCount + 1
else:
sNegativeCount = sNegativeCount + 1
return sPositiveCount, sNegativeCount
pongtSet = set()
number = int(input("Enter the Total Positive Negative Set Items = "))
for i in range(1, number + 1):
value = int(input("Enter the %d Set Item = " %i))
pongtSet.add(value)
print("Positive and Negative Set Items = ", pongtSet)
sECount, sOCount = CountOfSetPositiveandNegativeNumbers(pongtSet)
print("The Count of Positive Numbers in pongtSet = ", sECount)
print("The Count of Negative Numbers in pongtSet = ", sOCount)Enter the Total Positive Negative Set Items = 6
Enter the 1 Set Item = 22
Enter the 2 Set Item = -90
Enter the 3 Set Item = -78
Enter the 4 Set Item = 9
Enter the 5 Set Item = 32
Enter the 6 Set Item = 8
Positive and Negative Set Items = {32, -90, 8, 9, -78, 22}
The Count of Positive Numbers in pongtSet = 4
The Count of Negative Numbers in pongtSet = 2 網(wǎng)頁題目:Python程序:統(tǒng)計(jì)集合中的正數(shù)和負(fù)數(shù)
URL標(biāo)題:http://www.dlmjj.cn/article/dphjhdo.html


咨詢
建站咨詢
