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']

次に、コードの 2 番目の部分が生成されます。

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 ステートメントを手動で入力することなく、すべての平均を比較するようにコードを単純化する方法はありますか? つまり、すべての平均値を比較して、平均値が 1 行よりも小さいか大きいかを出力します。

ありがとう。これが今日の私の最後の質問です。

4

2 に答える 2

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 に答える