我有多个文本文件,其中包含有关基于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感兴趣的国家数量的代码。
这是我用来读取文本文件的当前代码。但我不确定在2004-2015两年间告诉对python感兴趣的区域数量的最佳方法。起初我认为最好的方法是从所有文本文件创建一个列表,不包括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();
}
}
}
答案 0 :(得分:2)
使用Map<String, Integer>
创建HashMap
,每当您在扫描传入数据时找到新国家/地区,请将其添加到地图国家/地区&gt; 0。每次你发现使用python时都会增加值。
在地图entrySet
的最后一个循环中,以及e.value()
为零输出e.key()
的每种情况。