Afir
Afir

Reputation: 483

Iteration of list in Python

I have a list of bit 0 and 1. I need to iterate it one by one using looping and do the calculation. This is the code:

bit=['1','0','0','1']
message=''.join(bit)
print (message)

if (message==1):
    theta=0
else:
    theta=45

HWPX=[0, math.cos(4*math.radians(theta)), math.sin(4*math.radians(theta)), 0]
print (HWPX)

However, after I run this code, only the first bit will be calculated.

The result should produce like this:

[0, 1.0, 0.0, 0], [0, -1.0, 1.2246467991473532e-16, 0],[0, -1.0, 1.2246467991473532e-16, 0],[0, 1.0, 0.0, 0]

How to produce the desired result? Thank you.

Upvotes: 2

Views: 180

Answers (2)

RoadRunner
RoadRunner

Reputation: 26315

In your example, you compare message with integers in this conditional:

if (message==1):

Which will never work since 1 is not the string '1'. To fix this, you need to compare == instead with strings:

if (message=='1'):

You also need to loop over each of these bits and append the calculations to a list. Your current example doesn't support this.

One simple approach to this problem is to store the theta mappings in a dictionary, loop over each bit in bit and append the correct calculations. You also don't need to .join() the list into a bit string beforehand, since you loop over each item anyway. A design decision could be to keep it as a string to begin with, such as '1001', but this is up to you.

Demo:

import math

bit = ['1','0','0','1']

theta = {'0': 45, '1': 0}

result = []
for b in bit:
    hwpx = [0, math.cos(4*math.radians(theta[b])), math.sin(4*math.radians(theta[b])), 0]
    result.append(hwpx)

print(result)

Output:

[[0, 1.0, 0.0, 0], [0, -1.0, 1.2246467991473532e-16, 0], [0, -1.0, 1.2246467991473532e-16, 0], [0, 1.0, 0.0, 0]]

or as a list comprehension:

result = [[0, math.cos(4*math.radians(theta[b])), math.sin(4*math.radians(theta[b])), 0] for b in bit]

By using a dictionary in the above approach, there is no need for any conditionals, since you can use the mapped theta value depending on the bit string found. Then all you need is a simple loop and some calculations to worry about.

If you don't want to use a dictionary here, then conditionals work fine:

import math

bit = ['1','0','0','1']

result = []
for b in bit:
    theta = 0

    if b == '0':
        theta = 45

    hwpx = [0, math.cos(4*math.radians(theta)), math.sin(4*math.radians(theta)), 0]
    result.append(hwpx)

print(result)
# [[0, 1.0, 0.0, 0], [0, -1.0, 1.2246467991473532e-16, 0], [0, -1.0, 1.2246467991473532e-16, 0], [0, 1.0, 0.0, 0]]

Upvotes: 3

niraj
niraj

Reputation: 18208

I think you need to iterate through character of message, you can try as following:

import math

bit=['1','0','0','1']
message=''.join(bit)
print (message)

result = []
for i in message:
    if (i=='1'):
        theta=0
    else:
        theta=45

    HWPX=[0, math.cos(4*math.radians(theta)), math.sin(4*math.radians(theta)), 0]
    result.append(HWPX)
print (result)

Or using list comprehension with if and else:

import math

bit=['1','0','0','1']
message=''.join(bit)
print (message)

HWPX = [[0, math.cos(4*math.radians(0)), math.sin(4*math.radians(0)), 0] if i=='1'
            else [0, math.cos(4*math.radians(45)), math.sin(4*math.radians(45)), 0]  
                 for i in bit]
print(HWPX)

Upvotes: 4

Related Questions