鱼C论坛

 找回密码
 立即注册
查看: 3370|回复: 8

python问题,2鱼币

[复制链接]
发表于 2017-4-24 13:49:34 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 Jc嘻嘻 于 2017-4-24 14:15 编辑

题目,
三个同学评选班干部, 我把他们存在一个txt 文件中,   a,b,c3个同学
另外的同学给他们打分, 我把分数存在另一个txt文件中,  比如   2,3,4    2是a的分数, 3b同学的分数 4c同学的分数

我的意思就是  现在有三个同学,  其他同学(不管多少个) 给他们三个按顺序打分。
1,2,3
4,1,1
5,6,2
3,4,5
,,1
0,1,2
删掉我
1,2,1
。。。
可以有无数个。
但是这其中有 ,,  有文字, 我需要删掉其中的文字 ,如果是,, 我需要把中间的数字改成0
比如,,1
我要改成0,0,1
然后我把每竖排加起来
然后用sort
比较大小 最后选出得票最多的那个人。

我才学这个,,好多搞不懂 这是我目前写的  求各位帮助

x = 'candidates.txt'
def getCandidates(x):
    a = open('candidates.txt','r')
    s = []
    for i in range(5):
        s.append(a.readline().strip('\n'))
    print(s)

getCandidates(x)


y = 'papers.txt'
def getPapers(y):    #我想先读这个分数文件,
    a = open('papers.txt','r')
   # for line in a:   
#       print(line[:-1])
         x = a(i);
        def parseVote(s):    然后我想def另一个函数, 这个函数用来判断 ,,  文字 还是怎样,可是我卡在这里了。。
            for i in a:
                if(len(x)==0):
                    a.remove(i)
            
        
    a.close()
parseVote(y)



以下是我需要用到的function   ...我理解这个东西 但是喊我写 感觉都是蒙的 求指导!

def getCandidates(f):
getCandidates(f) returns a list containing the candidates' names from the file f. The names will be one per line with no extraneous characters. Disregard any blank lines in f. If f doesn't exist, print an appropriate error message and return the empty list.
For example, getCandidates("candidates.txt") = ["Major Clanger", "Soup Dragon", "Froglet", "Iron Chicken", "The Cloud"].

def parseVote(s):
parseVote(s) returns the vote from s. Return 0 for an empty vote, and -1 if there are any non-digits (other than spaces). For example,
parseVote("") = parseVote(" ") = 0,
parseVote("-3") = parseVote("no") = parseVote("1 5") = -1,
parseVote("15") = parseVote(" 15 ") = 15.

def parsePaper(s, n):
parsePaper(s, n) returns the votes from the ballot paper s in an election with n candidates, plus an error message if appropriate. If s is formal, return the list of numbers found in s and the empty string; if s is informal, return an empty list of numbers and the appropriate string below.
For example:
parsePaper("14, , 2", 4) = ([14, 0, 2], ""),
parsePaper(", , ", 4) = parsePaper("0, 0", 4) = ([ ], "blank"),
parsePaper("4, -8, 0", 4) = parsePaper("4, 7.8, 0", 4) = parsePaper("pointless, 5, 5", 4) = ([ ], "non-digits"),
parsePaper("1,2,,4,5", 4) = ([ ], "too long").

parsePaper will use parseVote.

def getPapers(f, n):
getPapers(f, n) returns a list containing the ballot papers from the file f, in an election with n candidates. Treat each line of the file as a separate paper. If f doesn't exist, print an appropriate error message and return the empty list. For example:
getPapers("smallfile.txt", 4) = [([1, 2, 3, 4], ""), ([], "blank"), ([0, 23, 0], ""), ([], "non-digits"), ([], "non-digits"), ([4,0,4,4], ""), ([], "too long"), ([], "blank")].
smallfile.txt is available from the project web-site.

getPapers will use parsePaper.

def normalisePaper(p, n): # sum(p) > 0
normalisePaper(p, n) returns p with each vote scaled according to its total, and padded to contain n votes. For example:
normalisePaper([1,2,3,4], 4) = [0.1, 0.2, 0.3, 0.4], normalisePaper([2], 3) = [1.0, 0.0, 0.0], normalisePaper([0, 4, 496], 3) = [0.000, 0.008, 0.992]

def normalisePapers(ps, n): # for every p on ps, sum(p) > 0
normalisePapers(ps, n) returns ps with each paper normalised, in an election with n candidates.
e.g. normalisePapers([[2], [7, 2, 1]], 3) = [[1.0, 0.0, 0.0], [0.7, 0.2, 0.1]].

normalisePapers will use normalisePaper.

def countVotes(cs, ps): # ps have been normalised for an election with len(cs) candidates
countVotes(cs, ps) returns a list of lists containing the counts for the candidates cs from the ballot papers ps, in descending order of total number of votes. For example, countVotes(["A", "B", "C"], [[0.5, 0.5, 0], [0.05, 0.3, 0.65]]) = [[0.8, "B"], [0.65, "C"], [0.55, "A"]].

