如何从列表中的 for 循环获取值?
How do I get values from for loop in a list?
我试图在列表中打印下面给出的 for 循环的输出,但我得到以下结果:
import math
S= [(1,2),(3,4),(-1,1),(6,-7),(0, 6),(-5,-8),(-1,-1),(6,0),(1,-1)]
p,q = 3,-4
dist = []
for x,y in S:
dist=[]
cos_dist = math.acos((x*p + y*q)/((math.sqrt(x**2 + y**2))*(math.sqrt(p**2 + q**2))))
dist.append(cos_dist)
print(dist)
在这里,输出是:
[2.0344439357957027]
[1.8545904360032246]
[2.9996955989856287]
[0.06512516333438509]
[2.498091544796509]
[1.2021004241368467]
[1.4288992721907328]
[0.9272952180016123]
[0.14189705460416438]
但我希望它是:
[2.0344439357957027,1.8545904360032246,2.9996955989856287,0.06512516333438509,2.498091544796509,1.2021004241368467,1.4288992721907328,0.9272952180016123,0.14189705460416438]
我试过使用
print(','.join(dist))
但是它说
TypeError: sequence item 0: expected str instance, float found
如何获得我想要的输出?
首先,将 dist
初始化移到循环之外。
然后你必须在加入它们之前将浮点数转换为字符串,或者在循环中:
dist.append(str(cos_dist))
或者在循环之后:
print(','.join(map(str, dist)) )
总的来说,列表理解是一个更好的列表构建工具:
dist = [math.acos((x*p + y*q) / ((math.sqrt(x**2 + y**2))\
*(math.sqrt(p**2 + q**2)))) for x,y in S]
如果你仔细观察:
for x,y in S:
dist=[]
您每次都在分配 dist=[]
。因此,在每次迭代中,先前的值都会被丢弃,并创建一个名为 dist
的新空白列表。这就是为什么,你只能得到 1 个元素。
而是删除定义列表的行。另外,将 print(dist)
移到
之外
import math
S= [(1,2),(3,4),(-1,1),(6,-7),(0, 6),(-5,-8),(-1,-1),(6,0),(1,-1)]
p,q = 3,-4
dist = []
for x,y in S:
cos_dist = math.acos((x*p + y*q)/((math.sqrt(x**2 + y**2))*(math.sqrt(p**2 + q**2))))
dist.append(cos_dist)
print(dist)
备选方案:
dist=[math.acos((x*p + y*q)/((math.sqrt(x**2 + y**2))*(math.sqrt(p**2 + q**2)))) for x,y in S]
试试这个:
import math
S= [(1,2),(3,4),(-1,1),(6,-7),(0, 6),(-5,-8),(-1,-1),(6,0),(1,-1)]
p,q = 3,-4
dist = []
for x,y in S:
cos_dist = math.acos((x*p + y*q)/((math.sqrt(x**2 + y**2))*(math.sqrt(p**2 + q**2))))
dist.append(cos_dist)
print(dist)
第 1 点,您的列表声明应该在 for 循环之外,否则您将只有列表中的最新元素。第 2 点,您应该在循环外部打印以打印您在预期输出中显示的所有元素。这会做的事情,不需要 ','.join().
如果您想使用连接,请使用
print('[' + ','.join(map(str, dist)) + ']')
这将给出相同的结果。
我试图在列表中打印下面给出的 for 循环的输出,但我得到以下结果:
import math
S= [(1,2),(3,4),(-1,1),(6,-7),(0, 6),(-5,-8),(-1,-1),(6,0),(1,-1)]
p,q = 3,-4
dist = []
for x,y in S:
dist=[]
cos_dist = math.acos((x*p + y*q)/((math.sqrt(x**2 + y**2))*(math.sqrt(p**2 + q**2))))
dist.append(cos_dist)
print(dist)
在这里,输出是:
[2.0344439357957027]
[1.8545904360032246]
[2.9996955989856287]
[0.06512516333438509]
[2.498091544796509]
[1.2021004241368467]
[1.4288992721907328]
[0.9272952180016123]
[0.14189705460416438]
但我希望它是:
[2.0344439357957027,1.8545904360032246,2.9996955989856287,0.06512516333438509,2.498091544796509,1.2021004241368467,1.4288992721907328,0.9272952180016123,0.14189705460416438]
我试过使用
print(','.join(dist))
但是它说
TypeError: sequence item 0: expected str instance, float found
如何获得我想要的输出?
首先,将 dist
初始化移到循环之外。
然后你必须在加入它们之前将浮点数转换为字符串,或者在循环中:
dist.append(str(cos_dist))
或者在循环之后:
print(','.join(map(str, dist)) )
总的来说,列表理解是一个更好的列表构建工具:
dist = [math.acos((x*p + y*q) / ((math.sqrt(x**2 + y**2))\
*(math.sqrt(p**2 + q**2)))) for x,y in S]
如果你仔细观察:
for x,y in S:
dist=[]
您每次都在分配 dist=[]
。因此,在每次迭代中,先前的值都会被丢弃,并创建一个名为 dist
的新空白列表。这就是为什么,你只能得到 1 个元素。
而是删除定义列表的行。另外,将 print(dist)
移到
import math
S= [(1,2),(3,4),(-1,1),(6,-7),(0, 6),(-5,-8),(-1,-1),(6,0),(1,-1)]
p,q = 3,-4
dist = []
for x,y in S:
cos_dist = math.acos((x*p + y*q)/((math.sqrt(x**2 + y**2))*(math.sqrt(p**2 + q**2))))
dist.append(cos_dist)
print(dist)
备选方案:
dist=[math.acos((x*p + y*q)/((math.sqrt(x**2 + y**2))*(math.sqrt(p**2 + q**2)))) for x,y in S]
试试这个:
import math
S= [(1,2),(3,4),(-1,1),(6,-7),(0, 6),(-5,-8),(-1,-1),(6,0),(1,-1)]
p,q = 3,-4
dist = []
for x,y in S:
cos_dist = math.acos((x*p + y*q)/((math.sqrt(x**2 + y**2))*(math.sqrt(p**2 + q**2))))
dist.append(cos_dist)
print(dist)
第 1 点,您的列表声明应该在 for 循环之外,否则您将只有列表中的最新元素。第 2 点,您应该在循环外部打印以打印您在预期输出中显示的所有元素。这会做的事情,不需要 ','.join().
如果您想使用连接,请使用
print('[' + ','.join(map(str, dist)) + ']')
这将给出相同的结果。