[英]How can I sort the float list
I was writing this code for downloading the stock data and finding the percentage change in the price over the given time span.我正在编写这段代码来下载股票数据并查找给定时间跨度内价格的百分比变化。 After doing all the calculations, retrievals, and storing the final data in a list, I wanted to sort the change list, but I was unable to do so.
在完成所有计算、检索并将最终数据存储在列表中之后,我想对更改列表进行排序,但我无法这样做。 Please help me!
请帮我!
from pandas_datareader import data
import matplotlib.pyplot as plt
import pandas as pd
from datetime import date
import numpy as np
change=[]
today = date.today()
tickers = ['^GSPC', 'AAPL', 'MSFT', 'GOOG', 'MS', 'JPM']
for tick in tickers:
start_date = '2012-01-01'
end_date = today
dataset = data.DataReader(tickers, 'yahoo', start_date, end_date)
close=dataset['Close']
close_new=close.shift(1)
close2=close-close_new
close3=close2.dropna()
u=close.loc['2012-01-04',tick]
v=close.loc['2020-06-22',tick]
c=((v-u)/u)*100
change.append(c)
print(change.sort())
The list.sort()
method, modifies the list in-place (and returns None
). list.sort()
方法就地修改列表(并返回None
)。 Usually it's less convenient than sorted()
- but if you don't need the original list, it's slightly more efficient.通常它不如
sorted()
方便 - 但如果您不需要原始列表,它的效率会稍高一些。 Anyway, you can use sorted
:无论如何,您可以使用
sorted
:
print(sorted(change))
or numpy.sort
:或
numpy.sort
:
print(np.sort(change))
sort()
function does not return anything. sort()
function 不返回任何内容。 So please switch your last line to following 2 lines:因此,请将最后一行更改为以下 2 行:
change.sort()
print(change)
You can also use sorted
if you want to get a new sorted list.如果你想得到一个新的排序列表,你也可以使用
sorted
。
print(sorted(change))
You can basically use sorted()
If you just wanna sort it to print you can make that您基本上可以使用
sorted()
如果您只想对其进行排序以打印,则可以这样做
print(sorted(change)) # Or whatever your list name
Or you can re-define the variable if you wanna reuse the list或者,如果您想重用列表,您可以重新定义变量
change = sorted(change)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.