如何使一个特定的属性作为python的主键
我有一个小的要求,使一个特定的属性作为主键,这样通过使用这个属性我想访问同一行的其他属性。请任何人帮助我。如何使一个特定的属性作为python的主键
说明::我有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()
这是我的代码。在所有的值存储在各自的数组后。我想根据名称对它们进行排序,我写的代码我得到的输出是只有名称属性的排序数组,而我希望所有相应的值也被排序。请帮我
回复原来的问题(它已经被删除):
你可以使用Python字典: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
是字典。这会为您提供在您的问题中说明的主键属性,但不会让您轻松访问按代码提示的排序结果。
OR,使用列表:
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)
这给了你轻松访问按第一个元素(名称)排序的结果,但不会将任何属性视为特殊主键。
Dude你的回答是r8,但它不是我所问过的。请看一下这个问题。 – 2011-12-26 05:41:17
我瞥了一眼这个问题。如果我误解了你,也许你可以写一个更清晰的问题。我假设你可以把你的4个数组的值作为python字典中的值元组的元素。你可以使用你想要的主键作为字典的关键。这似乎满足你的问题的限制。 – 2011-12-26 05:44:55
我已经给我的代码..请通过它,并priovide我ans – 2011-12-26 05:55:09
在你的代码编写的方式,您的相关数据集(我猜想他们是n
,ge
,idee
和desi
不是彼此相关。你试图通过n.sort()
排序名称的列表,但这将不起作用;数据松散耦合
由于您试图对基于个人员工姓名的特别是的数据进行排序,因此最理想的解决方案是使用字典。通过@Sancho之前,Python字典允许你创建一个关键字(在这种情况下,雇员的名字)和值之间的关系(在这种情况下,无论attribut之后员工才有的)。因为程序员很清楚数据集与相关,但在Python的世界中,它们只是四个列表,每个列表在每个列表之间没有轴承关系在这种情况下使用字典是更好的解决方案。)
你可以发布一些代码来说明你试过了什么,你得到了什么输出以及你想要什么输出? – Makoto 2011-12-26 05:44:44
我已经给出我的代码..请通过它并priovide我ans – 2011-12-26 05:55:18
感谢您提供的代码,但你已经消除了原来的问题,使这个线程中的评论远没有用。您能否重新发布原始问题的内容,并在您的代码下面填写?谢谢。 – 2011-12-26 05:56:39