【问题标题】:How can l convert tuple to integer to do some calculations?我如何将元组转换为整数来进行一些计算?
【发布时间】:2017-01-17 12:34:13
【问题描述】:

我有 41 年的数据集,包括 10 列,我试图用 Matplotlib 绘制这些数据,并且我能够无错误地绘制列。但是,我想生成不同类型的图表,例如年平均 pcp 、月平均 pcp 和 pcp 年总和等。我从列中获取数据,我无法将这些数据转换为整数来进行一些计算。

这是来自 Csv 文件的示例数据集:

date    day month   year    pcp1    pcp2    pcp3    pcp4    pcp5    pcp6
1.01.1979   1   1   1979    0.431   2.167   9.375   0.431   2.167   9.375
2.01.1979   2   1   1979    1.216   2.583   9.162   1.216   2.583   9.162
3.01.1979   3   1   1979    4.041   9.373   23.169  4.041   9.373   23.169
4.01.1979   4   1   1979    1.799   3.866   8.286   1.799   3.866   8.286
5.01.1979   5   1   1979    0.003   0.051   0.342   0.003   0.051   0.342
6.01.1979   6   1   1979    2.345   3.777   7.483   2.345   3.777   7.483
7.01.1979   7   1   1979    0.017   0.031   0.173   0.017   0.031   0.173
8.01.1979   8   1   1979    5.061   5.189   43.313  5.061   5.189   43.313

这是我的代码:

import csv
import numpy as np
import matplotlib.pyplot as plt



with open('output813b.csv', 'r') as csvfile:

    # get number of columns
    for line in csvfile.readlines():
        array = line.split()
        first_item = array[0]

    num_columns = len(array)
    csvfile.seek(0)

    reader = csv.reader(csvfile, delimiter=',')
    next(reader)
    included_col1 = [1]
    included_col6=[4]
    x=[]
    y=[]


    for row in reader:

            content = list(row[i] for i in included_col1)
            content2= list(row[i] for i in included_col6)
            x.append(content)
            y.append(content2)

    klm=tuple(x[i] for i in range(1,1000) if x[i]==["1979"])
    s=0
    for i in range(1,len(klm)):
        s+=y[i] #### error (for +=: 'int' and 'list')

    print (tuple(y[5])) ###example output ('2.345',)
    print (int(y [5][0])) #### error invalid literal for int() with base 10: '2.345'

元组中的元组,我使用int(y[5][0]) 将元组转换为int,但出现错误。我在我的代码中放入错误消息。我该如何解决这个问题并进行一些计算。提前致谢

【问题讨论】:

  • 嗯...你不想让它成为float,因为它有小数点吗?

标签: python csv numpy matplotlib


【解决方案1】:

你有两个不同的问题:

1. s+=y[i] #### 错误(对于 +=: 'int' 和 'list')

以下是代码的相关部分:

y=[]
for row in reader:
        content2= list(row[i] for i in included_col6)
        y.append(content2)
s=0
for i in range(1,len(klm)):
    s+=y[i] #### error (for +=: 'int' and 'list')

然后:

  1. sint
  2. ycontent2 的列表
  3. content2list

然后你添加到 int s list y[i]。 那是不可能的。你真正想要什么?

2。 #### 以 10 为基数的 int() 错误无效文字:'2.345'

您正在尝试将十进制数转换为 int。

您应该使用 float(tuple(y[5][0])) 保留浮点数或使用 int(float(tuple(y[5][0]))) 将其转换为 int

【讨论】:

    【解决方案2】:

    最简单的事情是:

    int(float(y[5][0]))
    

    相反。由于您的字符串值中包含小数,因此您将无法直接转换为int,而无需先转换为float。请记住,您会损失一些精度:

    >>> int(float('2.345'))
    2
    

    因此,如果您想在计算中使用这些值,您可能只想将元组值转换为浮点数:

    float(y[5][0])
    

    【讨论】:

      【解决方案3】:

      由于 2.345 不是整数,因此您需要确定如何处理浮动部分。你想把它装在天花板上、地板上还是圆形?

      • int(round(float("2.345"))) 给出 2

      • int(math.ceil(float("2.345"))) 给出 3

      • int(math.floor(float("2.345"))) 给出 2

      【讨论】:

        【解决方案4】:

        那是因为你试图将十进制数转换为 int

        如果你想使用确切的值,你可以使用float(tuple(y[5][0])) 或者如果你想截断你可以使用int(float(tuple(y[5][0])))的值

        【讨论】:

        • 这是我的错误。我很抱歉花时间回答这个简单的问题。它让我忘记了。非常感谢您的所有努力
        猜你喜欢
        • 2015-02-24
        • 2015-12-16
        • 2018-08-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-09
        相关资源
        最近更新 更多