Hi
I have certain number of books which each have an author, title and ISBN number. How is the best way I can store this so that when I want a list of the books I will be able to list the books with the title, author and ISBN number?

I was thinking about using a list or dictionary but don't know how to do it.

Cheers

Recommended Answers

All 11 Replies

Is there another way other than that. Maybe linked lists?

Member Avatar for leegeorg07

if you were to use lists you would have to have 3 seperate links and print all three when you want to view them or you could use a dictionary and a list or a ditionary with two of the peices of information grouped into one

You could easily make a class called Book and have a list of that. It could go:

class Book(object):
    def __init__(self):
        self.Title = name
        self.Author = author
        self.ISBN = ISBN

    def getAuthor(self,author):
        if author == self.Author:
            return self
            #you could have other such methods for ISBN and Title

books = []
b1 = Book("Marlie and Me","John Grogan",1234)
b2 = Book("The Northern Lights", "Phillip Pullman",4321)

for f in books:
    if f.getAuthor("Phillip Pullman"):
        print f.Title

I think that works.. i haven't put it into IDLE or anything, but generally classes are a very good way to go.

Using an SQLite database is the simpliest and best. It comes with Python and a tutorial is here http://www.devshed.com/c/a/Python/Using-SQLite-in-Python/

Yeah, SQLite is Ideal for that work. I advice you to take a leap. It is very simple and need no server (Zero config). Try it and you will enjoy. Here is a paper from www.python.org :
http://docs.python.org/library/sqlite3.html

Here google results
http://www.google.co.tz/search?q=python+sqlite&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a

Whatever you decide, jump in and try it. You will have to post some code if you want any specific help.

Hi

Thanks yeah I have it working now.

I have a bit of a problem increases the number of books in stock if the ISBN number is already in the dictionary. I am using the ISBN number as the Key then have a list with the Author, Title and Number in Stock in a list as the value attached to the key. How do you increase the stock if the ISBN is already in the dictionary?

booklist = {}

def addbook(ISBN, author, title, stock):

    if booklist.has_key(ISBN):

	stock = stock + 1

    else:

	booklist[ISBN] = [author, title, stock]



def listofbooks():

    for book in booklist:

	print booklist[book]



def findbook(ISBN):

    if booklist.has_key(ISBN):

	print booklist[ISBN]

    else:

	print "book not found"



addbook("1234", "Steven", "Book 1", 1)

addbook("2345", "Tom", "Book 2", 1)

addbook("1234", "Steven", "Book 1", 1)

listofbooks()

findbook("1234")

You can change addbook like this

def addbook(ISBN, author, title, stock):
    if ISBN in booklist:
	booklist[ISBN][2] += stock
    else:
	booklist[ISBN] = [author, title, stock]

If you do not understand the shorthand version in Gribouillis' code, it is the same as

def addbook(ISBN, author, title, stock):
    if ISBN in bookdic:
        ## booklist[ISBN][2] += stock
        ISBN_list = bookdic[ISBN]   ## returns the list for this key
        print ISBN, ISBN_list
        ISBN_stock = ISBN_list[2]
        ISBN_stock += stock
        ISBN_list[2] = ISBN_stock
    else:
	bookdic[ISBN] = [author, title, stock]

As you can see, the original code is much simplier once you understand it.

Can someone explain how to add a file to a dictionary.
The file is simple. Maybe a word or a number on each line.
Here is the code I have. Didn't know if I should use this post or start a new one. Just in case I will start a new one.
Do not run the program i get an unusually long output

import random

def main():
    ##Open a file
    outfile = open('numbers.txt', 'w')
    for count in range(20):
        ##Get random numbers
        rebmun = random.randint(1,99)
        outfile.write(str(rebmun) + '\n')
    outfile.close()
    text = open('numbers.txt','r').read()
    muns = text.split()
    
    numbers = {}
    for ch in muns:
        numbers[ch] = numbers.values()
        print numbers
main()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.