3

I want to know how to output the name, not the values, of each index (and sub-index) in a list and in sub-lists.

For example, let's say I have a main list, which has 3 other lists each containing 5 sub-lists. Here is a small example to clarify:

List1 = [SubList1, SubList2, SubList3, SubList4, SubList5]
List2 = [Apple, Banana, Orange, Pear, Grape]
List3 = [Red, Yellow, Orange, Green, Purple]


Lists = [List1, List2, List3]

MainList = [Lists]

How can I print out 'List1'?

When I type in

MainList[0][0]

it outputs

[SubList1, SubList2, ..., Sublist5]

I want the actual name of the index, not its values.

I thought about using dictionary keys, but the report generator that I am using for the XML conversion outputs all the data into lists. Does this mean I have no choice but to modify my report generator script to output the data into dictionaries?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Mike Issa
  • 295
  • 2
  • 13

5 Answers5

2

Try to use dict() for that purpose:

List1 = ['SubList1', 'SubList2', 'SubList3', 'SubList4', 'SubList5']
List2 = ['Apple', 'Banana', 'Orange', 'Pear', 'Grape']
List3 = ['Red', 'Yellow', 'Orange', 'Green', 'Purple']


xd = {'List1': List1, 'List2': List2, 'List3': List3}

print xd['List1']

for k, v in xd.iteritems():
    if v == List1:
        print k

>>> ['SubList1', 'SubList2', 'SubList3', 'SubList4', 'SubList5']
>>> List1
xiº
  • 4,605
  • 3
  • 28
  • 39
  • Thank you, I tried your suggestion and I am receiving the following error: AttributeError: 'dict' object has no attribute 'iteritems' I am using Python 3.4 by the way – Mike Issa Oct 09 '15 at 15:16
  • Found the answer to my problem: http://stackoverflow.com/questions/30418481/error-dict-object-has-no-attribute-iteritems-when-trying-to-use-networkx – Mike Issa Oct 09 '15 at 15:18
  • 1
    @MikeIssa yep. You didn't need `iteritems()` in Python 3. It is generator by default. Just use `items()`. – xiº Oct 09 '15 at 15:21
  • Sadly, that's `O(n)` solution. – GingerPlusPlus Oct 09 '15 at 16:02
2

You seem to misunderstand. List1 in your example is a variable name. It is not stored in the Lists array, only its value is stored. So you are not going to get List1 out of Lists. Array indexes are integers and the index of List1 in Lists is 0. As the other commenter suggested you might want to convert your Lists structure into a dict.

Lists = {}
Lists['List1'] = [SubList1, SubList2, SubList3, SubList4, SubList5]
Lists['List2'] = [Apple, Banana, Orange, Pear, Grape]
Lists['List3'] = [Red, Yellow, Orange, Green, Purple]

print(Lists.keys())
GingerPlusPlus
  • 5,336
  • 1
  • 29
  • 52
Mad Wombat
  • 14,490
  • 14
  • 73
  • 109
1

list object has no name attributes, you should use pandas.Series as follow:

import pandas as pds
List1 = pds.Series([SubList1, SubList2, SubList3, SubList4, SubList5],name='List1')
List2 = pds.Series([Apple, Banana, Orange, Pear, Grape],name='List2')

you find the name like this:

print List1.name,List2.name

A list of list become a pandas.DataFrame:

Lists = pds.DataFrame([List1, List2, List3])

To answer your question, you should proceed as follow:

-each sublist become a series

Sublist = pds.Series([...],name='Sublist')

-each list become a dataframe:

List = pds.DataFrame([Sublist1,Sublist2,..])

-"the" Lists become a dict:

Lists = {'List1' : List1,
         'List2' : List2,
         ...
         }

-you get the name:

for name_list,list in Lists.items(): 
     name_list,list.columns
G.S
  • 392
  • 2
  • 13
1

Quick solution:

class NamedList(list):
    __slots__ = ['__name__'] # optimisation, can be ommited
    def __init__(self, name, *args):
        super().__init__(*args)
        self.__name__ = name
    def __repr__(self):
        return "%s(%r, %s)" % (type(self).__name__, self.__name__, super().__repr__())

test = NamedList('test', [1, 2, 3])
print(test)
print(test.__name__)

Output:

NamedList('test', [1, 2, 3])
test
GingerPlusPlus
  • 5,336
  • 1
  • 29
  • 52
1

If your lists always are stored as variables, you can retrieve their names by using some sort of identity dict (sadly there is none in standard library):

names = IdentityWeakKeyDict((obj, name for name, obj in locals().items()))

names should now map objects to their names.

GingerPlusPlus
  • 5,336
  • 1
  • 29
  • 52