57个挑战(pythonjava)lesson42(2)
在昨天题目基础上做挑战问题:
在金额上增加一个$符号 和逗号。- 这个在展示函数上直接增加即可。以金额高低来做排序。重写程序,主要用csv 的解析库来实现解析效果。
挑战2两个求解思路,
方法1 : 建设2个字典,以Salary 为 key, 以First 和 Last 为value。 这样做排序的时候,先对Salary list 做排序, 然后基于这两个字典做查询即可。
方法2: 把Salary 的 list 中的元素index 单独抽出来,在做排序的时候,记录原始index的最后位置,再根据这个位置在list 里寻找数据。--这个之前挑战已经做过了,有demo。
方法3: 把这些内容存到数据库里,做一条查询即可,再做展示。
挑战3的代码如下:
python 版本:import csv class Parsedatafile: originallist = [] firstcolumn = [] seconcolumn = [] thirdcolumn = [] def readdatafile(this): # 从指定的文件中读取数据,并读入到一个列表中 filename = input("Please provde the name of the file from which you want open") with open(filename, "r") as f: reader = csv.reader(f) for line in reader: this.originallist.append(line) f.close() print("this is the list we get from the file {0}".format(this.originallist)) def processlist(this): # 对列表做格式化,并生成对应的三个队列 for i in this.originallist: this.firstcolumn.append(i[0]) this.seconcolumn.append(i[1]) this.thirdcolumn.append(i[2]) print("This is the firstcolumn list: {0}".format(this.firstcolumn)) print("This is the secondcolumn list: {0}".format(this.seconcolumn)) print("This is the thirdcolumn list: {0}".format(this.thirdcolumn)) def displaylist(this): # 对处理后的队列操作,逐行做展示 # 找到每个列表里面最宽的元素 col1length = 0 col2length = 0 col3length = 0 for i in this.firstcolumn: if len(i) > col1length: col1length = len(i) for i in this.seconcolumn: if len(i) > col2length: col2length = len(i) for i in this.thirdcolumn: if len(i) > col3length: col3length = len(i) space = " " line1 = "Last" + space * (col1length - 4 + 1) + "First" + space * (col2length - 5 + 1) + "Salary" + space * ( col3length - 6 + 1) line2 = len(line1) * "-" print(line1) print(line2) for i in range(0, len(this.firstcolumn)): print(this.firstcolumn[i].ljust(col1length + 1), end="") print(this.seconcolumn[i].ljust(col2length + 1), end="") print(this.thirdcolumn[i]) lesson42 = Parsedatafile() lesson42.readdatafile() lesson42.processlist() lesson42.displaylist()
python csv 函数的详细用法可以参考网文,
https://blog.csdn.net/xietansheng/article/details/117604917
这里主要是把每一行的数据解析成了一个队列。
看到下面运行效果是一样的。
Java 版本:
使用csvReader来解析CSV 文件,效果类似,也是解析成了一个队列,从队列里面直接读取元素即可。
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.nio.Buffer; import java.util.ArrayList; import java.util.Scanner; import com.opencsv.CSVReader; public class ParseFilev2{ private ArrayList originallist = new ArrayList<>(); private ArrayList firstcolumn = new ArrayList<>(); private ArrayList secondcolumn = new ArrayList<>(); private ArrayList thirdcolumn = new ArrayList<>(); private Scanner sc = new Scanner(System.in); void readdatafile(){ System.out.println("Please provde the name of the file from which you want open"); String filename = sc.nextLine(); try{ String[] nextLine; CSVReader csvReader = new CSVReader(new FileReader(filename)); while((nextLine=csvReader.readNext())!= null) { firstcolumn.add(nextLine[0]); secondcolumn.add(nextLine[1]); thirdcolumn.add(nextLine[2]); originallist.add(nextLine[0]+","+nextLine[1]+","+nextLine[2]); } }catch(IOException e){ } System.out.println("This is the original list we get from the file"+ originallist.toString()); } //从指定的文件中读取数据,并读入到一个列表中 void displaylist(){ //对处理后的队列操作,逐行做展示 //找到每个列表里面最宽的元素 int fcmlength = 0; int scmlength = 0; int tcmlength = 0; for (int i = 0 ; i< firstcolumn.size();i++){ if (fcmlength< firstcolumn.get(i).length()) {fcmlength = firstcolumn.get(i).length();} if (scmlength< secondcolumn.get(i).length()) {scmlength = secondcolumn.get(i).length();} if (tcmlength< thirdcolumn.get(i).length()) {tcmlength = thirdcolumn.get(i).length();} } fcmlength = fcmlength + 1; scmlength = scmlength + 1; tcmlength = tcmlength + 1; System.out.printf("%-"+fcmlength+"s","Last"); System.out.printf("%-"+scmlength+"s","First"); System.out.printf("%-"+tcmlength+"s ","Salary"); for(int j=0;j< fcmlength+scmlength+tcmlength;j++) { System.out.printf("-"); } System.out.println(); for( int k =0 ;k < firstcolumn.size();k++) { System.out.printf("%-"+fcmlength+"s", firstcolumn.get(k)); System.out.printf("%-"+scmlength+"s", secondcolumn.get(k)); System.out.printf("%-"+tcmlength+"s", thirdcolumn.get(k)); System.out.println(); } } public static void main(String[] args) { ParseFilev2 lesson42 = new ParseFilev2(); lesson42.readdatafile(); lesson42.displaylist(); } }
效果图:
胶东籍开国将军知多少?胶东对中国革命胜利贡献巨大,1955年全军实行军衔制,烟台籍共有28人先后被授予开国少将。6位毕业于莱阳二乡师高锐刘春山李道之张少虹姜林东慕湘。胶东将军一个特点是长寿,90岁以上的
清朝十二位皇帝画像及生平,从努尔哈赤到溥仪清太祖努尔哈赤生卒年15591626年,68岁父母父,塔克世母,喜塔喇氏后妃皇后叶赫那拉氏元妃继妃大妃等年号天命在位时间16161626年,10年谥号高皇帝庙号太祖陵寝福陵(今沈阳
李白居然是大唐叛军一员,并险些因此丧命,你知道吗?李白一生中有两次为皇家服务的经历,其一是在天宝初年被玄宗征召,供奉翰林,其二是在安史之乱时,被永王李璘请下庐山,延为幕府。天宝十四载(755年),安禄山在范阳起兵造反,第二年攻陷潼
婚外情打胎吸毒,一手好牌打得稀烂?晚年醒悟成三八红旗手民国名媛有南唐北陆的说法,南方有唐瑛,北方有陆小曼,两人皆是才貌双全情商极高的女子。胡适更说陆小曼是北京城里一道不得不看的风景。昨天讲了林徽因,今天我们就来讲一讲陆小曼的故事。陆小
赵云死时诸葛亮只是敷衍哭两下,张苞死他却哭到吐血,这是为何?五虎上将中的赵云死后,诸葛亮只是随便哭了两声。可当听闻张苞的死讯时,诸葛亮却哭到吐血昏厥,这是为何呢?难道在诸葛亮眼中,陪伴他共患难20多年的赵云,还比不过一个小小的张苞呢?这里面
2008年,东风快递泄露后两个间谍被判死刑,为何西方不满?头条创作挑战赛2008年11月28日,北京法院对两名老年犯人依法执行死刑,是直接枪毙,还是注射死亡,媒体资料并没有透露具体用什么样的手段?估计药物注射死亡的可能性极大。这两名老年犯
不应该被遗忘的五位民族英雄,你知道几位?同样是民族英雄,但有的名垂青史,被世人祭奠,有的却默默无闻,被世人遗忘。今天小编就列举五位,与大家共勉!第一位颁布杀胡令的冉闵冉闵是不是民族英雄历来争议很大,赞同的人说他颁布杀胡令
杜甫对诸葛亮评价有多高?从他写的八首诗中可以看出唐代大诗人杜甫,虽然才华横溢,诗绝当代,但可惜没有官运,一生被任命的最大官职也不过是左拾遗。他年轻时也参加了一次科举考试,但因为当时当宰相李林甫演出一场野无遗贤闹剧,参加考试的士子
为啥康熙刚死,雍正就把宜妃赶出了宫?百年后才知道原因历代帝王在去世之后都很有可能留下一大批后宫妃子,少则百余人,多则可达千人以上,这也是没办法的事,毕竟古代帝王是可以有三宫六院七十二嫔妃的,嫔妃以下更是随便纳娶,一般情况下,前一任帝
肮脏的崛起之路韩国财阀与性剥削2009年,拍摄乐天饼干出道的女星张紫妍自缢身亡。2011年,张紫妍昔日留下的50页血泪遗书被曝光,与此同时,一张冗长的陪睡名单也被公诸于世。韩国财阀的丑闻,在这一年,缓缓拉开了序
刘邦为何宁死也不接受诊治呢刘邦晚年征讨叛乱的英布时,曾被流箭射中,之后在返程途中生病,回到长安后,病情越发严重,吕后便找来良医为刘邦诊治,而医生明确表示能将病治好。但刘邦不仅不接受治疗还谩骂医生,最后又赏赐