python - 从python中的列表中减去每个索引处的值
问题描述
嗨,我想从 x 获取每个索引处的 x 值,以计算 y,最好对所有索引使用 for 循环
x = [1,2,3,4,5,6,7,8,9,10]
indexes_for_x =[0,1,2,3]
#subtract values of x at each index from x to calculate y
# for index 0 since x = 1 result should look like below
y_at_index_0 = [0,1,2,3,4,5,6,7,8,9]
解决方案
我希望我已经正确理解了你的问题。如果你想从中减去每个值,x[<indexes_for_x>]
你可以这样做:
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
indexes_for_x = [0, 1, 2, 3]
for v in indexes_for_x:
print(f"y_at_index_{v} = {[w - x[v] for w in x]}")
印刷:
y_at_index_0 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
y_at_index_1 = [-1, 0, 1, 2, 3, 4, 5, 6, 7, 8]
y_at_index_2 = [-2, -1, 0, 1, 2, 3, 4, 5, 6, 7]
y_at_index_3 = [-3, -2, -1, 0, 1, 2, 3, 4, 5, 6]
推荐阅读
- c# - 如何在 SQL 中创建一个列作为 .NET Core MVC 中的数据数组?
- nginx - 网络调用中带有 nginx 的 502 网关错误
- c# - 滚动到页面底部并检查是否在最后 Selenium C#
- amazon-dynamodb - 在 DynamoDb 中,有没有办法在编写项目之前自动检查项目的二级索引?
- sql - 复制 JSON 文件并从用户数据库加载
- java - Jacoco 综合报告显示 0% 覆盖率
- wordpress - 带有 WordPress 页面加载的 Docker 非常缓慢
- azure - 无法使用 Azure SSH Web 应用复制文件
- for-loop - 如何检查C++中输入行中是否有空格
- c++ - C++ 应用一个函子,该函子存储在指向 Eigen unaryExpr 的共享指针向量中