windows 10 pro office 2019 pro office 365 pro windows 10 home windows 10 enterprise office 2019 home and business office 2016 pro windows 10 education visio 2019 microsoft project 2019 microsoft project 2016 visio professional 2016 windows server 2012 windows server 2016 windows server 2019 Betriebssysteme office software windows server https://softhier.com/ instagram takipçi instagram beğeni instagram görüntüleme instagram otomatik beğeni facebook beğeni facebook sayfa beğenisi facebook takipçi twitter takipçi twitter beğeni twitter retweet youtube izlenme youtube abone instagram

Print name of list, instead of list

Asked By: Anonymous

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("%sn" % myList)
      f.write("---------------------------------n")
      for item in myList:
         f.write("%sn" % item)

However, f.write("%sn" % 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("%sn" % 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("%sn" % item)

However, i got this error:

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

Solution

Answered By: Anonymous

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("%sn" % 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.

techinplanet staff


Windows 10 Kaufen Windows 10 Pro Office 2019 Kaufen Office 365 Lizenz Windows 10 Home Lizenz Office 2019 Home Business Kaufen windows office 365 satın al follower kaufen instagram follower kaufen porno