TypeError:'浮动对象不可迭代'

时间:2015-12-06 06:28:59

标签: python min

我有

格式的Datalog文件
<?php 

function formthrottle_check()
{
    if (!is_writable('.'))
    {
        return '8';
    }

    try
    {
        if (in_array("sqlite",PDO::getAvailableDrivers(),TRUE))
        {
            $db = new PDO('sqlite:muse-throttle-db.sqlite3');
            if ( file_exists('muse-throttle-db') )
            {
                unlink('muse-throttle-db');
            }
        }
        else if (function_exists("sqlite_open")) 
        {
            $db = new PDO('sqlite2:muse-throttle-db');
            if ( file_exists('muse-throttle-db.sqlite3') )
            {
                unlink('muse-throttle-db.sqlite3');
            }
        }
    }
    catch( PDOException $Exception ) {
        return '9';
    }

    $retCode ='5';
    if ($db) 
    {
        $res = $db->query("SELECT 1 FROM sqlite_master WHERE type='table' AND name='Submission_History';");
        if (!$res or $res->fetchColumn() == 0)
        {
            $created = $db->exec("CREATE TABLE Submission_History (IP VARCHAR(39), Submission_Date TIMESTAMP)");

            if($created == 0)
            {
                $created = $db->exec("INSERT INTO Submission_History (IP,Submission_Date) VALUES ('256.256.256.256', DATETIME('now'))");
            }

            if ($created != 1)
            {
                $retCode = '2';
            }
        }
        if($retCode == '5')
        {
            $res = $db->query("SELECT COUNT(1) FROM Submission_History;");
            if ($res && $res->fetchColumn() > 0)
            {
                $retCode = '0';
            }
            else
                $retCode = '3';
        }

        // Close file db connection
        $db = null;
    } 
    else
        $retCode = '4';

    return $retCode;
}   

function formthrottle_too_many_submissions($ip)
{
    $tooManySubmissions = false;

    try
    {
        if (in_array("sqlite",PDO::getAvailableDrivers(),TRUE))
        {
            $db = new PDO('sqlite:muse-throttle-db.sqlite3');
        }
        else if (function_exists("sqlite_open")) 
        {
            $db = new PDO('sqlite2:muse-throttle-db');
        }
    }
    catch( PDOException $Exception ) {
        return $tooManySubmissions;
    }

    if ($db) 
    {
        $res = $db->query("SELECT 1 FROM sqlite_master WHERE type='table' AND name='Submission_History';");
        if (!$res or $res->fetchColumn() == 0)
        {
            $db->exec("CREATE TABLE Submission_History (IP VARCHAR(39), Submission_Date TIMESTAMP)");
        }
        $db->exec("DELETE FROM Submission_History WHERE Submission_Date < DATETIME('now','-2 hours')");

        $stmt = $db->prepare("INSERT INTO Submission_History (IP,Submission_Date) VALUES (:ip, DATETIME('now'))");
        $stmt->bindParam(':ip', $ip);
        $stmt->execute();
        $stmt->closeCursor();

        $stmt = $db->prepare("SELECT COUNT(1) FROM Submission_History WHERE IP = :ip;");
        $stmt->bindParam(':ip', $ip);
        $stmt->execute();
        if ($stmt->fetchColumn() > 25) 
            $tooManySubmissions = true;
        // Close file db connection
        $db = null;
    }
    return $tooManySubmissions;
}
?>

对于每一列,我试图找到最小值和相应的行号。

但是使用下面的代码,41, 3.68, 3.58, 3.71, 3.54, 3.68, 3.79 42, 3.69, 3.57, 3.73, 3.55, 3.67, 3.78 43, 3.68, 3.57, 3.73, 3.54, 3.68, 3.79 44, 3.68, 3.57, 3.73, 3.54, 3.67, 3.79 45, 3.68, 3.57, 3.73, 3.54, 3.67, 3.78 46, 3.68, 3.57, 3.73, 3.54, 3.67, 3.78 47, 3.68, 3.57, 3.73, 3.54, 3.67, 3.78 48, 3.68, 3.57, 3.73, 3.54, 3.67, 3.78 49, 3.68, 3.57, 3.73, 3.54, 3.67, 3.78 函数给出了&#39; float对象不可迭代的错误。

min()

2 个答案:

答案 0 :(得分:1)

错误是因为

print (min(result[0][i]))

result[0][i]是一个float对象,而不是您预期的列表。

相反,请列出min

for j in range (len(result)):
    print(result[j][0])
    print(min(result[j][1:]))

范围[1:]表示除第一个元素之外的所有内容。

答案 1 :(得分:0)

min遍历参数中的list / tuple并给出其中最小的项,但是你为它提供了一个float,所以它试图遍历float。

绝对没有理由这样做,但如果你这样做,它就不会崩溃:

print (min(result[0][i],))

因为现在它不是浮动,而是一个元组。它只会打印result [0] [i]

的值

如果你想要的是获得每一行的最小值,你必须这样做:

for i in range (len(result)):
    print (min(result[i]))

如果您想要的是每列中的最小值(假设所有行具有相同的列数),您必须执行以下操作:

for j in range(len(result[0]))
    print ('minimum in column ' + str(j) + ':')
    print (min([result[i][j] for i in range(len(result))]))

其中j是列的索引