def printCount(c):
printCount(c) displays the election count c, i.e. the result from countVotes. See the sample output for the required format.

def main():
main() prompts the user for the names of the necessary files, then conducts the election. See the sample output for the required output.

main will use getCandidates, getPapers, normalisePapers, countVotes, and printCount.
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-4-24 13:55:47 | 显示全部楼层
然后呢?比如你们班有53个同学,去掉这3个干部不参加,应该形成50个txt文件。
你需要的就是把这个50个txt文件逐个打开,然后把数字取出来,加起来,除以50,得到班干部的平均分。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-4-24 14:10:04 | 显示全部楼层
ooxx7788 发表于 2017-4-24 13:55
然后呢?比如你们班有53个同学,去掉这3个干部不参加,应该形成50个txt文件。
你需要的就是把这个50个txt ...

这是我的一个作业,我把它简化了。
不需要减。

我的意思就是  现在有三个同学,  其他同学(不管多少个) 给他们三个按顺序打分。
1,2,3
4,1,1
5,6,2
3,4,5
,,1
0,1,2
删掉我
1,2,1
。。。
可以有无数个。
但是这其中有 ,,  有文字, 我需要删掉其中的文字 ,如果是,, 我需要把中间的数字改成0
比如,,1
我要改成0,0,1
然后我把每竖排加起来
然后用sort
比较大小 最后选出得票最多的那个人。

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-4-24 14:40:29 | 显示全部楼层
看不懂啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-4-24 14:45:30 | 显示全部楼层

回帖奖励 +2 鱼币

Jc嘻嘻 发表于 2017-4-24 14:10
这是我的一个作业,我把它简化了。
不需要减。
a = ',,1'
b=a.split(',')
c=[each.replace('','0') if each == '' else each for each in b]
c
['0', '0', '1']
明白了吗?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-4-24 14:56:19 | 显示全部楼层

嗯,但是这里你是直接打出了a = ',,2'
那如果我是一个txt文件,我怎么只读这一行的数呢。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-4-24 15:15:08 | 显示全部楼层
Jc嘻嘻 发表于 2017-4-24 14:56
嗯,但是这里你是直接打出了a = ',,2'
那如果我是一个txt文件,我怎么只读这一行的数呢。

哇,那你这是一点python都不懂了啊。
打开文件,然后有readline和readlines方法,根据你txt文件的大小自行选用。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-4-24 17:15:18 | 显示全部楼层
看我的测试数据
def change(astr):
  newlis = []
  lis = astr.split(",")  #这个地方到底是英文逗号还是中文逗号,要看你的文件里面是什么
  for each in lis:
    if each == "" or each == " ":
      newlis.append(0)
    else:
      newlis.append(int(each))
  print(newlis)
  return newlis

def delstr(lines):   
  thelist = []
  # with open(filename,"r") as f:  
  #   lines = f.readlines()        
  for line in lines:
    k = line.replace(",","").replace(" ","").replace(",","")
    try:
      if int(k):
        thelist.append(line)
    except:
      pass
  return thelist

lines = ["1,2,3","4,1,1",",,1","删掉我"]  #测试数据
lis = delstr(lines)
p1 = 0
p2 = 0
p3 = 0
for each in lis:
  nums = change(each)
  p1 += nums[0]
  p2 += nums[1]
  p3 += nums[2]
print(p1,p2,p3)

输出如下
[1, 2, 3]
[4, 1, 1]
[0, 0, 1]
5 3 5

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-4-24 17:18:24 | 显示全部楼层

回帖奖励 +2 鱼币

本帖最后由 gopythoner 于 2017-4-24 17:36 编辑

如果你要处理你的文件,可以直接饮用这个代码
其中filename = 'papers.txt'
p1,p2,p3分别代表三个同学的总分
注意的问题,看我注释,你的文件里面是英文还是中文逗号,需要这我注释的地方改成相应的
def change(astr):
  newlis = []
  lis = astr.split(",")  #这个地方到底是英文逗号还是中文逗号,要看你的文件里面是什么
  for each in lis:
    if each == "" or each == " ":
      newlis.append(0)
    else:
      newlis.append(int(each))
  print(newlis)
  return newlis

def delstr(filename):   
  thelist = []
  with open(filename,"r") as f:  
    lines = f.readlines()        
  for line in lines:
    k = line.replace(",","").replace(" ","").replace(",","")
    try:
      if int(k):
        thelist.append(line)
    except:
      pass
  return thelist

p1 = 0
p2 = 0
p3 = 0
lis = delstr(filename)
for each in lis:
  nums = change(each)
  p1 += nums[0]
  p2 += nums[1]
  p3 += nums[2]
print(p1,p2,p3)

    

               

PS:只要你的文件格式满足每个投票之前都是用逗号分开的,那么,不管多少个同学需要投票都是可以计算的,只需要将我最后那段改成一个循环判断就行,很容易做到
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-7 18:25

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表