Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将字符串列表转换为整数_Python - Fatal编程技术网

Python 将字符串列表转换为整数

Python 将字符串列表转换为整数,python,Python,我正在做一个项目,其中一个要求是我转换数据中的温度,但我不知道如何启动它。我需要以华氏温度来表示,但我的问题是数据是列表中的字符串,我不知道如何将其转换为整数,以便将其转换为华氏温度 数据中列出的所有温度均以摄氏度为单位 这是我到目前为止所拥有的 import requests import matplotlib.pyplot as plt import fix_yahoo_finance as yf link = "https://data.giss.nasa.gov/gistem

我正在做一个项目,其中一个要求是我转换数据中的温度,但我不知道如何启动它。我需要以华氏温度来表示,但我的问题是数据是列表中的字符串,我不知道如何将其转换为整数,以便将其转换为华氏温度

数据中列出的所有温度均以摄氏度为单位

这是我到目前为止所拥有的

import requests
import matplotlib.pyplot as plt
import fix_yahoo_finance as yf

link = "https://data.giss.nasa.gov/gistemp/tabledata_v3/GLB.Ts+dSST.txt"
f = requests.get(link)
data = f.text
lines = data.split('\n')


el = []                    # creates an empty list
for each in lines[8:159]:  # gets rid of unnecessary information. Prints from 1880 to 2018
    tempL = each.split()
    if (tempL and tempL[0] != 'Year'):    # gets rid of the excess data like the title
        el.append(tempL[:13])
到目前为止,我的结果是这样的

