-1

I use Python 3.6.3 and visual studio code

x=[],y=[]'a=[],d+[],i=0
a = ['Al', 'red', '1', '1', 'blue', 'green','', '65', \
'Bill', 'yellow', '1', '2', 'blue', 'red','', '55', \
'Alice', 'pink', '1', '3', 'blue', 'green','', '66', \
'Fred', 'pink', '1', '4', 'orange', 'puce','', '65]

for p in range(1,5):
    x=[a[2+i]]
    y=[a[3+i]]
    d(x,y)=a[0:8]    # variables x and y on left of = sign.
    i=i+8

print(d('1','2'))   
print(d('1','3')[0])

# want to get the following
'Bill', 'yellow', '1', '2', 'blue', 'red','', '55'
'Alice'

My question is: In Python, can variables be used on the left side of the = sign? What data structure or what else would do it? It worked in BASIC.

Why would I want to do this? Imagine I have 1000 groups instead of just these 4.

When I get answers, how do I acknowledge and approve or whatever I need to do. I haven't found a guide yet.

Mike Müller
  • 82,630
  • 20
  • 166
  • 161
  • 3
    "can variables be used on the left side of the = sign" - what do you mean? that's what assignment is. Please fix the code you posted so that it can be run without errors, and try to explain further what you mean because as it is now - it's not clear. – Nir Alfasi Oct 26 '17 at 03:52
  • @alfasin Check his code comments, seems like he meants that tuple but I don't get it. – Saelyth Oct 26 '17 at 03:52
  • 1
    What is `d(x,y)=a[0:8] ` supposed to mean? – Nir Alfasi Oct 26 '17 at 03:56
  • 1
    Variable declaration on the first line is not right. Check it. – skrubber Oct 26 '17 at 03:59
  • is d supposed to be a function?? – 0TTT0 Oct 26 '17 at 04:04
  • Python does not support variable variables, if that is what you mean. Newer languages have tended not to support this sort of thing, because experience has revealed that this easily leads to messy code. Rather, use a `dict`. Check out the accepted [answer for a possible duplicate target](https://stackoverflow.com/a/1373185/5014455). However, I am not sure if that is what you mean. Can you give a simpler example? Note this `x=[],y=[]'a=[],d+[],i=0` is a syntax error in Python, so it's hard to follow along. – juanpa.arrivillaga Oct 26 '17 at 04:21
  • "Imagine I have 1000 groups instead of just these 4.": you probably want to use `dict`s (also knows as dictionaries, maps, associative arrays and what not). –  Oct 26 '17 at 04:22
  • So, in Python, the naive way to do this is to use a list of lists. A "2-dimensional" list. Something like [this gist](https://gist.github.com/juanarrivillaga/93f1fcef84690b989a8299d1fd1b7c52) – juanpa.arrivillaga Oct 26 '17 at 04:31
  • What I still don't understand is what's going on with your `(1, 1), (1, 2), (1, 3), (1, 4)` scheme. Why can it not be replaced by `1, 2, 3, 4`, or better yet, indices in a list. Or was this just example data? – juanpa.arrivillaga Oct 26 '17 at 04:38
  • I HAVE TOI WORK NOW. WILL TRY TO ANSWER QUESTIONS WITHIN TEN HOURS IF I CAN GET FREE – Duane Garrison Oct 26 '17 at 15:17

3 Answers3

0

Looks like that you would like to use a dictionary:

a = ['Al', 'red', '1', '1', 'blue', 'green','', '65', 
    'Bill', 'yellow', '1', '2', 'blue', 'red','', '55', 
    'Alice', 'pink', '1', '3', 'blue', 'green','', '66', 
    'Fred', 'pink', '1', '4', 'orange', 'puce','', '65']

step = 8
d = {}
for start in range(0, len(a), step):
    end = start + step
    part = a[start:end]
    d[tuple(part[2:4])] = part


print(d['1','2'])   
print(d['1','3'][0])

Output:

['Bill', 'yellow', '1', '2', 'blue', 'red', '', '55']
Alice

This will work for any number of groups.

Mike Müller
  • 82,630
  • 20
  • 166
  • 161
  • Alfasin-- what I mean is that I want to use variables on the left side of the = sign in some data function that I would like to know about. Possibly a dictionary. d(x,y) would possibly be a dictionary defined by the variables x and y, which are defined by the 3rd and 4th list values in list a. Saelyth--x and y could be used as a tuple(x,y). 0ttt0--d could be a function, that is what I am looking for, a fuction that can use variables on the left side of the = sign. I need to give Mike's answer a try, it looks really promising. thanks all – Duane Garrison Oct 27 '17 at 02:08
  • It works beautifully even though I didn't put data in properly for showing both numbers increasing. But the question about variables on the left side of the = sign must be that it can't be done. I was trying to break my list into groups of groups (1,1 to 1,4)(2,1 to 2,4)(3,1 to 3,4) ect.. I was looking back 35 years and basic programming. I' m trying to move into newer language. They say Python is easy. – Duane Garrison Oct 28 '17 at 15:05
  • Great that it helped. BTW, you can [accept](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) an answer if it solves your problem. – Mike Müller Oct 29 '17 at 10:44
  • I got excited about the concise coding compared to what I was doing and forgot that my main question was, "In Python, can variables be used on the left side of the = sign?" I am used to creating names using variables in loops for new names. Like name(x)=value, where x will be from 1 to 20 and I will have created 20 names for 20 values. I'm afraid that I am not expressing myself well as nobody has addressed my question directly in any of the questions that I have asked. I will leave this open until someone will say yes or no to the question. thank you – Duane Garrison Oct 30 '17 at 02:42
  • Well the dictionary `d` in my solution is probably closest to what you mean with name. You assign new values to it in each iteration. Your `name(x)=value` is invalid syntax in Python because you call `name` with `()`. You cannot assign the result of a function call. – Mike Müller Oct 30 '17 at 08:13
  • What I am looking for is a way to use x(a variable) to create name(1), name(2), name(3), ..........name(100), so that I can compare, add, subtract, multiply, ect.. the values in those names. name(3) = 5, name(88) = 6, print(name(3) + name(88)) >>> 11. as a simplified example. in essence, using variables on the left side of an equation. Without being able to do this I will need to list possibly hundreds or thousands of names. It looks like Python can not do this, It is how it worked in basic. – Duane Garrison Oct 31 '17 at 22:55
  • In Python you use a dictionary for exactly your use case. `name = {} ; name[3] = 5; name[88] = 6; name[3] + name[88] >>> 111`. – Mike Müller Nov 01 '17 at 07:30
0

Mike Muller's answer works perfectly, even though I screwed up and forgot to increase the variable that stayed at 1. It works on my big list of 110 groups with the step value of 1435. I couldn't move from one group to the next.

0

I will close close this question by saying that the code given to me by Mike Muller and his comment about dictionaries let me see how to use variables on the left side of an = sign. So the answer to my question is yes. Here are my code with results.

step = 1435                                 # Mike Muller's 
d = {}
for start in range(0, len(l), step):
    end = start + step
    part = l[start:end]
    d[tuple(part[2:4])] = part
uu=1
vari = {}
for x in range(1,12):                         
    for y in range(1,15):
        if (str(x),str(y)) in d:
            vari[uu] = d[str(x),str(y)][50]
            print('key  ',x,y)                         # check results
            print ('value  ',vari[uu])                 # check results
            uu=uu+1                      
            print('counter  ',uu)                      # check results
print('out of loop check  ', vari[87])                 # check results


key   1 1
value   121
counter   2
key   1 2
value   124

and so on to the end

counter   87
key   11 6
value   121
counter   88
key   11 7
value   124
counter   89
key   11 8
value   121
counter   90
out of loop check   121