3

I'm an R newbie and am trying to create a basic "database" for my comic books. However, I have a problem.

The idea is to place each new entry as a list. I assumed that I could set up the lists to look like the following.

[Thor, 50, Marvel]
[Thor, 51, Marvel]
[Thor, 52, Marvel]
...
eventually, I 'd like to include entries for story arc, writer, artist, etc.

However, I'm using the following code to enter in the comics and have found that each new entry is just added to the end of the list.

option = 0
comicdb = []

while option != 3:
    print "--------------------------"
    print "1. Add a New Comic Book"
    print "2. Print the Database"
    print "3. Quit"
    option = int(raw_input("Pick an Option: "))
    if option == 1:
        title = raw_input("Comic Book Title: ")
        issue = int(raw_input("Issue Number: "))
        publisher = raw_input("Publisher: ")
        comicdb.append(title)
        comicdb.append(issue)
        comicdb.append(publisher)
        print comicdb

After running the code a couple times, the list looks like:

['Thor', 50, 'Marvel', 'Thor', 51, 'Marvel', 'Thor', 52, 'Marvel']

I'm assuming that one of the following things is wrong, but I can't figure it out:

  1. append is the wrong command to use
  2. i should use a dictionary or tuple instead of a list

Help!

3
  • I don't fully understand what you want to happen. What is the structure at the top of your question supposed to be? There's a list on each line, but what are the lists in? Are they supposed to be in a list, as in [['a', 1], ['b', 2]]? Commented Mar 23, 2011 at 2:59
  • That's acctually supposed to be three seperate lists. Commented Mar 23, 2011 at 3:03
  • ok, but where are they stored? Commented Mar 23, 2011 at 3:06

4 Answers 4

4

The answer is simple. you insert 3 words to the list instead of appending a list which contains the 3 words.

It should be like this:

option = 0
comicdb = []

while option != 3:
    print "--------------------------"
    print "1. Add a New Comic Book"
    print "2. Print the Database"
    print "3. Quit"
    option = int(raw_input("Pick an Option: "))
    if option == 1:
        title = raw_input("Comic Book Title: ")
        issue = int(raw_input("Issue Number: "))
        publisher = raw_input("Publisher: ")
        temp_list = []
        temp_list.append(title)
        temp_list.append(issue)
        temp_list.append(publisher)
        comicdb.append(temp_list)
        print comicdb
Sign up to request clarification or add additional context in comments.

Comments

4

You should use a nested structure.

comicdb.append((title, issue, publisher))

Comments

1

You need a list of lists or a list of dictionaries.

record = {}
record['title'] = raw_input("Comic Book Title: ")
record['issue'] = int(raw_input("Issue Number: "))
record['publisher'] = raw_input("Publisher: ")
comicdb.append(record)
print comicdb

Comments

0

As much as I commend your effort in writing a database from scratch, I must recommend that you have a look at SQLAlchemy - a Python "object relational mapper" that allows you to connect Python objects to SLQ-like databases (chiefly SQLite, the simplest, server-free one). I am amazed by the complexity that can be attained with very little code. It is clean, powerful, easy to understand, and grows rapidly.

I use it at work very successfully, and at home to keep track of all my daughters pictures (I have more megabytes (teras?) of picture of my daughters at home than I have from Cassini spacecraft at work, mind you).

So, if you are just practicing - go ahead. If you want to stand on solid ground and have a great and neat application for keeping track of your comics, then have a look at any database manager, but I recommend Python / SQLAlchemy.

Cheers.

2 Comments

Thanks for the suggestion. I just wanted to start with the basics, and eventually work my way to something like SQLAlchemy.
I still think that implementing an SQLAlchemy solution (or, really, any database manager for Python) is easier. In other words, you don't need to "make you way through" - they are very accessible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.