字符串“整数”到整数的列表,占“非数字”字符串Python

问题描述 投票:1回答:3

我从在线数据库中获取数据。它将日期和数值作为字符串返回到列表中。即['87', '79', '50', 'M', '65'](这是ay轴图的值,x轴值是与这些值相关的年份,即['2018', '2017', '2016', '2015', '2014']。在我可以绘制这些值之前,我首先需要将它们转换为整数。我通过简单地使用maxT_int = list(map(int,maxTList)完成了这个,然而,问题仍然存在,有时数据丢失并被“M”表示为缺失,如上例所示。

我想要做的是删除'M'或以某种方式解释它并能够绘制值。

当列表中没有'M'时,我可以很好地绘制值。有关如何最好地处理此问题的任何建议?

我的完整代码如下所示

import urllib
import datetime
import urllib.request
import ast
from bokeh.plotting import figure
#from bokeh.io import output_file, show, export_png
import numpy as np



# Get user input for day
# in the format of mm-dd
print("Enter a value for the day that you would like to plot.")
print("The format should be mm-dd")
dayofmonth = input("What day would you like to plot? ")


# testing out a range of years
y = datetime.datetime.today().year

# get starting year
ystart = int(input("What year would you like to start with? "))
# get number of years back
ynum = int(input("How many years would you like to plot? "))
# calculate the number of years back to start from current year
diff = y - ystart
#assign values to the list of years
years = list(range(y-diff,y-(diff+ynum), -1))

start = y - diff
endyear = y - (diff+ynum)

i = 0
dateList=[]
minTList=[]
maxTList=[]
for year in years:
    sdate = (str(year) + '-' + dayofmonth)
    #print(sdate)

    url = "http://data.rcc-acis.org/StnData"

    values = {
    "sid": "KGGW",
    "date": sdate,
    "elems": "maxt,mint",
    "meta": "name",
    "output": "json"
    }

    data = urllib.parse.urlencode(values).encode("utf-8")


    req = urllib.request.Request(url, data)
    response = urllib.request.urlopen(req)
    results = response.read()
    results = results.decode()
    results = ast.literal_eval(results)

    if i < 1:
        n_label = results['meta']['name']
        i = 2
    for x in results["data"]:
            date,maxT,minT = x
            #setting the string of date to datetime

            date = date[0:4]
            date_obj = datetime.datetime.strptime(date,'%Y')
            dateList.append(date_obj)
            minTList.append(minT)
            maxTList.append(maxT)

maxT_int = list(map(int,maxTList))


# setting up the array for numpy
x = np.array(years)
y = np.array(maxT_int)


p = figure(title="Max Temps by Year for the day " + dayofmonth + " " + n_label, x_axis_label='Years',
           y_axis_label='Max Temps', plot_width=1000, plot_height=600)

p.line(x,y,  line_width=2)
output_file("temps.html")
show(p)
python numpy weather
3个回答
1
投票

你可以使用numpy.nan和一个函数:

import numpy as np

lst = ['87', '79', '50', 'M', '65']

def convert(item):
    if item == 'M':
        return np.nan
    else:
        return int(item)

new_lst = list(map(convert, lst))
print(new_lst)

或者 - 如果你进入列表理解:

new_lst = [int(item) if item is not 'M' else np.nan for item in lst]


Both will yield
[87, 79, 50, nan, 65]

0
投票

您可以使用列表推导,迭代y值两次。

raw_x = ['2018', '2017', '2016', '2015', '2014']
raw_y = ['87', '79', '50', 'M', '65']

clean_x = [x for x, y in zip(raw_x, raw_y) if y != 'M']
clean_y = [y for y in raw_y if y != 'M']

0
投票

试试这个:

>>> maxTList = ['87', '79', '50', 'M', '65']
>>> maxT_int = [int(item) for item in maxTList if item.isdigit()]
>>> maxT_int
[87, 79, 50, 65]

就像现在一样,代码只是丢弃非数字字符串(如问题中所指定的),使得maxT_int比maxTList短(在这种情况下,您必须将相同的算法应用于其他列表以确保排除相应的年份) 。 如果您希望它们相等,则可以指定一个默认值,以防该字符串不是有效的int(如果和顺序相反,请注意):

>>> maxT_int2 = [int(item) if item.isdigit() else -1 for item in maxTList]
[87, 79, 50, -1, 65]
© www.soinside.com 2019 - 2024. All rights reserved.