如何在Python中将特定属性设为主键

2 投票
2 回答
2946 浏览
提问于 2025-04-17 09:00

我有一个小需求,就是想把某个特定的属性设置为主键,这样我就可以通过这个属性来访问同一行的其他属性。有没有人能帮我一下?

具体来说,我有4个数组,我想根据我选择的那一行来获取这些数组中的数据。简单来说,我在用Python当数据库,想根据某个特定列的值来存储和获取其他元组的值。

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()

这是我的代码。在把所有值存储到相应的数组后,我想根据名字对它们进行排序。但是我写的代码输出的结果只是名字属性的排序数组,而我希望所有对应的值也能一起排序。请帮帮我。

2 个回答

0

根据你代码的写法,你的数据集(我猜是 ngeideedesi)之间没有关系。你试图通过 n.sort() 来排序名字的列表,但这样做是行不通的,因为这些数据之间的联系很松散。

既然你是想根据某个员工的名字来排序数据,那么最好的办法就是用字典。正如 @Sancho 之前提到的,Python 的字典可以让你在一个键(在这里就是员工的名字)和一些值(比如这个员工的其他属性)之间建立关系。

我用“相关”这个词有点开玩笑,因为对程序员来说,这些数据集显然是有关系的,但在 Python 的世界里,它们只是四个没有任何联系的列表。在这种情况下,使用字典是更好的解决方案。

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']

在你的代码上下文中,使用字典:

更具体地说,在看了你的代码后,一个解决方案是把你的保存函数改成这样:

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)

这样你就可以轻松获取按第一个元素(名字)排序的结果,但不会把任何属性当作特殊的主键来处理。

撰写回答