汇总浮点列表(Python)

汇总浮点列表(Python),python,Python,输入文件采用以下格式: Britany 6.06 5.31 4.34 8.60 4.14 3.12 3.53 5.16 Eula 6.46 9.84 7.17 4.89 6.24 8.82 4.31 9.08 Georgianna 0.52 6.95 6.67 5.54 8.27 0.57 8.42 2.76 Serina 3.07 9.22 3.59 0.89 3.91 9.79 6.48 7.81 以下是我目前的代码: empty={} floatList

输入文件采用以下格式:

Britany     6.06 5.31 4.34 8.60 4.14 3.12 3.53 5.16
Eula        6.46 9.84 7.17 4.89 6.24 8.82 4.31 9.08
Georgianna  0.52 6.95 6.67 5.54 8.27 0.57 8.42 2.76
Serina      3.07 9.22 3.59 0.89 3.91 9.79 6.48 7.81
以下是我目前的代码:

empty={}
floatList=[]
infile= open(userOutputFile,'r',encoding='UTF8')
for line2 in infile:
    tempList2= line2.split()[1:]
    tempList2.remove(max(tempList2))
    tempList2.remove(min(tempList2))
for i in tempList2:
    tempList2 = float(i)
    floatList.append(tempList2)
我试图做的是将文件的行分割成一个字符串列表,然后删除最大值和最小值。这似乎是可行的,但是,当我试图计算每一行的总和,然后将该总和分配给一个字典,其中相应的名称作为键,值作为总和时,我遇到了问题。基本上,字典会读取类似Britany(原始文件中的键):43.01(计算的总数)的内容。

适用于此:

In [23]: import pandas as pd

In [24]: df = pd.read_table('t.txt', header=None, delimiter=' \w')

In [25]: df.sum(axis=1)
Out[25]:
0    2.26
1    3.81
2    4.70
3    4.76
dtype: float64

In [28]: dict(zip(df[0], df.sum(axis=1)))
Out[28]:
{'Britany    ': 2.2600000000000007,
 'Eula       ': 3.8099999999999996,
 'Georgianna ': 4.7000000000000002,
 'Serina     ': 4.7599999999999998}

In [29]: df.min(axis=1)
Out[29]:
0    0.06
1    0.08
2    0.27
3    0.07
dtype: float64

一件很明显的事情是,您将从到字典的值和的相加与实际迭代分开,并每次重新定义列表

例如:

empty={}
floatList=[]
infile= open(userOutputFile,'r',encoding='UTF8')
for line2 in infile:
    tempList2= line2.split()[1:]
    tempList2.remove(max(tempList2))
    tempList2.remove(min(tempList2))
print tempList2
我得到['3.07','9.22','3.59','3.91','6.48','7.81',],它们只是最后一行的中间值

因此,您应该执行以下操作:

empty={}
infile= open(userOutputFile,'r').readlines()
for line2 in infile:
    name = line2.split()[0]    #Sets the name to be the initial value
    tempList2= line2.split()[1:]
    tempList2.remove(max(tempList2))
    tempList2.remove(min(tempList2))

    sum = 0

    for i in tempList2:
        i_val = float(i)
        sum += i_val      #Sums iteratively over each value in tempList2

    empty[name] = sum
现在做:

空的 {'Georgianna':30.75999998,'Serina':34.08,'Eula':42.66,'Britany':28.54}

同一次迭代对值求和,并将它们附加到字典的名称下。

注意:

  • 在消除最小值和最大值之前,您应该将内容转换为浮点值。您现在正在进行字符串比较,在这种情况下,
    “10.0”
    True
  • Python有一个
    sum
    函数,可以为您汇总列表中的所有数字
  • 几张清单将大大简化事情
这就是我想到的:

infile = open(userOutputFile,'r',encoding='UTF8')
lines = ( line.split() for line in infile )
result = { xs[0]: sum(sorted(map(float, xs[1:]))[1:-1]) for xs in lines }
sum(排序(map(float,xs[1:]))[1:-1])
部分可能有点难以理解。这就是它所做的,从内到外:

  • 抓取行中的所有数字(除第一个条目外的所有数字)
  • 将数字的字符串表示形式转换为浮点数
  • 对产生的浮动进行排序
  • 抓取除第一个和最后一个之外的所有对象(排序后的最小值和最大值)
  • 把它们都总结起来
  • 提供以下输出:

    {'Eula': 42.66, 'Georgianna': 30.759999999999998, 'Britany': 28.54, 'Serina': 34.08}
    

    嘿,我已经稍微编辑了你的文章-可运行的代码片段目前只适用于HTML/Javascript,Python代码应该放在标准代码块中(缩进四个空格)
    {'Eula': 42.66, 'Georgianna': 30.759999999999998, 'Britany': 28.54, 'Serina': 34.08}