0

I am getting list output like this, I want to make clear separation between lists so that I can access each list separately how do I do that

for Book in rootNode.getBooks():
    CompareTables=[]
    CompareTables.append(Book.getName())
    for Table in Book.getTables():
        CompareTables.append(Table.getName())
    print CompareTables

Output:

['Document', 'A','B','C']
['Document', 'A','B','C','D','E','F']

I want output like this:

print CompareTables1

Output:

 ['Document', 'A','B','C']

print CompareTables2

 ['Document', 'A','B','C','D','E','F']
3
  • Would you mind explaining a little more? If I understand you, the goal to do the printing after the loop rather than inside the loop, is that right? Commented Mar 19, 2013 at 11:53
  • It doesnt matters as now when i am printing CompateTables its giving me different lists, that is ok, but I want to access them by different names as I want to compare elements in them Commented Mar 19, 2013 at 11:58
  • Did you make it work yet? Commented Mar 19, 2013 at 13:14

2 Answers 2

3

Create a new list to add your per-book lists to:

all_compare_tables = []
for Book in rootNode.getBooks():
    CompareTables=[]
    CompareTables.append(Book.getName())
    for Table in Book.getTables():
        CompareTables.append(Table.getName())
    all_compare_tables.append(CompareTables)

print all_compare_tables[0]
print all_compare_tables[1]

# better yet, loop:

for compare_tables in all_compare_tables:
    print compare_tables

Alternatively, you could use a dictionary:

all_compare_tables = {}
for i, Book in enumerate(rootNode.getBooks(), 1):
    CompareTables=[]
    CompareTables.append(Book.getName())
    for Table in Book.getTables():
        CompareTables.append(Table.getName())
    all_compare_tables['CompareTables{}'.format(i)] = CompareTables

The above example generates keys as CompareTables1, CompareTables2, etc. but perhaps you had better names in mind.

Printing these then becomes:

print all_compare_tables['CompareTables1']
print all_compare_tables['CompareTables2']

or loop over the all_compare_tables.keys() or all_compare_tables.values() sequences.

Sign up to request clarification or add additional context in comments.

23 Comments

[['Document','A','B','C' ]] [['Document', 'A','B','C','D','E','F']]
any more help? They still seems to be same, I want to have access them separtely
@AbhishekSharma: I don't think you executed my code correctly; you seem to have created multiple all_compare_tables lists inside of your loop.
No change its still the same print all_compare_tables['CompareTables1'] gives me both tables
@AbhishekSharma: and you are using the exact same code as in my samples? I very much doubt that you are. Please do doublecheck you are not creating the CompareTables list outside of the loop, for example.
|
0

Well, a dict is made for such cases:

CompareTables = {}
for Book in rootNode.getBooks():
    book_name = Book.getName()
    CompareTables[book_name] = []
    for Table in Book.getTables():
        CompareTables[book_name].append(Table.getName())
    print CompareTables

Here your 2 books seem to have same title Document, so you can use an index to differentiate them:

CompareTables = {}
for i, Book in enumerate(rootNode.getBooks(), 1):
    book_name = Book.getName() + str(i)
    CompareTables[book_name] = []
    for Table in Book.getTables():
        CompareTables[book_name].append(Table.getName())
    print CompareTables

10 Comments

{'Document1': ['A','B','C']} {'Document1': ['A','B','C','D','E','F']} STILL I CAN'T DISTINGUIS THEM
@Martijn: this is why I added the 2nd part ! Are you sure you read my post entirely ?
@Emmanuel: Print CompareTables is giving me both list, I want to make them separately accessible by two different list name
It seems my value of i is not increasing, every time it prints Document1 for both files
Because If a change value of i from 1 to 2 in loop, it changes the value to Document2, Document 2 in both, any suggestions?
|

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.