[['1880', '-29', '-18', '-11', '-20', '-12', '-23', '-21', '-9', '-16', '-23', '-20', '-23'], ['1881', '-16', '-17', '4', '4', '2', '-20', '-7', '-3', '-14', '-21', '-22', '-11'], ['1882', '14', '15', '3', '-19', '-16', '-26', '-21', '-6', '-10', '-25', '-16', '-25'], ['1883', '-32', '-39', '-13', '-17', '-20', '-13', '-8', '-15', '-21', '-14', '-22', '-16'], ['1884', '-16', '-8', '-37', '-43', '-37', '-41', '-35', '-26', '-27', '-24', '-30', '-29'], ['1885', '-59', '-30', '-25', '-42', '-42', '-44', '-36', '-32', '-24', '-19', '-20', '-5'], ['1886', '-42', '-45', '-39', '-28', '-26', '-39', '-22', '-34', '-26', '-29', '-31', '-27'], ['1887', '-73', '-52', '-34', '-39', '-32', '-24', '-23', '-32', '-23', '-33', '-23', '-34'], ['1888', '-38', '-36', '-41', '-22', '-22', '-18', '-9', '-16', '-10', '2', '1', '-6'], ['1889', '-11', '19', '8', '7', '-2', '-14', '-10', '-20', '-22', '-22', '-33', '-30'], ['1890', '-44', '-45', '-40', '-37', '-45', '-27', '-27', '-38', '-39', '-24', '-48', '-31'], ['1891', '-42', '-47', '-19', '-28', '-18', '-20', '-20', '-16', '-14', '-21', '-35', '-4'], ['1892', '-28', '-12', '-35', '-35', '-25', '-23', '-32', '-25', '-13', '-13', '-42', '-39'], ['1893', '-80', '-54', '-23', '-30', '-36', '-29', '-16', '-27', '-20', '-17', '-17', '-35'], ['1894', '-54', '-33', '-25', '-48', '-35', '-44', '-26', '-22', '-25', '-21', '-25', '-20'], ['1895', '-44', '-42', '-29', '-25', '-27', '-24', '-18', '-16', '-9', '-8', '-15', '-16'], ['1896', '-26', '-14', '-26', '-31', '-18', '-14', '-4', '-4', '-3', '11', '-8', '-4'], ['1897', '-15', '-15', '-14', '-2', '-5', '-16', '-7', '-11', '-8', '-12', '-19', '-19'], ['1898', '-3', '-30', '-52', '-33', '-32', '-21', '-24', '-25', '-22', '-33', '-38', '-25'], ['1899', '-19', '-40', '-34', '-21', '-23', '-33', '-18', '-7', '-3', '-5', '12', '-28'], ['1900', '-37', '-3', '1', '-12', '-10', '-13', '-13', '-10', '-5', '9', '-10', '-7'], ['1901', '-24', '-4', '8', '-4', '-17', '-13', '-16', '-20', '-22', '-31', '-17', '-28'], ['1902', '-20', '-3', '-29', '-30', '-35', '-35', '-30', '-32', '-29', '-29', '-39', '-46'], ['1903', '-26', '-4', '-24', '-43', '-44', '-46', '-37', '-49', '-49', '-48', '-45', '-52'], ['1904', '-68', '-58', '-51', '-55', '-56', '-51', '-54', '-50', '-53', '-40', '-22', '-35'], ['1905', '-40', '-59', '-24', '-36', '-33', '-31', '-29', '-22', '-19', '-25', '-10', '-19'], ['1906', '-28', '-31', '-16', '-4', '-24', '-21', '-25', '-20', '-27', '-19', '-39', '-16'], ['1907', '-42', '-51', '-27', '-39', '-47', '-44', '-37', '-35', '-33', '-24', '-49', '-48'], ['1908', '-44', '-31', '-54', '-46', '-41', '-41', '-40', '-46', '-36', '-45', '-50', '-49'], ['1909', '-69', '-47', '-51', '-60', '-56', '-53', '-45', '-31', '-35', '-37', '-31', '-55'], ['1910', '-45', '-44', '-51', '-40', '-35', '-38', '-35', '-37', '-36', '-38', '-54', '-66'], ['1911', '-64', '-58', '-60', '-54', '-52', '-48', '-41', '-42', '-38', '-23', '-18', '-22'], ['1912', '-26', '-12', '-35', '-20', '-22', '-26', '-44', '-55', '-51', '-57', '-38', '-43'], ['1913', '-42', '-43', '-43', '-40', '-47', '-49', '-37', '-34', '-36', '-34', '-19', '-4'], ['1914', '4', '-11', '-22', '-29', '-23', '-26', '-24', '-14', '-13', '-4', '-18', '-8'], ['1915', '-17', '0', '-10', '6', '-7', '-21', '-11', '-20', '-17', '-25', '-13', '-17'], ['1916', '-9', '-13', '-28', '-30', '-32', '-47', '-36', '-26', '-32', '-27', '-40', '-80'], ['1917', '-58', '-61', '-60', '-51', '-55', '-43', '-25', '-22', '-18', '-40', '-28', '-68'], ['1918', '-42', '-32', '-25', '-45', '-46', '-36', '-31', '-30', '-15', '-5', '-11', '-28'], ['1919', '-20', '-23', '-20', '-14', '-28', '-38', '-29', '-33', '-22', '-19', '-41', '-42'], ['1920', '-23', '-23', '-8', '-26', '-27', '-36', '-32', '-25', '-20', '-27', '-27', '-44'], ['1921', '-3', '-19', '-23', '-29', '-29', '-28', '-15', '-24', '-17', '-3', '-13', '-17'], ['1922', '-34', '-44', '-13', '-24', '-33', '-33', '-25', '-31', '-32', '-32', '-14', '-18'], ['1923', '-29', '-37', '-35', '-40', '-34', '-26', '-30', '-30', '-28', '-11', '1', '-2'], ['1924', '-22', '-24', '-7', '-31', '-18', '-23', '-26', '-34', '-31', '-34', '-20', '-41'], ['1925', '-36', '-38', '-25', '-25', '-30', '-32', '-28', '-15', '-16', '-18', '4', '9'], ['1926', '19', '6', '11', '-13', '-22', '-23', '-24', '-13', '-13', '-10', '-5', '-28'], ['1927', '-27', '-18', '-37', '-31', '-25', '-26', '-16', '-21', '-11', '-1', '-4', '-33'], ['1928', '-1', '-8', '-27', '-28', '-30', '-37', '-19', '-23', '-19', '-17', '-9', '-18'], ['1929', '-46', '-60', '-32', '-40', '-39', '-41', '-33', '-29', '-23', '-12', '-12', '-54'], ['1930', '-30', '-27', '-9', '-24', '-23', '-20', '-20', '-12', '-12', '-9', '15', '-8'], ['1931', '-10', '-24', '-8', '-21', '-20', '-7', '-3', '-3', '-6', '2', '-12', '-9'], ['1932', '14', '-18', '-19', '-6', '-18', '-29', '-24', '-23', '-12', '-10', '-27', '-26'], ['1933', '-34', '-34', '-30', '-25', '-29', '-33', '-21', '-24', '-27', '-21', '-30', '-46'], ['1934', '-24', '-4', '-33', '-30', '-11', '-15', '-10', '-11', '-16', '-8', '3', '-4'], ['1935', '-33', '15', '-13', '-36', '-30', '-27', '-22', '-22', '-21', '-8', '-28', '-20'], ['1936', '-28', '-39', '-22', '-20', '-18', '-21', '-8', '-13', '-9', '-4', '-4', '-3'], ['1937', '-13', '4', '-18', '-17', '-8', '-5', '-5', '2', '11', '8', '9', '-11'], ['1938', '1', '-2', '8', '4', '-10', '-18', '-9', '-6', '2', '11', '4', '-22'], ['1939', '-7', '-8', '-19', '-9', '-6', '-8', '-8', '-7', '-7', '-3', '6', '44'], ['1940', '-6', '13', '15', '18', '10', '8', '12', '5', '13', '8', '17', '28'], ['1941', '21', '32', '8', '17', '14', '12', '21', '14', '2', '34', '24', '21'], ['1942', '26', '2', '9', '11', '9', '4', '0', '-4', '-5', '-1', '8', '7'], ['1943', '-4', '13', '-8', '7', '4', '-6', '8', '0', '6', '22', '19', '24'], ['1944', '35', '26', '27', '20', '19', '20', '18', '17', '30', '26', '10', '2'], ['1945', '9', '-2', '7', '18', '5', '-1', '3', '27', '22', '19', '7', '-9'], ['1946', '15', '7', '1', '6', '-8', '-21', '-14', '-16', '-7', '-6', '-6', '-35'], ['1947', '-13', '-8', '7', '7', '-3', '2', '-8', '-9', '-13', '6', '2', '-16'], ['1948', '7', '-15', '-25', '-11', '1', '-4', '-11', '-12', '-11', '-7', '-12', '-23'], ['1949', '9', '-16', '-2', '-10', '-8', '-23', '-14', '-12', '-14', '-6', '-10', '-22'], ['1950', '-33', '-28', '-7', '-20', '-9', '-3', '-10', '-16', '-12', '-22', '-38', '-20'], ['1951', '-35', '-43', '-21', '-13', '0', '-2', '3', '6', '8', '5', '-2', '14'], ['1952', '13', '11', '-10', '2', '-2', '-4', '4', '4', '7', '-1', '-14', '-1'], ['1953', '8', '14', '11', '18', '9', '10', '0', '6', '4', '6', '-6', '2'], ['1954', '-28', '-13', '-15', '-17', '-21', '-17', '-19', '-17', '-10', '-4', '6', '-22'], ['1955', '14', '-17', '-34', '-21', '-21', '-12', '-12', '1', '-11', '-5', '-24', '-30'], ['1956', '-16', '-26', '-23', '-27', '-31', '-16', '-12', '-26', '-20', '-24', '-17', '-8'], ['1957', '-11', '-4', '-6', '-1', '9', '16', '1', '15', '9', '1', '6', '16'], ['1958', '39', '24', '11', '3', '7', '-7', '5', '-3', '-2', '4', '2', '0'], ['1959', '8', '9', '18', '13', '4', '2', '6', '-1', '-6', '-9', '-9', '-1'], ['1960', '-1', '16', '-34', '-14', '-7', '-4', '-3', '1', '7', '7', '-12', '18'], ['1961', '7', '19', '9', '12', '11', '11', '0', '3', '7', '0', '3', '-15'], ['1962', '7', '14', '12', '4', '-4', '4', '2', '-1', '1', '0', '7', '0'], ['1963', '-2', '20', '-14', '-6', '-4', '5', '8', '27', '20', '15', '15', '-1'], ['1964', '-8', '-13', '-23', '-31', '-25', '-2', '-3', '-21', '-29', '-31', '-21', '-30'], ['1965', '-8', '-17', '-11', '-19', '-14', '-9', '-12', '-3', '-16', '-5', '-7', '-6'], ['1966', '-17', '-1', '5', '-12', '-11', '1', '9', '-8', '-2', '-15', '-2', '-6'], ['1967', '-6', '-21', '4', '-5', '14', '-8', '1', '1', '-6', '8', '-7', '-3'], ['1968', '-25', '-15', '20', '-6', '-13', '-8', '-12', '-9', '-18', '10', '-3', '-13'], ['1969', '-11', '-14', '1', '18', '19', '5', '-2', '3', '9', '13', '13', '27'], ['1970', '9', '22', '8', '9', '-4', '-2', '-2', '-9', '11', '6', '2', '-12'], ['1971', '-2', '-20', '-17', '-8', '-5', '-19', '-11', '-3', '-4', '-6', '-5', '-9'], ['1972', '-24', '-17', '2', '0', '-4', '6', '1', '17', '2', '9', '4', '20'], ['1973', '29', '31', '27', '26', '26', '18', '10', '3', '10', '14', '6', '-6'], ['1974', '-15', '-28', '-5', '-11', '-2', '-6', '-4', '11', '-11', '-8', '-7', '-9'], ['1975', '7', '7', '13', '5', '16', '1', '-1', '-19', '-3', '-9', '-16', '-17'], ['1976', '-1', '-6', '-21', '-8', '-24', '-16', '-10', '-14', '-9', '-26', '-4', '10'], ['1977', '19', '22', '25', '28', '30', '24', '20', '16', '0', '2', '17', '2'], ['1978', '6', '14', '21', '15', '6', '-2', '3', '-17', '5', '0', '16', '10'], ['1979', '12', '-11', '18', '13', '5', '13', '2', '14', '25', '24', '28', '47'], ['1980', '30', '42', '30', '33', '36', '17', '28', '23', '19', '18', '29', '20'], ['1981', '55', '41', '49', '32', '24', '32', '34', '32', '17', '14', '24', '43'], ['1982', '10', '15', '-1', '10', '15', '5', '13', '7', '14', '13', '15', '43'], ['1983', '52', '41', '43', '31', '36', '20', '18', '33', '39', '16', '32', '17'], ['1984', '30', '17', '29', '8', '33', '5', '17', '16', '21', '16', '6', '-5'], ['1985', '22', '-6', '17', '11', '17', '18', '-1', '15', '15', '11', '8', '15'], ['1986', '28', '39', '27', '24', '24', '11', '11', '12', '0', '13', '11', '16'], ['1987', '36', '46', '16', '22', '25', '37', '45', '28', '39', '32', '25', '48'], ['1988', '58', '43', '51', '45', '43', '43', '34', '47', '41', '39', '12', '33'], ['1989', '15', '35', '36', '33', '16', '14', '34', '37', '37', '32', '20', '38'], ['1990', '41', '41', '77', '55', '46', '38', '43', '31', '29', '41', '46', '41'], ['1991', '41', '49', '35', '52', '38', '53', '48', '40', '48', '30', '31', '32'], ['1992', '44', '42', '48', '24', '32', '25', '12', '7', '0', '9', '3', '22'], ['1993', '37', '39', '36', '27', '27', '25', '28', '13', '10', '22', '6', '16'], ['1994', '27', '2', '27', '40', '28', '42', '30', '21', '29', '41', '46', '36'], ['1995', '51', '78', '45', '47', '26', '41', '48', '46', '32', '47', '45', '28'], ['1996', '25', '48', '32', '36', '27', '25', '35', '49', '25', '19', '41', '40'], ['1997', '31', '38', '53', '36', '36', '53', '35', '41', '53', '62', '65', '59'], ['1998', '60', '90', '63', '63', '69', '77', '68', '66', '42', '44', '49', '56'], ['1999', '48', '65', '34', '32', '31', '37', '37', '31', '41', '39', '38', '43'], ['2000', '23', '56', '57', '57', '36', '41', '38', '42', '40', '28', '32', '29'], ['2001', '42', '43', '55', '50', '56', '54', '60', '48', '53', '50', '69', '55'], ['2002', '75', '75', '89', '56', '63', '55', '61', '53', '62', '55', '58', '43'], ['2003', '73', '55', '57', '53', '60', '47', '53', '65', '64', '74', '52', '73'], ['2004', '58', '72', '64', '62', '38', '42', '24', '43', '50', '64', '70', '51'], ['2005', '72', '57', '69', '66', '62', '66', '63', '60', '74', '75', '71', '65'], ['2006', '56', '69', '62', '50', '46', '64', '52', '70', '61', '67', '70', '73'], ['2007', '95', '70', '69', '73', '66', '59', '60', '57', '59', '57', '54', '46'], ['2008', '23', '34', '73', '51', '47', '46', '59', '44', '62', '63', '64', '53'], ['2009', '61', '51', '52', '58', '64', '65', '69', '66', '68', '63', '76', '65'], ['2010', '73', '79', '91', '85', '73', '62', '59', '63', '59', '69', '77', '46'], ['2011', '48', '51', '62', '62', '51', '57', '71', '71', '54', '63', '56', '53'], ['2012', '45', '48', '56', '68', '75', '63', '53', '62', '72', '74', '73', '52'], ['2013', '67', '56', '65', '53', '58', '66', '57', '66', '78', '67', '78', '65'], ['2014', '73', '52', '76', '77', '85', '66', '56', '81', '88', '81', '66', '77'], ['2015', '81', '87', '90', '75', '75', '79', '71', '79', '82', '107', '103', '110'], ['2016', '115', '135', '131', '107', '91', '77', '82', '100', '88', '90', '91', '83'], ['2017', '98', '113', '113', '92', '89', '70', '82', '87', '76', '88', '86', '89'], ['2018', '77', '85', '91', '87', '81', '74', '78', '73', '76', '99', '78', '89']]

任何建议都会有帮助。谢谢。

您已经走得很远了,我不确定您想如何存储/表示转换为华氏温度后的温度,但这段代码可能有帮助:

import requests
import matplotlib.pyplot as plt
import fix_yahoo_finance as yf

link = "https://data.giss.nasa.gov/gistemp/tabledata_v3/GLB.Ts+dSST.txt"
f = requests.get(link)
data = f.text
lines = data.split('\n')


el = []                    # creates an empty list
for each in lines[8:159]:  # gets rid of unnecessary information. Prints from 1880 to 2018
    tempL = each.split()
    if (tempL and tempL[0] != 'Year'):    # gets rid of the excess data like the title
        el.append(tempL[:13])



###### What I added:

for temp_list in el:
    year = int(temp_list[0]) # the first elements seems to be the year
    
    for temp_Cel in temp_list[1:]: # all other elements are the temps
        temp_Far = int(temp_Cel) * (9 / 5) + 32 # int() converts the string to a integer
        print(temp_Far)
    

你有没有研究过把字符串转换成数字和把温度转换成华氏温度的问题?还是这个问题?这些不是温度,而是百分之一摄氏度的温度变化。除了第一个元素,它是一年。