简体   繁体   English

根据 Python 中另一个列表的位置选择列表的位置

[英]Pick position of a list based on the position of another list in Python

Im a Python beginner and also new to Stackoverflow cant seem to find a solution to this problem and I have been looking around in weeks.我是 Python 初学者,也是 Stackoverflow 的新手,似乎无法找到解决此问题的方法,我已经四处寻找了数周。 It's an assignment and we cant use inbuild Python functions.这是一项任务,我们不能使用内置 Python 函数。

I want to find the position of an item in list A and choose the same position from list B. The item in list A should not be equal to zero.我想找到一个项目在列表 A 中的位置,并从列表 B 中选择相同的位置。列表 A 中的项目不应该等于零。 Once done, I have to add the corresponding value in B. Eg:完成后,我必须在 B 中添加相应的值。例如:

A = [0,0,1,1]
B = [25,9,8,3]

A should result in position 2,3 B therefore equals to 8,3 8+3 = 11 A 应导致位置 2,3 B 因此等于 8,3 8+3 = 11

Below is what I have tried so far以下是我到目前为止所尝试的

binary = [0,1,1,0,0,0]
decimal = [32,16,8,4,2,1]
output_decimal = []
for position in range(0, len(binary)):
    if binary[position] !=0:
        print(position)

I think this is what you wanted.我想这就是你想要的。

index = 0
sums = 0
binary = [0,1,1,0,0,0]
decimal = [32,16,8,4,2,1]

for value in binary:
    if value == 1:
        sums += decimal[index]

    index += 1

print(sums)

If you don't need the positions you could just use the following如果您不需要这些职位,您可以使用以下内容

result = sum([y for x, y in zip(binary, decimal) if x])

In the list comprehension every pair of binary, decimal positions will be iterated and you only keep the decimal ones if the binary is not zero.在列表理解中,每对二进制、小数位将被迭代,如果二进制不为零,则只保留小数位。 Then you sum up all kept items.然后你总结所有保留的项目。

import numpy as np

binary = np.array([0,1,1,0,0,0])

decimal = np.array([32,16,8,4,2,1])

values = np.where(binary == 1)

output_decimal = decimal[values[0]]

print(output_decimal)

This answer Done By using Numpy Package..这个答案通过使用 Numpy 包完成..

I think you got it.我想你明白了。 From your code, simply register the positions in the separate list that you created and then sum it up从您的代码中,只需在您创建的单独列表中注册位置,然后将其汇总

binary = [0,1,1,0,0,0]
decimal = [32,16,8,4,2,1]
output_decimal = []
for position in range(0, len(binary)):
    if binary[position] !=0:
        output_decimal.append(decimal[position])
# to add everything
print(sum(output_decimal))

Output gives you: 16+8 = 24输出给你: 16+8 = 24

so To find the position of an item in a list and select the same position from another list you can very much use a loop to iterate over the item in the first list.所以要找到列表中项目的位置并从另一个列表中选择相同的位置,您可以非常使用循环来迭代第一个列表中的项目。 Inside the loop, you then check if the item is not equal to zero, and if it isn't, then you can add the appropriate value in the second list to the output list.在循环内,然后检查该项目是否不等于零,如果不是,则可以将第二个列表中的适当值添加到输出列表。

binary = [0,1,1,0,0,0]
decimal = [32,16,8,4,2,1]
output_decimal = []

# Iterate over the items in binary
for i, b in enumerate(binary):
    # Check if the item is not equal to zero
    if b != 0:
        # If it is not, add the corresponding value in decimal to the output list
        output_decimal.append(decimal[i])

# Print the output list
print(output_decimal)

so To count the sum of the values in the output list, you can simply use the built-in sum() function, like this:所以要计算输出列表中值的总和,您可以简单地使用内置的 sum() 函数,如下所示:

total = sum(output_decimal)

if you don't want to use sum then you can use the code below:如果你不想使用 sum 那么你可以使用下面的代码:

total = 0
for value in output_decimal:
    total += value

I think using enumerate may be good idea in this case:我认为在这种情况下使用枚举可能是个好主意:

result = sum([B[i] for i, val in enumerate(A) if val != 0])
print(result)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM