python - Python 列表中的位置

标签 python

我正在尝试编写一个代码,以便在每个元素与列表中的所有其他元素之间进行操作,但不与其自身进行操作。下面是代码。

list = [10,20,30,30,40,50,50,50,60,70];
for i in list:
        sum=i;
        for j in list:
                if list.index(i) != list.index(j):
                       s=(50-((j-i)/2))*0.13;
                       sum+=s;  
        print("score of %d is %f"%(i,sum));

但是代码仍然无法工作。它不满足仓位的 if 条件。

最佳答案

您可以使用enumerate来获取索引。这可能会做你想做的事:

lis = [10, 20, 30, 30, 40, 50, 50, 50, 60, 70]

for idx1, el1 in enumerate(lis):
    sum_ = el1
    for  idx2, el2 in enumerate(lis):
        if idx1 != idx2:
             sum_ += (50 - (el2 - el1)/2) * 0.13
        print("score of %d is %f"%(idx1, sum_))

关于python - Python 列表中的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36417361/

相关文章:

python - 通过服务帐户使用 Google People API

python - 合并 2 个具有多个变量的数据集(不能仅使用相似的变量)

python - 使用 for 循环和 if 语句的最长重复子串

Python循环遍历文件夹并重命名文件

python - 基于另一个数据框在 Pandas 中创建新列

python - cron任务设置环境变量失败

python - initializer is not a constant, error C2099, 在为 python 编译用 c 编写的模块时

python - Python中的静态类

python - shell 中 `set -x` 的 Python 等价物是什么?

python - 调用返回 FloatType() 的 UDF 时为 "expected zero arguments for construction of ClassDict (for numpy.dtype)"