Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中一次比较两个数据库记录_Python_List - Fatal编程技术网

在python中一次比较两个数据库记录

在python中一次比较两个数据库记录,python,list,Python,List,我试图比较数据库表中的成对学生。 我的数据库表如下: id | edu 1 | 1 2 | 1 3 | 2 4 | 2 #getting edu info data=curr.execute('select id,edu from student_details') result = curr.fetchall() mydic1=dict(result) data2=curr.execute('selec

我试图比较数据库表中的成对学生。 我的数据库表如下:

    id | edu
    1  | 1
    2  | 1
    3  | 2
    4  | 2
    #getting edu info
    data=curr.execute('select id,edu from student_details')
    result = curr.fetchall()
    mydic1=dict(result)

    data2=curr.execute('select id,edu from student_details ')
    result2 = curr.fetchall()
    mydic2=dict(result2)

    looping=curr.execute('select count(id) from student_details where id <= 4')
    loop_times = curr.fetchall()
    count = int(loop_times[0][0])

    count = count + 1
    listOflist=[]
    x=0
    for i in range(1,count):
        row = [] 
        for x in range(0,i):
            row.append(0)
        for j in range(i+1, count):
            if mydic1[i]==mydic2[j]:
               row.append(1)
            else:
               row.append(0)
        listOflist.append(row)
      print edu
     [[0, 1, 0, 0], [0, 0, 0, 0], [0, 0, 0, 1], [0, 0, 0, 0]]
我一次比较一对学生,如果他们有相似的教育代码,我会将他们输入一个列表,如果不是0,他们的id为1。 我的代码如下:

    id | edu
    1  | 1
    2  | 1
    3  | 2
    4  | 2
    #getting edu info
    data=curr.execute('select id,edu from student_details')
    result = curr.fetchall()
    mydic1=dict(result)

    data2=curr.execute('select id,edu from student_details ')
    result2 = curr.fetchall()
    mydic2=dict(result2)

    looping=curr.execute('select count(id) from student_details where id <= 4')
    loop_times = curr.fetchall()
    count = int(loop_times[0][0])

    count = count + 1
    listOflist=[]
    x=0
    for i in range(1,count):
        row = [] 
        for x in range(0,i):
            row.append(0)
        for j in range(i+1, count):
            if mydic1[i]==mydic2[j]:
               row.append(1)
            else:
               row.append(0)
        listOflist.append(row)
      print edu
     [[0, 1, 0, 0], [0, 0, 0, 0], [0, 0, 0, 1], [0, 0, 0, 0]]
但我不知道这是否是python中比较数据库记录的最有效的方法。欢迎提出任何改进建议。

一种方法是使用并创建一个字典,其中包含每个“edu”编号的学生编号列表。因此,结果与此类似:

edu_map == {1: [1, 2], 2: [3, 4]}
基本思想是迭代表中的每个条目,并将学生编号添加到结果dict中的“edu”编号中:

edu_map = defaultdict(list)
for stud, edu in mydic1.items():
    edu_map[edu] += [stud]
defaultdict
的原因是,当请求时,它会自动创建一个新的字典条目(在本例中为空列表),但该条目不存在。因此,如果edu不在edu映射中,您不需要检查
,然后添加一个空列表,因为
defaultdict
会为您这样做

要将此结果转换为类似于您的列表,您可以再次检查每个学生并获取他们的“教育单元”编号,然后使用
edu地图中的编号来获取该编号的列表:

result = []
for stud, edu in mydic1.items():
    stud_in_edu = edu_map[edu]
    stud_in_edu = [1 if other_stud != stud and other_stud in stud_in_edu else 0
                   for other_stud in range(len(mydic1))]
    result.append(stud_in_edu)
这假定
mydic1
仅包含有效条目。如果您已经拥有所有条目,则无需在单独的SQL查询中额外计算它们。

一种方法是使用并创建一个字典,其中包含每个“edu”编号的学生编号列表。因此,结果与此类似:

edu_map == {1: [1, 2], 2: [3, 4]}
基本思想是迭代表中的每个条目,并将学生编号添加到结果dict中的“edu”编号中:

edu_map = defaultdict(list)
for stud, edu in mydic1.items():
    edu_map[edu] += [stud]
defaultdict
的原因是,当请求时,它会自动创建一个新的字典条目(在本例中为空列表),但该条目不存在。因此,如果edu不在edu映射中,您不需要检查
,然后添加一个空列表,因为
defaultdict
会为您这样做

要将此结果转换为类似于您的列表,您可以再次检查每个学生并获取他们的“教育单元”编号,然后使用
edu地图中的编号来获取该编号的列表:

result = []
for stud, edu in mydic1.items():
    stud_in_edu = edu_map[edu]
    stud_in_edu = [1 if other_stud != stud and other_stud in stud_in_edu else 0
                   for other_stud in range(len(mydic1))]
    result.append(stud_in_edu)

这假定
mydic1
仅包含有效条目。如果您已经拥有所有条目,则无需在单独的SQL查询中额外计算它们。

mydic1
mydic2
是相同的,因此您可以删除第6-8行,并在if条件下使用
mydic1
。另外,如果您能解释结果列表显示的内容,也会很有帮助。例如,第二个条目不是
[1,0,0,0]
是故意的吗?谢谢你的建议,是的,这是故意的。在这种情况下,我的答案实际上应该做你想做的事情。
mydic1
mydic2
是相同的,因此你可以删除第6-8行,并在if条件下使用
mydic1
。另外,如果您能解释结果列表显示的内容,也会很有帮助。例如,第二个条目不是
[1,0,0,0]
是故意的吗?谢谢你的建议,是的,这是故意的。在这种情况下,我的答案应该是你想做的。非常感谢你的建议。通过这个答案,我学到了一个不同的方面。这当然取决于最终的结果。例如,
edu\u地图
已经包含相同的信息,但方式不同。非常感谢您的建议。通过这个答案,我学到了一个不同的方面。这当然取决于最终的结果。例如,
edu\u地图
已经包含相同的信息,但方式不同。