我来自 Python 背景,目前正在将我的 Python 程序移植到 Java。我需要有关解决问题的最佳方法的建议。
最初,我在 Python 中创建了一个元组列表:
loft = [('india',1),('accepts',1),('narendra',1), ('modi',1),('manmohan',1),('singh',1),('sonia gandhi',1),('rajkot',1),('sharma',1),('raja',1),('india',2),('manmohan',2),('singh',2),('nepal',2),('prime minister',2),('meeting',2),('economy',2),('manmohan',3),('narendra',3),('modi',3),('gupta',3),('rajkot',3),('patel',3),('singh',3),('rajiv',3),('aajtak',3),('manmohan',4),('nepal',4),('bahadur',4),('king',4),('meeting',4),('economy',4),('wife',4),('plane',4)]
(在印度,accepts 是关键字,数字是从数据库中获取的 id。)。现在,申请:
di = {}
for x,y in ll:
di.setdefault(x,[]).append(y)
newdi = {}
我的列表变成了字典:
di = {'manmohan': [1, 2, 3, 4], 'sonia gandhi': [1], 'raja': [1], 'india': [1, 2], 'narendra': [1, 3], 'patel': [3], 'sharma': [1], 'nepal': [2, 4], 'gupta': [3], 'singh': [1, 2, 3], 'meeting': [2, 4], 'economy': [2, 4], 'rajkot': [1, 3], 'prime minister': [2], 'plane': [4], 'bahadur': [4], 'king': [4], 'wife': [4], 'accepts': [1], 'modi': [1, 3], 'aajtak': [3], 'rajiv': [3]}
Java部分:
public void step1() throws SQLException{
Connection con= new Clustering().connect();
Statement st = con.createStatement();
Statement st1 = con.createStatement();
ResultSet rs = st.executeQuery("select uid from url where artorcat=1");
ArrayList<Tuples> allkeyword = new ArrayList<Tuples>();
long starttime = System.currentTimeMillis();
while (rs.next()) {
int id = rs.getInt("uid");
String query = "select tags.tagname from tags left join tag_url_relation on tags.tid=tag_url_relation.tid where tag_url_relation.uid="+id;
ResultSet rs1 = st1.executeQuery(query);
while (rs1.next()){
String tag = rs1.getString(1);
//Creating an object t of type Tuples
//and pass values to constructor
Tuples t = new Tuples(id,tag);
//adding the above tuple to arraylist allkeyword
allkeyword.add(t);
}//job done, now lets test by iterating
}
Iterator<Tuples> it = allkeyword.iterator();
while(it.hasNext()){
Tuples t = it.next();
System.out.println(t.getId());
System.out.println(t.getKeyword());
}
long endtime = System.currentTimeMillis();
long totaltime = endtime-starttime;
System.out.println("Total time:" + totaltime);
}
And here is Tuples class :
/**
*
*
* Tuple class is created to create a multiple data type tuple. We are using this tuples object to retrieve keyword and
* id in step1 in Clustering.java.
* @author akshayy
*
*/
public class Tuples {
int i;
String s;
public Tuples(int i, String s) {
this.i= i;
this.s=s;
}
public int getId(){
return this.i;
}
public String getKeyword(){
return this.s;
}
}
到目前为止,一切都很好。我创建了一个包含关键字和 id 的元组类的数组列表。现在如何在 id 中查找关键字的出现的下一步。像 'manmohan' 在 id 1,2,3,4 等中找到。
di = {'manmohan': [1, 2, 3, 4], 'sonia gandhi': [1], 'raja': [1], 'india': [1, 2], 'narendra': [1, 3], 'patel': [3], 'sharma': [1], 'nepal': [2, 4], 'gupta': [3], 'singh': [1, 2, 3], 'meeting': [2, 4], 'economy': [2, 4], 'rajkot': [1, 3], 'prime minister': [2], 'plane': [4], 'bahadur': [4], 'king': [4], 'wife': [4], 'accepts': [1], 'modi': [1, 3], 'aajtak': [3], 'rajiv': [3]}
请建议我在 arraylist 中查找类似项目并像上面那样对它们进行排序的下一个方法应该是什么。还是我需要完全不同的东西?