2017-09-16 5 views
0

タプルのリストから最小値を見つけるのは初めてのことです。タプルリストから最小値を見つける

def min_steps(step_records): 
    """ random """ 
    if step_records != []: 
     for steps in step_records: 
      day, step = steps 
      result = min(step) 
    else: 
     result = None 
    return result 

これはエラーになり:リストはこのようなものであれば

'int' object is not iterable

にはどうすればminを返すのですか?

step_records = [('2010-01-01',1), 
       ('2010-01-02',2), 
       ('2010-01-03',3)] 
+0

、あなたの質問や書式コードを編集してください理解する。 – Styx

答えて

0

tuplesは(:Accessing a value in a tuple that is in a list参照)を索引付けすることができます。

あなたが行っていたように、我々は、これらの指標とコール・最小値からリストを作成することができます使用:

def min_steps(step_records): 
    """ random """ 
    if step_records: 
     result = min([step[1] for step in step_records]) # min([1,2,3]) 
    else: 
     result = None 
    return result 

step_records = [('2010-01-01',1), 
       ('2010-01-02',2), 
       ('2010-01-03',3)] 

print(min_steps(step_records)) 

出力:それは簡単だろうので

1 
関連する問題