简体   繁体   English

如何访问python中列表列表的特定部分以与数学模块一起使用

[英]How to access specific part of a list of lists in python to use with math module

This is an assignment for school.这是学校的作业。 I have a text file which contains the following abbreviated list with each entry on a single line.我有一个文本文件,其中包含以下缩写列表,每个条目都在一行中。 The first entry is the date and the second entry after the pip is the value for stock market close, there are approximately 365 entries in the file.第一个条目是日期,点之后的第二个条目是股市收盘价,文件中大约有 365 个条目。

8/28/2018|26064.01953
8/29/2018|26124.57031
8/30/2018|25986.91992

Using the following code I have split the data into a list of lists with the date and value separated.使用以下代码,我将数据拆分为日期和值分开的列表列表。

import os
import math
import statistics
def main ():
    infile = open('DJI.txt', 'r')
    values = infile.read()
    infile.close()
    values=values.split("\n")
    values=[value.split("|") for value in values]
    print(values)
    avg = sum([float(l[1]) for l in values])/len(values)
main()  

This gives the following output这给出了以下输出

[['8/28/2018', '26064.01953'], ['8/29/2018', '26124.57031'], ['8/30/2018', '25986.91992'],

the Avg line gives the following error: IndexError: list index out of range Avg 行给出以下错误:IndexError: list index out of range

My task is to create a program which calculates Average close value for the entire year.我的任务是创建一个计算全年平均收盘价的程序。 Average close value per month Highest close value and the date in which that happened.每月平均收盘价 最高收盘价和发生的日期。 Lowest close value and the date of which that happened.最低收盘价和发生的日期。 Sort prices lowest to highest and write the sorted list to a new text file called DJI_Sorted.将价格从低到高排序,并将排序后的列表写入名为 DJI_Sorted 的新文本文件。

I am have trouble with how to access the second value in the list of lists to perform the statistics on the file.我在如何访问列表列表中的第二个值以对文件执行统计时遇到问题。 I am also unsure how I would write a code which sorts the list from lowest to highest as well as the average close for each month, rather than on the entire file.我也不确定如何编写一个代码,将列表从最低到最高以及每个月的平均收盘价进行排序,而不是对整个文件进行排序。

Your help is greatly appreciated.非常感谢您的帮助。

You can access each list in your list with values[i] and each element of such a list with values[i][j] .您可以使用values[i]访问列表中的每个列表,并使用values[i][j]此类列表的每个元素。 So the value of the 10th date would be values[9][1] .所以第 10 个日期的值将是values[9][1] Since you know the number of elements per inner list and it is rather small, you could also unpack your lists.由于您知道每个内部列表的元素数量并且相当小,因此您也可以解压缩您的列表。

Example with three elements per inner list: a,b,c = values[i] .每个内部列表包含三个元素的示例: a,b,c = values[i]

You want to iterate over the entire list, so a for-loop is what you need and instead of handling indices you can directly unpack the inner lists in variables with meaningful names.您想遍历整个列表,因此需要一个 for 循环,您可以直接将内部列表解压缩到具有有意义名称的变量中,而不是处理索引。

for date, value in values:
   value = float(value)
   if value > highest:
       highest = value
       highest_date = date

Another option would be a list comprehension:另一种选择是列表理解:

avg = sum([float(l[1]) for l in values])/len(values)

Since this is your homework, I don't want to give you a complete solution, but this should be enough to solve all your questions described above.既然这是你的功课,我不想给你一个完整的解决方案,但这应该足以解决你上面描述的所有问题。 One last tip: for monthly statistics you need to further split the date, then there are multiple options to go from there (saving it the values for each month in one list/dictionary, or computing the on the fly)最后一个提示:对于每月的统计数据,您需要进一步拆分日期,然后有多种选择(将每个月的值保存在一个列表/字典中,或即时计算)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM