I had a small requirement of making a specific attribute as primary key, such that by using this attribute I want to access the other attributes of the same row. Could any one help me please.
Description:: I had 4 arrays and I want to retrieve the data in the arrays corresponding to the row I have selected.In simple terms I am using Python as a database and store the retrieve the values of other tuples basing on a specific column value.
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()
This is my code. After storing all the values in the respective arrays.I want to sort them basing on name, to the code I have written the output I got is the sorted array of only the name attribute, Instead I want all the corresponding values also to be sorted.Please help me
Replying to original question (it’s been erased):
You could use a python dict: http://docs.python.org/library/stdtypes.html#dict.
Then if you wanted the values associated with ‘key2’:
In the context of your code, using a dict:
More specifically, after seeing your code, one solution would be to change your save function to this:
where
employeesis a dict. This gives you your primary key property stated in your question, but doesn’t give you easy access to sorted results as your code hints at.OR, using a list:
where
employeesis a list, not a dict. Then you can use thesortedbuilt-in function (http://docs.python.org/library/functions.html#sorted) combined with theoperatormodule to get your sorted results:This gives you easy access to results sorted by the first element (the names), but doesn’t treat any of the attributes as a special primary key.