Python—从';对于循环';并合并成一个列表
我当前的输出是单个浮点数,不适合我的下一步:sum()和statistics.mean()。我曾尝试过嵌套列表理解,但当这不起作用时,我尝试了下一个嵌套循环,但我得到了相同的错误-TypeError:“float”对象不可iterable。通过使用[ss],每个输出都会列出,但不会合并到单个列表中 感谢您的帮助。如果您需要澄清或有问题,请随时提问Python—从';对于循环';并合并成一个列表,python,python-3.x,list,for-loop,list-comprehension,Python,Python 3.x,List,For Loop,List Comprehension,我当前的输出是单个浮点数,不适合我的下一步:sum()和statistics.mean()。我曾尝试过嵌套列表理解,但当这不起作用时,我尝试了下一个嵌套循环,但我得到了相同的错误-TypeError:“float”对象不可iterable。通过使用[ss],每个输出都会列出,但不会合并到单个列表中 感谢您的帮助。如果您需要澄清或有问题,请随时提问 import pandas as pd import numpy as np import math import statistics frame
import pandas as pd
import numpy as np
import math
import statistics
frame=[bdrc,bdmp,bdmv,bdsm] #These are sources selected and then concated for variable, popPrices.
result=pd.concat(frame)
popPrices=result["Price"]
#Grand Mean
xpop=popPrices.mean() #The mean
for popsq in popPrices: #An attempt to have each individual sample treated with the grand mean. - success.
ss=math.pow(popsq - xpop,2)
print(ss) #This will print floats individually, but need it in a list.
电流浮动输出:
244107.59945389628
54722.0922075194
6765577.961772737
643320.2371350557
...
...
通缉名单输出:
[244107.59945389628, 54722.0922075194, 6765577.961772737, 643320.2371350557, ..., ...]
使用列表理解
ss=[math.pow(popsq - xpop,2) for popsq in popPrices]
您需要将这些项目添加到列表中;现在您只是将它们赋给一个变量。试试这个:
resultList = []
for popsq in popPrices:treated with the grand mean. - success.
resultList.append(math.pow(popsq - xpop,2))
print(resultList)
您可以创建一个空列表并附加结果
result=[]
for popsq in popPrices:
ss=math.pow(popsq - xpop,2)
result.append(ss)
result=[]
for popsq in popPrices:
ss=math.pow(popsq - xpop,2)
result.append(ss)