user1584421
user1584421

Reputation: 3863

Print name of list, instead of list

I have a list of lists:

sensors = [[] for i in range(8)]

I am creating an export to text file from this list of lists.

I want to print the name of each list , following the contencts of the sublist. For example, i want:

sensors[0]
------------
1
2
3

sensors[1]
------------
4
5
6

where sensors[0] = [1,2,3] and sensors[1] = [4,5,6].

I have tried this so far:

with open(fileName, 'w') as f:
   for myList in sensors:
      f.write("%s\n" % myList)
      f.write("---------------------------------\n")
      for item in myList:
         f.write("%s\n" % item)

However, f.write("%s\n" % myList) gives me not the name of the array but it's contents.

So i get:

[1,2,3]
------------
1
2
3
    
[4,5,6]
------------
4
5
6

This is understandable. But in this code snippet, how i can replace the f.write("%s\n" % myList) line, in order to print the name of the sub-list?

EDIT: This is the code i used after Rahul's suggestion:

with open(fileName, 'w') as f:
         for myList in range(len(sensor)):
              f.write("%s" % namestr(sensor).strip() + '[' + str(myList) + ']\n')
              f.write("---------------------------------\n")
              for item in myList:
                 f.write("%s\n" % item)

However, i got this error:

   for item in myList:
TypeError: 'int' object is not iterable

Upvotes: 1

Views: 647

Answers (1)

Rahul Anand
Rahul Anand

Reputation: 38

Let me know if I have fully understood your problem. So you want to get variable name not its value.

def namestr(obj):
   return [name for name in globals() if globals()[name] is obj][0]

var = 'Hello'
print(var)
print(namestr(var))

Output

Hello
var

Well, I know that above logic doesn't make sense as it is just iterating through a global namespace and return what we need.

But seems like in your case, you probably need sublist name like above. But when it is known that sensors[0] is not a placeholder or variable. It is just a memory location that program refers to.

Edit

But we can do some string manipulations.

sensors = [[] for i in range(8)]
sensors[0] = [1,2,3]
sensors[1] = [4,5,6]
print(sensors)

def namestr(obj):
   return [name for name in globals() if globals()[name] is obj][0]


fileName = 'somefile'
with open(fileName, 'w') as f:
   for ind in range(len(sensors)):
      f.write("%s" % namestr(sensors).strip() + '[' + str(ind) + ']\n')
      f.write("---------------------------------\n")
      for item in sensors[ind]:
         f.write("%s\n" % item)

Output File Content

sensors[0]
---------------------------------
1
2
3
sensors[1]
---------------------------------
4
5
6
sensors[2]
---------------------------------
sensors[3]
---------------------------------
sensors[4]
---------------------------------
sensors[5]
---------------------------------
sensors[6]
---------------------------------
sensors[7]
---------------------------------

What I did? I just got the name of your list using namestr function which I created in the program and then carefully getting the index of the list and some basic string manipulations.

Hope it will help.

Upvotes: 1

Related Questions