0

第一部分产生

    average = ['2 is the average for mark a', 
       '1 is the average for mark b', 
       '1 is the average for mark c',
       '2 is the average for mark d', 
       '1 is the average for mark e', 
       '1 is the average for mark f', 
       '1 is the average for mark g', 
       '1 is the average for mark h', 
       '1 is the rainfall average for mark i',
       '1 is the average for mark j']

然后代码的第二部分产生

z = [1.2423] 

我现在试图将平均值的结果与 z 列表进行比较并产生结果。基本上

if the integer value for a is greater than z then print greater than
if the integer value for b is less than z the print less than   
if the integer value for c is greater than z the print greater than.

有没有一种方法可以简化我的代码,以便它比较所有平均值而不必手动键入每个 if 语句?即比较所有平均值,然后打印平均值是否小于或大于一行。

谢谢。这是我今天的最后一个问题。

4

2 回答 2

0

您在此处提供的“几乎没有”信息可以解决您的问题。此外,一些进一步的研究不会误入歧途。你会到达那里。坚持下去。

average = [a list of your data averages]
z = 1.2423 # z variable doesn't need to be a list, just an int


for i in average:
   if int(i) > z:
       print "greater than"
   elif int(i) < z:
       print "less than"
   else:
       print "equal to z"
于 2013-06-25T04:50:45.940 回答
0
>>> z = 1.2423
>>> [{-1: 'less than', 0: 'equal to', 1: 'greater than'}[(x > z) - (x < z)] for x in [1, 1, 2, 1, 1, 2, 2]]
['less than', 'less than', 'greater than', 'less than', 'less than', 'greater than', 'greater than']

如果您正在运行 2.x,则方括号中的数学可以替换为cmp(x, z).

于 2013-06-25T04:56:04.103 回答