-2
data1 = [1,2,3,4,5]
data2 = [7,8,9,10,11]  


x = data1
y = data2

What I required are the strings of above variable as follows:

xlabel = 'data1'
ylabel = 'data2'

Rather than writing manually, how can I call using x like below:

xlabel = str(x) 

But above is wrong. So, how to do?

xlabel =???

Is there a pythonic way of doing it?

Peter O.
  • 32,158
  • 14
  • 82
  • 96
puti
  • 537
  • 1
  • 5
  • 9
  • You want to set `x` and `y` to the name of the variables `data1` and `data2` ? – flau Aug 21 '14 at 16:32
  • to the name of variables, but as the strings – puti Aug 21 '14 at 16:33
  • 1
    Why do you want to do this? It's probably possible but I doubt it's a good idea. – dshepherd Aug 21 '14 at 16:33
  • 7
    There's almost no reason to ever do this in the global space. If you want that kind of functionality, use a dictionary to store your values. – Roger Fan Aug 21 '14 at 16:33
  • what do you want to do with the string? – Padraic Cunningham Aug 21 '14 at 16:33
  • 3
    I suggest you read [this](http://nedbatchelder.com/text/names.html). Names aren't properties of objects, they're just "tags" to use to refer to them. – jonrsharpe Aug 21 '14 at 16:33
  • 1
    If you have the variable `data1` in order to write `str(data1)`, then it is within your power to write `'data1'` instead .. :) – wim Aug 21 '14 at 16:34
  • this is just to avoid manual works. i have to use the same name of variables as the strings for further processing – puti Aug 21 '14 at 16:35
  • Put the lists into another list and operate on the list of lists instead. – dshepherd Aug 21 '14 at 16:35
  • 1
    Then you should either use a dictionary if you want to index with strings or use a list if you just want to store them in order. – Roger Fan Aug 21 '14 at 16:38
  • @wim there is difference between str(data1) and 'data1' in my codes, because the former one refers to the string of lists. – puti Aug 21 '14 at 16:38
  • @Marcin this is the other direction (getting the string from the object, not making an object from the string) – wim Aug 21 '14 at 16:43
  • 1
    @puti the Pythonic way is **not to do this**. If you need to access the lists using a string, put them in a dictionary `{'data1': [...], ...}`. – jonrsharpe Aug 21 '14 at 16:56
  • @jonrsharpe i do not need to acess the lists as string; i need to access list's name as string. – puti Aug 21 '14 at 16:57
  • @puti and several people have told you several different ways that that isn't a sensible thing to be doing. `str(x)` gives you a string representing the object the name `x` refers to (`x.__str__()`). There is **almost certainly** a better way to get around whatever has made you think you need this - if you explain what you're actually trying to achieve we might be able to help you find it. – jonrsharpe Aug 21 '14 at 17:01
  • related: http://stackoverflow.com/q/12189000/674039 – wim Aug 21 '14 at 17:01
  • 1
    @jonrsharpe plt.scatter(x,y) then plt.xlabel(xlabel) – puti Aug 21 '14 at 17:03
  • @jonrsharpe since xlabel is 'data1'. I did not want to write it manually – puti Aug 21 '14 at 17:05
  • So where does that name `data1` come from to begin with? Please edit the question with a [minimal example](http://stackoverflow.com/help/mcve). – jonrsharpe Aug 21 '14 at 17:06
  • first the text file is read and data1 and data2 are used as two lists – puti Aug 21 '14 at 17:08
  • So is the name `data1` read in from the text file? Or is that just the first dataset from the file (in which case, why not have a list and use `'data{0}'.format(index)`)? Again, **edit the question**. – jonrsharpe Aug 21 '14 at 17:15
  • @Wim It's the same thing - the mistake is to treat variables and variable names as data, rather than using appropriate data structures. – Marcin Aug 21 '14 at 18:18
  • I think the "pythonic" way of doing what the OP is asking is to use your editor or sed or similar tool to replace all of "x" with "data1". E.g. '/s/x/data1/g'. Ok, so not really pythonic, but the way a pythonista might save the trouble of manually changing x to data1. – Jed Daniels Aug 21 '14 at 18:57
  • Maybe a noob question but I have the doubt. Why is not a good idea? Apartly it would not be so useful, Is there a reason to say it would be catastrophic? – PySerial Killer Nov 06 '17 at 17:29

3 Answers3

5

To see why this makes no sense, consider the following snippet

def magic_name_getter(object):
    # here be dragons
    return the_name

data1 = [1,2,3,4]
magic_name_getter(data1)  # returns 'data1'

data1 = [1,2,3,4,5]
data2 = [1,2,3,4,5]
magic_name_getter(data2)  # returns 'data2' (..or 'data1'?)

magic_name_getter([7,8,9,10,11])  # returns ... ummm ... ???

Objects in python can have many names, or no names at all.

So what you want, whilst not impossible, is very difficult. Since the variable names are important data for your use case, you should instead be using a dictionary of keys and values mapping the names to the lists so that you have easy access to the names (keys) aswell as the data (values).

wim
  • 338,267
  • 99
  • 616
  • 750
  • NameError: global name 'the_name' is not defined – puti Aug 21 '14 at 16:43
  • 4
    LOL. Exactly! Glad you finally get it! – wim Aug 21 '14 at 16:44
  • 3
    ROFL. I think the OP tried to run it. Original Poster, I don't think "magic_name_getter" and "the_name" will actually work. It is a thought experiment, not an empirical one. Just pretend it is "magic" and that it would work if magic were possible. – Jed Daniels Aug 21 '14 at 16:51
  • @wim please see my edited question. i could not get what i expected. – puti Aug 21 '14 at 16:52
3

You should really consider organizing your data, e.g., in a dictionary:

a = dict(
    data1 = [1,2,3,4,5],
    data2 = [6,7,8,9,10],
    )

x = a.keys()[0]
y = a.keys()[1]

print x, y

Output:

data1 data2

Usually you won't need to store the keys in separate variables like x and y, but work directly on a.keys() or a.items().

Falko
  • 17,076
  • 13
  • 60
  • 105
1

collections.namedtuple might help you out:

import collections
Parameters = collections.namedtuple('Parameters', 'dataname, data', verbose = False)
x = Parameters('data1', [1,2,3,4,5])
y = Parameters('data2', [7,8,9,10,11])

>>> x.dataname
'data1'
>>> x.data
[1, 2, 3, 4, 5]
>>> y.dataname
'data2'
>>> y.data
[7, 8, 9, 10, 11]

You would use it for your plot like this:

plt.scatter(x.data, y.data)
plt.xlabel(x.dataname)
wwii
  • 23,232
  • 7
  • 37
  • 77