Python 根据用户输入对数组元素进行排序
根据输入所属的类别对用户输入进行排序,然后显示输入在这些类别中的次数Python 根据用户输入对数组元素进行排序,python,arrays,Python,Arrays,根据输入所属的类别对用户输入进行排序,然后显示输入在这些类别中的次数 if tempCelcius <= 20: #counts the number of Cool days in list x elif tempCelcius >= 21 and tempC <= 30: #counts the number of Warm days in list y elif tempCelcius >= 31: #
if tempCelcius <= 20: #counts the number of Cool days in list
x
elif tempCelcius >= 21 and tempC <= 30: #counts the number of Warm days in list
y
elif tempCelcius >= 31: #counts number of Hot days in list
z =
print ('# of Cool days: ', x)
print ('# of Warm days: ', y)
print ('# of Hot days: ', z)
如果tempCelcius=21且tempC=31:#统计列表中的热天数
z=
打印(“#凉爽的日子:”,x)
打印(“#温暖的日子:”,y)
打印(“#热天:”,z)
您可以使用列表理解来获得每个温度范围的长度
temps = ['Day 1', 'Day 2', 'Day 3', 'Day 4', 'Day 5', 'Day 6', 'Day 7', 'Day 8', 'Day 9', 'Day 10']
celList = []
fahList = []
for temp in temps:
print ('What was the temp on', temp)
tempCelcius = int(input(' '))
fah = round((tempCelcius * 1.8) + 32)
celList.append(tempCelcius)
fahList.append(fah)
x = len([t for t in celList if t <= 20]) #counts number of cool days within the 10 temps
y = len([t for t in celList if 21 <= t <= 30]) #counts number of warm days within the 10 temps
z = len([t for t in celList if t >= 31]) #counts number of Hot days within the 10 temps
print("Cool days", x)
print("Warm days", y)
print("Hot days", z)
重复列出您拥有的临时工。并将if条件移动到迭代内部,当if条件成功时,增加变量数量(x,y,z)。然后打印这些变量,你所说的迭代温度列表是什么意思?温度列表。我知道你指的是温度。hahai编辑了我的程序。我不确定IF在列表中排序需要调用什么变量,以及如何添加每个变量的计数。为了更好地理解:
len
函数返回列表中的项目数,因此它自己进行计数?我知道这是一种很不清楚的方式,我在这方面还是很新的。我只是想确保我了解你所做的,这样我就可以在将来应用它。
What was the temp on Day 1
6
What was the temp on Day 2
6
What was the temp on Day 3
6
What was the temp on Day 4
99
What was the temp on Day 5
99
What was the temp on Day 6
99
What was the temp on Day 7
23
What was the temp on Day 8
23
What was the temp on Day 9
23
What was the temp on Day 10
23
Cool days 3
Warm days 4
Hot days 3