gpt4 book ai didi

java - 是否可以使用来自多个文本文件的数据在 java 中创建列表

转载 作者:搜寻专家 更新时间:2023-11-01 03:19:54 25 4
gpt4 key购买 nike

我有多个文本文件,其中包含基于 google 搜索的不同编程语言在不同国家/地区的受欢迎程度的信息。我有一个从 2004 年到 2015 年的每一年的文本文件。我还有一个将其分解为每周的文本文件(称为 iot.txt),但该文件不包括国家/地区。

来自 2004.txt 的示例数据:

Region  java    c++ c#  python  JavaScript

Argentina 13 14 10 0 17

Australia 22 20 22 64 26

Austria 23 21 19 31 21

Belgium 20 14 17 34 25

Bolivia 25 0 0 0 0

等等

来自 iot.txt 的示例:

Week    java    c++ c#  python  JavaScript

2004-01-04 - 2004-01-10 88 23 12 8 34

2004-01-11 - 2004-01-17 88 25 12 8 36

2004-01-18 - 2004-01-24 91 24 12 8 36

2004-01-25 - 2004-01-31 88 26 11 7 36

2004-02-01 - 2004-02-07 93 26 12 7 37

我的问题是我正在尝试编写代码来输出对 python 表现出 0 兴趣的国家/地区的数量。

这是我当前用来读取文本文件的代码。但我不确定最好的方法来判断 2004-2015 年期间对 python 兴趣为 0 的地区数量。起初我认为最好的方法是从不包括 iot.txt 的所有文本文件中创建一个列表,然后在其中搜索对 python 没有兴趣的任何条目,但我不知道该怎么做。

谁能推荐一种方法来做到这一点?

import java.io.BufferedReader;
import java.io.FileReader;

import java.util.*;

public class Starter{

public static void main(String[] args) throws Exception {
BufferedReader fh =
new BufferedReader(new FileReader("iot.txt"));
//First line contains the language names
String s = fh.readLine();
List<String> langs =
new ArrayList<>(Arrays.asList(s.split("\t")));
langs.remove(0); //Throw away the first word - "week"
Map<String,HashMap<String,Integer>> iot = new TreeMap<>();
while ((s=fh.readLine())!=null)
{
String [] wrds = s.split("\t");
HashMap<String,Integer> interest = new HashMap<>();
for(int i=0;i<langs.size();i++)
interest.put(langs.get(i), Integer.parseInt(wrds[i+1]));
iot.put(wrds[0], interest);
}
fh.close();
HashMap<Integer,HashMap<String,HashMap<String,Integer>>>
regionsByYear = new HashMap<>();
for (int i=2004;i<2016;i++)
{
BufferedReader fh1 =
new BufferedReader(new FileReader(i+".txt"));
String s1 = fh1.readLine(); //Throw away the first line
HashMap<String,HashMap<String,Integer>> year = new HashMap<>();
while ((s1=fh1.readLine())!=null)
{
String [] wrds = s1.split("\t");
HashMap<String,Integer>langMap = new HashMap<>();
for(int j=1;j<wrds.length;j++){
langMap.put(langs.get(j-1), Integer.parseInt(wrds[j]));
}
year.put(wrds[0],langMap);
}
regionsByYear.put(i,year);
fh1.close();
}
}
}

最佳答案

创建一个 Map<String, Integer>使用 HashMap每次你在扫描传入数据时发现一个新国家,将其添加到 map country->0 中。每次找到 python 的用法时,都会增加该值。

最后循环遍历 entrySet map 的每个案例,其中 e.value()是零输出e.key() .

关于java - 是否可以使用来自多个文本文件的数据在 java 中创建列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33754879/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com