List具有字符串格式的多种数据类型

时间:2016-04-08 15:08:22

标签: python python-2.7

我给出了成绩列表

# Grades.py
grades=["D 100","D 50", "D 92","D 87","M 83","D 0","D 73","M 86"]

作业的目标是计算我认为应该在87%左右的成绩的平均值,但首先我需要用100来取代最低等级,所以' D 0'现在应该等于D 100'。接下来,您需要从列表中删除最低等级。应删除D 73。新列表应如下所示

# Grades.py
grades=["D 100","D 92","D 87","M 83","D 100","D 73","M 86"]

接下来我需要平均成绩。 (D)每日= 40%且M(主要)= 60%

因此,在某种程度上找到最低等级将其更改为100并完全删除最低等级我需要一种方法将列表转换为整数,但是当我转换为整数时我无法跟踪什么每天都是重要的。所以我的主要问题之一是如何将字符串列表转换为整数并仍然知道什么是主要的以及每天是什么?我也试过想到一种将列表转换为字典的方法,但由于有多个D和M,所以没有办法创建字典...... 我踩了。所以,如果你能提供一个关于你的思考过程和我需要做的工作的详细答案,我将非常感谢,我将标记你的答案。

到目前为止,这是我的代码..

# Grades.py
grades=["D 100","D 50", "D 92","D 87","M 83","D 0","D 73","M 86"]

intg = [int(i[2:]) for i in grades] #makes a list of ints 
print intg # [100, 50, 92, 87, 83, 0, 73, 86]

最终输出应为87%

提前致谢

  • Allen(avbirm)

4 个答案:

答案 0 :(得分:1)

分步解决方案:

In [1]: grades=["D 100","D 50", "D 92","D 87","M 83","D 0","D 73","M 86"]

In [2]: weights = {'D': 0.4, 'M': 0.6}

In [3]: temp = sorted([(g[0], int(g[2:])) for g in grades], key=lambda (_, g): g)

In [4]: temp
Out[4]: 
[('D', 0),
 ('D', 50),
 ('D', 73),
 ('M', 83),
 ('M', 86),
 ('D', 87),
 ('D', 92),
 ('D', 100)]

In [5]: grades = temp[2:] + [(temp[0][0], 100)]

In [6]: sum(weights[w]*x for w, x in grades) / sum(weights[w] for w, x in grades)
Out[6]: 88.18750000000003

答案 1 :(得分:1)

code = [ grade.split()[0] for grade in grades ]
num = num = [ 100 if not int( grade.split()[1]) else int( grade.split()[1]) for grade in grades ] # replace 0 to 100

indexs  = [ i for i,item in enumerate(num) if item == min(num) ] # find min 
#remove min in code and num
num = [ item for i, item in enumerate(num) if i not in indexs]
code = [ item for i, item in enumerate(code) if i not in indexs]

#weight grade based on code then average
numD= [ n for c,n in zip(code,num) if c == 'D' ] 
numM= [ n for c,n in zip(code,num) if c == 'M' ]
final  =  sum(numD)*0.4/len(numD) +sum(numM)*0.6/len(numM)

输出

86.86

答案 2 :(得分:0)

grades=["D 100","D 50", "D 92","D 87","M 83","D 0","D 73","M 86"]
summD=0
summDi=0
summM=0
summMi=0
for i in grades:
    if i[0]=='D':
        summD+=int(i[2:])
        summDi+=1
    elif i[0]=='M':
        summM+=int(i[2:])
        summMi+=1
summ=((summD/summDi)*0.4)+((summM/summMi)*0.6)
print summ

答案 3 :(得分:0)

将每种类型(每日或主要)的所有值相加,除以该类型中的值的数量,然后应用加权。

from __future__ import division # If using Python 2.x

WEIGHTS = {'D': 0.4, 'M': 0.6}

grades=["D 100","D 50", "D 92","D 87","M 83","D 0","D 73","M 86"]
grades.sort(key=lambda gr: int(gr[2:])) # Sort by value
grades[-1] = grades[-1][:2] + str(100) # Set the last to 100
grades = grades[:-2] + grades[-1] # Drop the second to last

accumulator = {}
counter = {}

for grade in grades:
    accumulator[grade[0]] = accumulator.get(grade[0], 0) + int(grade[2:]) # Sum the grades by type
    # The get method allows a default value to be used
    counter[grade[0]] = counter.get(grade[0], 0) + 1 # Count the grades of each type

final_grade = 0
for type in WEIGHTS.keys():
    average = accumulator[type] / counter[type] # Calculate the average for each class
    final_grade += WEIGHTS[type] * average # Apply weighting