57个挑战(python+java)-lesson42(2)

在昨天题目基础上做挑战问题:

  1. 在金额上增加一个$符号 和逗号。- 这个在展示函数上直接增加即可。
  2. 以金额高低来做排序。
  3. 重写程序,主要用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();
    }
}


效果图:


展开阅读全文

页面更新:2024-03-03

标签:队列   字典   函数   金额   元素   效果   文件   方法   数据   列表

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2008-2024 All Rights Reserved. Powered By bs178.com 闽ICP备11008920号-3
闽公网安备35020302034844号

Top