Python 如何读取文本文件并将其转换为列表以便与统计包一起使用
How to read a text file and convert into a list for use with statistics package in Python
我目前运行的代码如下
import os
import math
import statistics
def main ():
infile = open('USPopulation.txt', 'r')
values = infile.read()
infile.close()
index = 0
while index < len(values):
values(index) = int(values(index))
index += 1
print(values)
main()
The text file contains 41 rows of numbers each entered on a single line like so:
151868
153982
156393
158956
161884
165069
168088
etc.
我的任务是创建一个程序,显示该时间段内人口的平均变化。在此期间人口增长最多的年份。时间段内人口增长最小的年份(与上一年相比)。
该代码将在一行中打印每个文本文件条目,但在尝试转换为 int 以用于统计数据包时,我收到以下错误:
值(索引)= int(值(索引))
语法错误:无法分配给函数调用
values(index) = int(values(index)) 行取自阅读以及堆栈溢出资源。
我个人会使用 numpy 来读取文本文件。
在你的情况下我会这样做:
import numpy as np
def main ():
infile = np.loadtxt('USPopulation.txt')
maxpop = np.argmax(infile)
minpop = np.argmin(infile)
print(f'maximum population = {maxpop} and minimum population = {minpop}')
main()
您可以将 values = infile.read()
更改为 values = list(infile.read())
它将以列表而不是字符串的形式输出。
每当读取这样的文件时往往会发生的事情之一是,在每一行的末尾都有一个不可见的 '\n'
声明文本文件中的新行,所以一个简单的方法要按行拆分并将它们转换为整数,而不是使用 values = list(infile.read())
,您可以使用 values = values.split('\n')
来拆分基于行的行,只要之前声明了 values
。
您拥有的 while 循环可以轻松替换为 for 循环,您可以在其中使用 len(values) 作为结尾。
values(index) = int(values(index))
部分是在 while 循环中执行此操作的一种不错的方法,但是无论何时在 for
循环中,您都可以使用 values[i] = int(values[i])
将它们转换为整数,并且然后 values
变成一个整数列表。
我个人的设置方式是:
import os
import math
import statistics
def main ():
infile = open('USPopulation.txt', 'r')
values = infile.read()
infile.close()
values = values.split('\n') # Splits based off of lines
for i in range(0, len(values)) : # loops the length of values and turns each part of values into integers
values[i] = int(values[i])
changes = []
# Use a for loop to get the changes between each number.
for i in range(0, len(values)-1) : # you put the -1 because there would be an indexing error if you tried to count i+1 while at len(values)
changes.append(values[i+1] - values[i]) # This will get the difference between the current and the next.
print('The max change :', max(changes), 'The minimal change :', min(changes))
#And since there is a 'change' for each element of values, meaning if you print both changes and values, you would get the same number of items.
print('A change of :', max(changes), 'Happened at', values[changes.index(max(changes))]) # changes.index(max(changes)) gets the position of the highest number in changes, and finds what population has the same index (position) as it.
print('A change of :', min(changes), 'Happened at', values[changes.index(min(changes))]) #pretty much the same as above just with minimum
# If you wanted to print the second number, you would do values[changes.index(min(changes)) + 1]
main()
如果您需要对我在代码中所做的任何事情进行任何说明,请直接询问。
我目前运行的代码如下
import os
import math
import statistics
def main ():
infile = open('USPopulation.txt', 'r')
values = infile.read()
infile.close()
index = 0
while index < len(values):
values(index) = int(values(index))
index += 1
print(values)
main()
The text file contains 41 rows of numbers each entered on a single line like so:
151868
153982
156393
158956
161884
165069
168088
etc.
我的任务是创建一个程序,显示该时间段内人口的平均变化。在此期间人口增长最多的年份。时间段内人口增长最小的年份(与上一年相比)。
该代码将在一行中打印每个文本文件条目,但在尝试转换为 int 以用于统计数据包时,我收到以下错误:
值(索引)= int(值(索引))
语法错误:无法分配给函数调用
values(index) = int(values(index)) 行取自阅读以及堆栈溢出资源。
我个人会使用 numpy 来读取文本文件。
在你的情况下我会这样做:
import numpy as np
def main ():
infile = np.loadtxt('USPopulation.txt')
maxpop = np.argmax(infile)
minpop = np.argmin(infile)
print(f'maximum population = {maxpop} and minimum population = {minpop}')
main()
您可以将 values = infile.read()
更改为 values = list(infile.read())
它将以列表而不是字符串的形式输出。
每当读取这样的文件时往往会发生的事情之一是,在每一行的末尾都有一个不可见的 '\n'
声明文本文件中的新行,所以一个简单的方法要按行拆分并将它们转换为整数,而不是使用 values = list(infile.read())
,您可以使用 values = values.split('\n')
来拆分基于行的行,只要之前声明了 values
。
您拥有的 while 循环可以轻松替换为 for 循环,您可以在其中使用 len(values) 作为结尾。
values(index) = int(values(index))
部分是在 while 循环中执行此操作的一种不错的方法,但是无论何时在 for
循环中,您都可以使用 values[i] = int(values[i])
将它们转换为整数,并且然后 values
变成一个整数列表。
我个人的设置方式是:
import os
import math
import statistics
def main ():
infile = open('USPopulation.txt', 'r')
values = infile.read()
infile.close()
values = values.split('\n') # Splits based off of lines
for i in range(0, len(values)) : # loops the length of values and turns each part of values into integers
values[i] = int(values[i])
changes = []
# Use a for loop to get the changes between each number.
for i in range(0, len(values)-1) : # you put the -1 because there would be an indexing error if you tried to count i+1 while at len(values)
changes.append(values[i+1] - values[i]) # This will get the difference between the current and the next.
print('The max change :', max(changes), 'The minimal change :', min(changes))
#And since there is a 'change' for each element of values, meaning if you print both changes and values, you would get the same number of items.
print('A change of :', max(changes), 'Happened at', values[changes.index(max(changes))]) # changes.index(max(changes)) gets the position of the highest number in changes, and finds what population has the same index (position) as it.
print('A change of :', min(changes), 'Happened at', values[changes.index(min(changes))]) #pretty much the same as above just with minimum
# If you wanted to print the second number, you would do values[changes.index(min(changes)) + 1]
main()
如果您需要对我在代码中所做的任何事情进行任何说明,请直接询问。