How do I match an Index in 1 list with a different list index value and output the 2nd list values?

Question:

So for context, I am building a program where the user inputs 6 binary digits which are stored in List 1. I then need to see if there are any digits of value 1 in this list.
If there is a value of 1 at Index[0] then in list 2 Index[0] has value 32. The output needs to be from list 2.
So for example, the user inputs a 6 digit binary of 101001 making list 1 [1, 0, 1, 0, 0, 1]….. List 2 already have set values of [32, 16, 8, 4, 2, 1]…. Therefore, output needs to be, [32, 8, 1] to represent the 1 values in list 1.
Below is what I have in terms of setup of lists and validation for input.

binaryList = []
binaryWeights = [32, 16, 8, 4, 2, 1]

while len(binaryList) < 6:
    digit = int(input('Please enter your binary digit: '))
    if digit not in range(0, 2):
        print('Not a valid binary digit try again')
        continue
    else:
        binaryList.append(digit)


print(binaryList)

for i in range(len(binaryList)):
    if i == 1:

Any help would be greatly appreciated as my head is fried trying to think of a solution after hours of tinkering.

Asked By: Daniel

||

Answers:

ans=[]

for i in range(len(binaryList)):
    j=-1*(i+1)
    if binaryList[j] == 1:
      ans = [binaryWeight[j]]+ans

print(ans)

You can use above code for your task. It loop through binaryList digits and select mapped number from binaryWeights when binaryList digit is equal 1.Note that loop in reverse order.

Answered By: YJR

Let’s say you’ve created binaryList as [1, 0, 1, 0, 0, 1] like you’ve indicated.

Think about what’s actually happening in your for loop:

for i in range(len(binaryList)):

i is going to be 0, 1, 2, 3, 4, 5, so i == 1 will only happen one time in that loop.

What you’re actually trying to do is see if the value of binaryList is 1. There are a couple of ways to do this.

First, you could access the value of binaryList at the index i like this:

for i in range(len(binaryList)):
   if binaryList[i] == 1:

Or you can use the handy enumerate() method, which provides both the index and the value of an iterable.

for index, value in enumerate(binaryList):
    if value == 1:

Next, I’m assuming you want to create a new list based on the values in binaryWeights. Let’s just call that newList (and make sure you define it earlier in your code like you did with binaryList. Then you can do the following:

for index, value in enumerate(binaryList):
    if value == 1:
        newList.append(binaryWeights[index])

Notice that we use the index to access the value of the position in binaryWeights that matches the position of the value 1 in binaryList. Also note that you don’t need to have an else with an if statement. Python will just ignore cases that don’t match the if statement.

Answered By: Andrew

I think the most starightforward way to do this is using a small list comprehension. You can use enumerate to iterate over values and indices at the same time.

binaryList = [1, 0, 1, 0, 0, 1]
binaryWeights = [32, 16, 8, 4, 2, 1]

[binaryWeights[i] for i,b in enumerate(binaryList) if b]
# [32, 8, 1]
Answered By: Rodrigo Rodrigues
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.