【问题标题】:Display the same indexed value from 3 lists in python stock script在 python 股票脚本中显示来自 3 个列表的相同索引值
【发布时间】:2017-11-04 17:51:08
【问题描述】:

目标

我的目标是拥有 3 个股票列表,并根据用户输入的匹配值显示每个列表的相同索引位置。

我的 3 个列表

Price = [37.10, 46.18, 51.76, 145.64]

Symbol = ['T', 'KO', 'ABQ', 'LOW']

Name = ['ATT', 'COCA COLA COMPANY', 'ABBOT LABORATORIES', 'LOWES COMPANY INC']

我的完整脚本会要求用户提供investment amount and if below $1000 it'll invest in the cheapest stock

If over $1000, it'll invest in the most expensive stock.

所以if under $1000, it should invest all the money into ATT at $37.10 per share

如果over $1000, it should invest all the money into LOW at $145.64 a share.

输出应显示其投资的给定股票的股票购买量。

输出给用户

您今天想投资多少? 1001.57

以下是可供购买的库存:

Price | Symbol | Name 

$ 37.1 | T | ATT

$ 46.18 | KO | COCA COLA COMPANY

$ 51.76 | ABT | ABBOTT LABORATORIES

$ 145.64 | LOW | LOWES COMPANIES INC

这将以每股 37.1 美元的价格购买 26.99 股“T:ATT”。

这将以每股 145.64 美元的价格购买 6.87 股“LOW:LOWES COMPANIES INC”。

我的代码如下:

investing = (float(input("How much are you wanting to invest today? ")))

print ("\nBelow are the stocks available for purchase:")

print ("\nLast Trade | Symbol | Name ")

for pr, sy, na, in sorted(zip(stock_price, stock_symbol, stock_name)):
    print ('$',pr, '|',sy, '|',na)

cheapest_stock = (min(stock_price))

expensive_stock = (max(stock_price))

purchase_low = (investing/cheapest_stock) // 0.01 / 100

purchase_hi = (investing/expensive_stock) // 0.01 / 100


print (f"\nThis will purchase {purchase_amt} shares of 'T:ATT' at ${cheapest_stock} per share.")

print (f"\nThis will purchase {purchase_amt} shares of 'LOW:ATT' at ${expensive_stock} per share.")

问题 我不知道如何获得由min(stock_price) 确定的低值或由max(stock_price) 确定的高值以显示其他列表中股票代码和股票名称的匹配值。

所以我目前在上面的打印语句中手动输入了它。

【问题讨论】:

  • 非常感谢!效果很好!
  • 如果答案对你有帮助,别忘了将答案标记为“已接受答案”

标签: python-3.x


【解决方案1】:

获取价格列表中最小/最大元素的索引,并从其他列表中找到该索引中的元素。

例如:如果您想要最低价格的符号,那么下面的代码会给出所需的结果。

index=Price.index(min(Price)) #Gives the index of Minimum element in Price list
print(Symbol[index]) #prints the value of an element at an above index from list Symbol.

#or we can write as
Symbol[Price.index(min(Price))]

使用 index() 获取元素的索引。

lis.index("foo") #this code gives index of element "foo" in list lis.

【讨论】:

  • 最后一个请求。当我投资 745.87 购买便宜的金额时,我希望 ti 显示 2 个小数位。所以 20.10 股,每股 37.10 美元。我尝试将 %.20f 添加到我的打印语句中,但它不起作用。 print (f"\n{guest},我们将投资您的 ${investing} 以 ${cheapest_stock} 购买股票代码:'{symbol_lo}' 和股票名称:'{name_lo}' 的 {purchase_amt} 股美元每股。”)伙计,我们将以每股 37.1 美元的价格投资您的 745.87 美元购买 20.1 股股票代码:“T”和股票名称:“ATT”。
  • @cyberjax21 print("%s, we will invest your amount of %.2f in purchasing %.2f shares of the Stock Symbol: %s and Stock Name: %s at $%.2f dollars per share." %(guest,investing,purchase_amt,symbol_lo,name_lo,cheapest_stock)) #This code gives the desired result.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多