在python中分别将列表中的值相乘 [英] Multiplying values in a list individually in python

查看:798
本文介绍了在python中分别将列表中的值相乘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在python列表上执行单独的数学运算.例如:

I'm trying to perform separate mathematical operations on a python list. As an example:

[1000, 0.15, 1150, 0.1, 0.2, 828, 49.68, 877.68, 100, 977.68]

我想将1000乘以0.15,然后将该值加到1000(结果= 1150).

I would like to multiply 1000 by 0.15, then take that value and add it to 1000 (result = 1150).

然后我想使用同一列表中的值对1150进行其他数学运算(除法,乘法,加法等).

I would then like to carry out other mathematical operations (division, multiplication, addition etc) on the 1150, using values in the same list.

这里最好的一般方法是什么?

What would be the best general approach here?

我不是在寻找任何人将所有代码都写给我,但是numpy是最好的方法吗?我可以使用for循环来实现这一点吗?

I'm not looking for anybody to write me all the code but would numpy be the best approach? Can I achieve this using for loops?

推荐答案

您只需索引列表中的位置即可完成此操作...

You can do this just by indexing the position in the list...

lst = [1000, 0.15, 1150, 0.1, 0.2, 828, 49.68, 877.68, 100, 977.68]
val = lst[0]
val += (val*lst[1])
print(val)
>>> 1150

print(val=lst[2])
>>> True

这篇关于在python中分别将列表中的值相乘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