开发者

Adding values to a list

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!


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


You should use a nested structure.

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


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