57个挑战(pythonjava)lesson42(2)
在昨天题目基础上做挑战问题:
在金额上增加一个符号和逗号。这个在展示函数上直接增加即可。以金额高低来做排序。重写程序,主要用csv的解析库来实现解析效果。
挑战2两个求解思路,
方法1:建设2个字典,以Salary为key,以First和Last为value。这样做排序的时候,先对Salarylist做排序,然后基于这两个字典做查询即可。
方法2:把Salary的list中的元素index单独抽出来,在做排序的时候,记录原始index的最后位置,再根据这个位置在list里寻找数据。这个之前挑战已经做过了,有demo。
方法3:把这些内容存到数据库里,做一条查询即可,再做展示。
挑战3的代码如下:
python版本:importcsvclassParsedatafile:originallist〔〕firstcolumn〔〕seconcolumn〔〕thirdcolumn〔〕defreaddatafile(this):从指定的文件中读取数据,并读入到一个列表中filenameinput(Pleaseprovdethenameofthefilefromwhichyouwantopen)withopen(filename,r)asf:readercsv。reader(f)forlineinreader:this。originallist。append(line)f。close()print(thisisthelistwegetfromthefile{0}。format(this。originallist))defprocesslist(this):对列表做格式化,并生成对应的三个队列foriinthis。originallist:this。firstcolumn。append(i〔0〕)this。seconcolumn。append(i〔1〕)this。thirdcolumn。append(i〔2〕)print(Thisisthefirstcolumnlist:{0}。format(this。firstcolumn))print(Thisisthesecondcolumnlist:{0}。format(this。seconcolumn))print(Thisisthethirdcolumnlist:{0}。format(this。thirdcolumn))defdisplaylist(this):对处理后的队列操作,逐行做展示找到每个列表里面最宽的元素col1length0col2length0col3length0foriinthis。firstcolumn:iflen(i)col1length:col1lengthlen(i)foriinthis。seconcolumn:iflen(i)col2length:col2lengthlen(i)foriinthis。thirdcolumn:iflen(i)col3length:col3lengthlen(i)spaceline1Lastspace(col1length41)Firstspace(col2length51)Salaryspace(col3length61)line2len(line1)print(line1)print(line2)foriinrange(0,len(this。firstcolumn)):print(this。firstcolumn〔i〕。ljust(col1length1),end)print(this。seconcolumn〔i〕。ljust(col2length1),end)print(this。thirdcolumn〔i〕)lesson42Parsedatafile()lesson42。readdatafile()lesson42。processlist()lesson42。displaylist()
pythoncsv函数的详细用法可以参考网文,
https:blog。csdn。netxietanshengarticledetails117604917
这里主要是把每一行的数据解析成了一个队列。
看到下面运行效果是一样的。
Java版本:
使用csvReader来解析CSV文件,效果类似,也是解析成了一个队列,从队列里面直接读取元素即可。
importjava。io。BufferedReader;importjava。io。FileReader;importjava。io。IOException;importjava。io。InputStreamReader;importjava。nio。Buffer;importjava。util。ArrayList;importjava。util。Scanner;importcom。opencsv。CSVReader;publicclassParseFilev2{privateArrayListStringoriginallistnewArrayList();privateArrayListStringfirstcolumnnewArrayList();privateArrayListStringsecondcolumnnewArrayList();privateArrayListStringthirdcolumnnewArrayList();privateScannerscnewScanner(System。in);voidreaddatafile(){System。out。println(Pleaseprovdethenameofthefilefromwhichyouwantopen);Stringfilenamesc。nextLine();try{String〔〕nextLine;CSVReadercsvReadernewCSVReader(newFileReader(filename));while((nextLinecsvReader。readNext())!null){firstcolumn。add(nextLine〔0〕);secondcolumn。add(nextLine〔1〕);thirdcolumn。add(nextLine〔2〕);originallist。add(nextLine〔0〕,nextLine〔1〕,nextLine〔2〕);}}catch(IOExceptione){}System。out。println(Thisistheoriginallistwegetfromthefileoriginallist。toString());}从指定的文件中读取数据,并读入到一个列表中voiddisplaylist(){对处理后的队列操作,逐行做展示找到每个列表里面最宽的元素intfcmlength0;intscmlength0;inttcmlength0;for(inti0;ifirstcolumn。size();i){if(fcmlengthfirstcolumn。get(i)。length()){fcmlengthfirstcolumn。get(i)。length();}if(scmlengthsecondcolumn。get(i)。length()){scmlengthsecondcolumn。get(i)。length();}if(tcmlengththirdcolumn。get(i)。length()){tcmlengththirdcolumn。get(i)。length();}}fcmlengthfcmlength1;scmlengthscmlength1;tcmlengthtcmlength1;System。out。printf(fcmlengths,Last);System。out。printf(scmlengths,First);System。out。printf(tcmlengths,Salary);for(intj0;jfcmlengthscmlengthtcmlength;j){System。out。printf();}System。out。println();for(intk0;kfirstcolumn。size();k){System。out。printf(fcmlengths,firstcolumn。get(k));System。out。printf(scmlengths,secondcolumn。get(k));System。out。printf(tcmlengths,thirdcolumn。get(k));System。out。println();}}publicstaticvoidmain(String〔〕args){ParseFilev2lesson42newParseFilev2();lesson42。readdatafile();lesson42。displaylist();}}
效果图:
国潮闹新春,北京欢乐谷春节游客接待量超往年北京日报客户端记者程功2023年春节,北京欢乐谷为广大游客打造新春年俗文化演艺国潮打卡活动生肖潮玩购物等丰富多样的春节游玩大礼包,更有总台文创兔年生肖形象兔团团家族以及春晚吉祥物兔
起初被认为是烂尾楼,如今成为成都的地标建筑,游客纷纷来打卡我国是一个广阔的国家,也是一个神奇的国家,在这个神奇的国家里面有许多神奇的地方,我国的三十二个省每个省都有每个省的特色,都会有让人游玩的好去处。我国也在不断的进步不断的发展。如今的
让咱西安火下去这个春节期间陕西本土第一节目的官方抖音号内容,虽然是数据是搞笑的夸张,但是也足以反映出来西安春节期间的火爆程度。上世纪九十年代末期,西安市南大街悬挂着一幅巨幅广告牌,内容为让咱西安
我穷游72个国家,勇闯羌塘无人区,治好了抑郁症,如今只想有个家这是我们讲述的第1455位真人故事我是王欣欣爷来啦,山西阳泉人,江湖人称欣爷,独自一人穿越70多个国家的女汉子。曾经,我是被命运抛弃的孩子,7岁时父亲离家远行,从此很少回家。原以为
胡歌娶女助理,宋仲基娶离异女,娱圈的择偶观,正走向另一条大路头条创作挑战赛2023年刚开年,娱乐圈就传来两个喜讯,一个是胡歌结婚生女,一个是宋仲基二婚。不过,比起恭喜声,更多的声音是对两人配偶的讨论。胡歌在官宣文案中表示妻子不是公众人物,言
扫码烧金最低28元?泉州关岳庙已申请电子烧金炉专利近日,福建泉州网友发视频称,在关岳庙扫码烧金最低28元。景区工作人员回应,电子烧金都有最低消费,添香油钱是可以自己定金额,随缘支付,这些都是董事会决定的。天眼查App显示,泉州通淮
昂立教育预计2022年度净利润1。39亿元到2亿元昂立教育预计2022年度净利润1。39亿元到2亿元中证网讯(记者王可)1月29日晚间,昂立教育发布2022年度业绩预盈公告称,经初步测算,公司2022年度将扭亏为盈,预计实现归属于
一月北上1400亿元国际资本看多A股本报记者顾梦轩夏欣广州北京报道2023年开年,A股市场否极泰来,开启了涨涨涨模式。Wind数据显示,截至2月1日,上证指数上涨6。33,深证成指上涨10。37,创业板指上涨11。3
山水比德2022年预计亏损1。3亿元1。5亿元中证智能财讯山水比德(300844)1月20日晚间披露2022年度业绩预告,预计2022年归母净利润亏损1。3亿元1。5亿元,上年同期盈利5485。48万元扣非净利润亏损1。4亿元
经济增长动力十足中国农历新春佳节,各地民众沉浸在欢乐喜庆的节日气氛中,国内消费活力进一步释放。经文化和旅游部数据中心测算,今年春节假期全国国内旅游出游3。08亿人次,同比增长23。1。实现国内旅游
大侦探8第二案烧脑上线!何炅张若昀高能破时空迷案!大侦探第八季惊喜回归何炅张若昀大张伟王鸥魏晨杨蓉魏大勋吴昕等老玩家置身大侦探宇宙继续探求真相之旅观看路径长江云TV综艺专区除了有配合默契有梗有料的老玩家本季大侦探依然保持逻辑清晰的