【问题标题】:Isolating the elements of a row in python在python中隔离一行的元素
【发布时间】:2014-11-09 11:33:00
【问题描述】:

我正在做一个项目,我正在尝试绘制我国各个州的降雨模式。通过使用此命令,我从我的数据库中获取数据:

 cur.execute('SELECT JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DECEMBER FROM Rainfall_In_Cm where STATE_UT = %s && DISTRICT = %s' ,(state , district))

结果以包含 1 个元素的列表形式出现(查询输出的第一行):

(Decimal('17.5'), Decimal('9.9'), Decimal('8.9'), Decimal('4.0'), Decimal('9.3'), Decimal('53.8'), Decimal('227.1'), Decimal('280.90'), Decimal('125.4'), Decimal('28.1'), Decimal('5.0'), Decimal('4.7'))

现在我希望所有元素都采用列表的形式,我可以使用 matplotlib 来绘制图形,并且我想从每个值的前面删除“十进制”字符串。我该怎么做?

【问题讨论】:

    标签: python-2.7 mysql-python


    【解决方案1】:

    您的结果是一个元组,而不是一个列表,但这不是问题。

    铸造应该适用于这些Decimal 对象。您可以使用列表推导:

    #Data from your example
    foo = (Decimal('17.5'), Decimal('9.9'), Decimal('8.9'), Decimal('4.0'), Decimal('9.3'), Decimal('53.8'), Decimal('227.1'), Decimal('280.90'), Decimal('125.4'), Decimal('28.1'), Decimal('5.0'), Decimal('4.7'))
    bar = [float(i) for i in foo]
    

    如果你想要整数,使用:

    bar = [int(i) for i in foo]
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-08
    • 2015-03-27
    • 1970-01-01
    • 2013-10-26
    • 2020-03-05
    • 1970-01-01
    • 2018-10-06
    • 2016-08-04
    相关资源
    最近更新 更多