如何在python中将特定属性作为主键

时间:2011-12-26 05:21:17

标签: python

我只需要将特定属性作为主键,因此通过使用此属性,我想访问同一行的其他属性。任何人都可以帮助我。

描述::我有4个数组,我想检索对应于我选择的行的数组中的数据。简单来说,我使用Python作为数据库,并存储检索基于a的其他元组的值特定列值。

import string
import sys
from Tkinter import *
import win32com.client

win = Tk()
win.title("Sorting the list of employees as of desire")
win.geometry("600x600+200+50")
win.resizable()

class Emp(object):
    def __init__(self):
        i = 0


        def save():            
            b = self.e.get()
            #print "The name of employee is", b
            n.append(b) 

            c = self.a.get()
            #print "The age of employee is", c
            ge.append(c)  

            idf = self.i.get()
            #print "The Ide of the Employee is ", id            
            idee.append(idf)  

            de = self.d.get()
            # print "The Designation of the Employee is ", de            
            desi.append(de) 

            #print "\n"


        def clear():
            self.e.delete(0, END)
            self.a.delete(0, END)
            self.i.delete(0, END)
            self.d.delete(0, END)

        n = []
        Label(text="Employee form ", font="Verdana 18 bold italic").pack()
        Name = Label(text="Name ", font="Verdana 10 bold ")
        Name.place(relx=0.1, rely=0.5)
        Name.pack()
        self.e = Entry(text="Enter the name of the employee ")
        self.e.place(relx=0.5, rely=0.5, anchor=CENTER)
        self.e.insert(0, "Name of Employee")
        self.e.pack()

        ge = []
        age = Label(text="Age ", font="Verdana 10 bold ")
        age.place(relx=0.1, rely=0.5)
        age.pack()
        self.a = Entry(text="Enter the age of the employee ")
        self.a.place(relx=0.5, rely=0.5, anchor=CENTER)
        self.a.insert(0, "Age of Employee")
        self.a.pack()

        idee = []
        ide = Label(text="ID ", font="Verdana 10 bold ")
        ide.place(relx=0.1, rely=0.5)
        ide.pack()
        self.i = Entry(text="Enter the ID of the employee ")
        self.i.place(relx=0.5, rely=0.5, anchor=CENTER)
        self.i.insert(0, "IDE of Employee")
        self.i.pack()


        desi = []
        des = Label(text="Designation ", font="Verdana 10 bold ")
        des.place(relx=0.1, rely=0.5)
        des.pack()
        self.d = Entry(text="The Designation of the employee ")
        self.d.place(relx=0.5, rely=0.5, anchor=CENTER)
        self.d.insert(0, "Designation of Employee")
        self.d.pack()

        def printf():
            global i 

            xyz = len(n)
            for i in range(0, xyz):
                print "Details are ::", "Name is :", n[i], "Age is : ", ge[i], "Employee Id is :", idee[i], "Designation is :", desi[i] 
                print "\n"

        def sorting():
            sor = raw_input("Enter a to sort from A or z to sort in reverse order")
            xyz = len(n)
            if sor == 'a' or'A' :
                n.sort()
                for i in range(0, xyz):
                    print "Details are ::", "Name is :", n[i]#, "Age is : ", ge[i], "Employee Id is :", idee[i], "Designation is :", desi[i] 
                    print "\n"
            elif sor == 'z' or 'Z':
                n.sort()
                print "Details are ::", "Name is :", n[i], "Age is : ", ge[i], "Employee Id is :", idee[i], "Designation is :", desi[i] 
                n.reverse()
                for i in range(0, xyz):
                    print "Details are ::", "Name is :", n[i]#, "Age is : ", ge[i], "Employee Id is :", idee[i], "Designation is :", desi[i] 
                    print "\n"

        btn = Button(text="Save ", font="verdana 12 ", command=save)
        btn.pack(fill=X, expand=YES)
        btn.place(relx=0.85, rely=0.06, anchor=CENTER)

        btnc = Button(text="Next", font="verdana 12 ", command=clear)
        btnc.pack(fill=X, expand=YES)
        btnc.place(relx=0.85, rely=0.90, anchor=CENTER)      

        btnp = Button(text="print", font="verdana 12 ", command=printf)
        btnp.pack(fill=X, expand=YES)
        btnp.place(relx=0.6, rely=0.9, anchor=CENTER)      

        btns = Button(text="Sort", font="verdana 12 ", command=sorting)
        btns.pack(fill=X, expand=YES)
        btns.place(relx=0.3, rely=0.9, anchor=CENTER)





abj = Emp()
win.mainloop()

这是我的代码。将所有值存储在相应的数组中之后。我想根据名称对它们进行排序,对于我编写的代码,我得到的输出是只有name属性的排序数组,而是我希望所有相应的值也被排序。请帮帮我

2 个答案:

答案 0 :(得分:2)

回复原始问题(已被删除):

你可以使用python dict:http://docs.python.org/library/stdtypes.html#dict

dataset = {}
dataset['key1'] = (value_1_1, value_1_2, value_1_3, value_1_4)
dataset['key2'] = (value_2_1, value_2_2, value_2_3, value_2_4)

然后,如果您想要与'key2'相关联的值:

row_of_interest = dataest['key2']

在代码的上下文中,使用dict:

更具体地说,在看到您的代码之后,一种解决方案是将保存功能更改为:

def save():
    name = self.e.get()
    age = self.a.get()
    employee_id = self.i.get()
    employee_designation = self.d.get()
    self.employees[name] = (age, employee_id, employee_designation)

其中employees是一个词典。这为您提供了问题中陈述的主要密钥属性,但不会让您轻松访问代码提示的排序结果。

或,使用列表:

def save():
    name = self.e.get()
    age = self.a.get()
    employee_id = self.i.get()
    employee_designation = self.d.get()
    self.employees.append((name, age, employee_id, employee_designation))

其中employees是一个列表,而不是一个字典。然后,您可以使用sorted内置函数(http://docs.python.org/library/functions.html#sorted)与operator模块相结合来获取排序结果:

sorted(self.employees, key=operator.itemgetter(0), reverse=False)

这使您可以轻松访问按第一个元素(名称)排序的结果,但不会将任何属性视为特殊主键。

答案 1 :(得分:0)

以编写代码的方式,您的相关数据集(我认为它们是ngeideedesi与之无关您尝试按n.sort()对名称列表进行排序,但这不起作用;数据松散耦合。

由于您尝试对单个员工的姓名进行基于具体的数据排序,因此最理想的解决方案是使用字典。正如@Sancho之前所说,Python字典允许您在密钥(在本例中为员工的姓名)和值(在这种情况下,员工之后的任何属性)之间创建关系。

(我对彼此使用“相关”,因为程序员明白数据集相关,但在Python世界中,它们只有四个列表在这种情况下,使用字典是更好的解决方案。)