問題描述
所以我是 python 新手,并且已經搜索過這個答案,但大多數回復都在我的腦海中.我有一個這樣的列表:
So I am new to python and have searched for this answer but most responses are over my head. I have a list like this:
right point point 1.76999998093
right fear fear 1.62700009346
right sit sit 1.46899986267
right chord chord 1.47900009155
right speed speeed 1.71300005913
right system system 1.69799995422
right hard hard 1.4470000267
right excite excite 2.93799996376
right govern govern 1.85800004005
right record record 1.62400007248
我正在嘗試將列表拆分為列并找到數字的均值/總和/標準差.所以基本上我只是想把最后一個變成一個數組形式,我可以使用 np.mean、np.sum 等.數據在一個名為正確"的文件中這是我目前所擁有的:
I am trying to split the list into columns and find the mean/sum/std dev of the numbers. So basically i am just trying to get the last into an array form I can use np.mean, np.sum, etc with. The data is in a file called 'right' Here is what I have so far:
right=open('right.txt').readlines()
for line in right:
l=line.split()
righttime=l[3]
print righttime
rightsum=np.sum(righttime)
rightmean=np.mean(righttime)
然后我得到這個錯誤:TypeError: cannot perform reduce with flexible type"我已經嘗試了很多方法并且不斷出錯.這是我嘗試過的另一種看起來很有希望的方法:
Then I get this error: "TypeError: cannot perform reduce with flexible type" I have tried it tons of ways and keep getting errors. This is another way I tried that seemed promising:
def TimeSum(data):
for line in data:
l=line.split()
righttime=l[3]
print righttime
return righttime
rightsum=np.sum(TimeSum(right))
但我有同樣的錯誤.有誰知道怎么做?
But I had the same error. Does anyone know how to do this?
推薦答案
生成一個列表并對元素求和:
Generate a list and sum the elements:
import numpy as np
right = open('right.txt').readlines()
mylist = []
for line in right:
l = line.split()
mylist.append(float(l[3])) # add to list "mylist"
rightsum = np.sum(mylist)
print rightsum
或者,也可以
mylist = [float(line.split()[3]) for line in right] # generate numbers list
print np.sum(mylist) # sum numbers
這篇關于在列表中查找列的總和得到“TypeError: cannot perform reduce with flexible type"的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!