0

I'm trying to basically create references to plot multiple relationships and store them in a list or possibly a dictionary.

Basically:

variable1 = 10

//in this case, 'ref' denotes that the variable should be a reference)
listA = [variable1(ref), variable2, variable3]
listB = [variable1(ref), variable4, variable5]

for i in listA:
i = i + 10

for i in listB:
i = i + 10

print listA[0]
//Should print 30

print listB[0]
//Should print 30

How can I split two references to the same variable into two separate lists?

1 Answers1

1

What about two lists, each containing keys of the same collection, say dictionary?

For example:

MASTER = [10,11,12,13,14]

LISTA = [0,1,2]
LISTB = [0,3,4]

for i in LISTA: MASTER[i] += 10
for i in LISTB: MASTER[i] += 10

print MASTER[LISTA[0]]

print MASTER[LISTB[0]]

ideone example

Or using a wrapper class:

class SharedInt:
    val = None
    def __init__(self, v): self.val = v

    def __add__(self, a): 
        self.val += a
        return self.val

    def __int__(self): return self.val

v1 = SharedInt(10)

listA = [v1, 11, 12]
listB = [v1, 13, 14]

for i in listA: i += 10
for i in listB: i += 10

print int(listA[0])
print int(listB[0])

ideone example

Lastly, or using embedded lists:

v1 = [10]

listA = [v1, 11, 12]
listB = [v1, 13, 14]

for i in listA: 
    if isinstance(i, list): i[0] += 10 
    else: i += 10
for i in listB: 
    if isinstance(i, list): i[0] += 10 
    else: i += 10

print listA[0]
print listB[0]

ideone example

Note that the first example treats all of your ListX members as "references" while the last two examples treats the members as "values", unless you make them SharedInt()s or enclose them in a list respectively.

In other words,

LISTA[1] = 21 # First example
ListA[1] = 11 # Second, third examples
jedwards
  • 29,432
  • 3
  • 65
  • 92
  • Those both work but I was looking for a cleaner command because this would become a nightmare in the program I'm writing. Is there a way to set the type of variable to mutable, immutable? Otherwise I may just have to use a dictionary. – CodeHard_or_HardCode Sep 21 '13 at 21:43
  • Well, ints will always be passed "by value" (in fact, a reference to an immutable object), so you have to do something to make that not happen. Wrapping it in a list (example 3) is a common technique, but it's not possible to pass it "by reference" as just a standard int. – jedwards Sep 21 '13 at 21:49
  • [This related question](http://stackoverflow.com/questions/15148496/python-passing-an-integer-by-reference) has some more discussion, but the outcome is the same unfortunately. – jedwards Sep 21 '13 at 21:53
  • If I set it as a dictionary, it becomes as a 'reference' no matter where it is stored, right? All of the objects of a dictionary point back to a single variable? – CodeHard_or_HardCode Sep 21 '13 at 21:56
  • Not exactly like that. For example `d = {'var1':10, 'var2':11, 'var3':12}` defines a dictionary with three entries. Something like `d['var1'] += 10` will increment the value associated with the key `var1` by 10. But `x=d['var1']; x+=10` will not -- `d['var1'] `will remain unchanged. – jedwards Sep 21 '13 at 21:59
  • Isn't there a way to make multiple keys to a single dictionary value? – CodeHard_or_HardCode Sep 21 '13 at 22:08
  • Not easily. The easy approach would be to map your multiple keys to a single "master" key (using one dictionary), then map your master key to a value using a second dictionary (or list). Alternatively, you could create your own class that did what you wanted, which wouldn't be too hard. – jedwards Sep 21 '13 at 22:10
  • I'm not too fond of classes, but maybe just a list of functions overwrite the value when called. Could you give an example of your master key method? – CodeHard_or_HardCode Sep 21 '13 at 22:18