0

I want to create a list of variables in Python, where the items in the lists are variables and when I change the variables, the items in the list change as well. For example:

a = 5
someList.append(a)
a = 3
someList
someList[0.value()]

Output:
[a]
[3]

ADDENDUM

Why do I want to do this?

Because I want to search for the minimum across a few sorted arrays of different sizes. At some point, the iterator pointer of one of the arrays becomes a null pointer and I want to remove that array from the list of arrays I am searching across.

The only way I can see to do that is to be able to create a list of variables that point to these arrays, so they can be removed when no longer relevant.

7
  • 10
    Why on Earth would you want to do that? Sounds like an XY problem to me. Commented May 8, 2014 at 18:54
  • Dude, just have a list. You don't need the other stuff. Commented May 8, 2014 at 18:55
  • 1
    This is not possible as you describe it - once you append the item to the list, there is no link to the name a. You could hack something similar together, but this is definitely a case where the result you want will be obtainable in a better way. Commented May 8, 2014 at 18:55
  • 1
    Are you looking for a dict? docs.python.org/2/tutorial/datastructures.html#dictionaries Commented May 8, 2014 at 18:56
  • You could use magic methods to create a custom type that acts like a primitive, and then assign instances of that type to the local variables and entries in the list, but the syntax will be a bit wonky, and you definitely won't be able to get your 0.value() indexing syntax. I'm with the others though in saying there's probably a better way to solve the problem that you are trying to address here. Commented May 8, 2014 at 18:58

2 Answers 2

4

ok I'm ignoring everything else you said, because I already took pain killers after reading it.

Because I want to search for the minimum across a few sorted arrays of different sizes

You say you have a "few" sorted arrays of "different sizes":

>>> l1 = [4,5,6,7,8,9]
>>> l2 = [2,7,8,9,11,12,13,14]
>>> l3 = [3,6,9,12,15,18,21]
>>> l4 = [5,6,7,8]

you want to find the minimum?

>>> ll = [l1,l2,l3,l4]
>>> min([l[0] for l in ll])
2

here you have it!

Here's the "trick": sorted array have a "magical" property: the smallest element is always at one end, usually at the first position!

\o/

you see, no pointers, no iteration, no headache!

let's make it a function:

>>> def get_minimum_of_many_lists(*args):
...    return min([l[0] for l in args])
...
>>> l1 = [4,5,6,7,8,9]
>>> l2 = [2,7,8,9,11,12,13,14]
>>> l3 = [3,6,9,12,15,18,21]
>>> l4 = [5,6,7,8]
>>>
>>> print(get_minimum_of_many_lists(l1,l2,l3))
2
>>> print(get_minimum_of_many_lists(l1,l3,l4))
3
>>> print(get_minimum_of_many_lists(l1,l4))
5
>>> print(get_minimum_of_many_lists(l1,l2,l3,l4,[0,1,2,3]))
0
Sign up to request clarification or add additional context in comments.

4 Comments

Ah that's brilliant. So now, how do I eliminate the result of the first time (i.e. 2) from showing up when I run this again?
you do not eliminate, you just use the scoping to your advantage! Use the function, Luke!
The scoping? Please tell me more Obi!
The scope is the whole area where a variable can be used. It's usually from the place it is being declared, until the end of the current block (defined by the indentation in python).
0

There's no easy way (if there's any) to store variables just as you want inside a list; you should use dictionaries instead; you get easy naming, listing and modifying of variables. Example:

a=1
b=1
myDict={}
myDict['a']=a
myDict['b']=b
print myDict

>> {'a': 1, 'b': 1}

myDict['b']=5
print myDict

>> {'a': 1, 'b': 5}

Comments

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.