Pick position of a list based on the position of another list in Python

Question:

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. It’s an assignment and we cant use inbuild Python functions.

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. Once done, I have to add the corresponding value in B.
Eg:

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

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)
Asked By: FirstStep

||

Answers:

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)
Answered By: Ghanteyyy

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

Answered By: Sin Han Jinn

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.

Answered By: dgw

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:

total = sum(output_decimal)

if you don’t want to use sum then you can use the code below:

total = 0
for value in output_decimal:
    total += value
Answered By: ZAKI AFFANDI

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)
Answered By: Ivan Perehiniak
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..

Answered By: Hari
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.