Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python中列表的迭代_Python_Loops - Fatal编程技术网

Python中列表的迭代

Python中列表的迭代,python,loops,Python,Loops,我有一个0位和1位的列表。我需要使用循环一个接一个地迭代它并进行计算。代码如下: 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) 但是,在我运行这

我有一个0位和1位的列表。我需要使用循环一个接一个地迭代它并进行计算。代码如下:

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)
但是,在我运行这段代码之后,将只计算第一位

结果应该是这样的:

[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]
如何产生期望的结果?
谢谢。

我认为您需要遍历
消息的字符,您可以尝试以下操作:

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)
或者将
列表理解
if
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)

在您的示例中,将
消息
与以下条件中的整数进行比较:

if (message==1):
因为
1
不是字符串
'1'
,所以它永远不会工作。要解决此问题,您需要将
==
与字符串进行比较:

if (message=='1'):
您还需要循环这些位,并将计算附加到列表中。您当前的示例不支持这一点

解决这个问题的一个简单方法是将θ映射存储在字典中,循环
位中的每个位,并附加正确的计算。您也不需要事先将列表
.join()
连接到一个位字符串中,因为您无论如何都要循环每个项目。设计决策可以是将其保留为字符串,例如
'1001'
,但这取决于您

演示:

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)
[[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]]
输出:

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)
[[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]]
或作为列表:

通过在上述方法中使用字典,不需要任何条件,因为您可以根据找到的位字符串使用映射的θ值。然后,您只需要一个简单的循环和一些需要担心的计算

如果您不想在这里使用字典,那么条件句也可以:

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]]

你的标题意味着你知道你需要一个循环,但这里没有循环。。。