-2

I have a symbolic sequence like x1,x2,...,x20 (without brackets). Can I take this sequence as variables in python? in other word y=x1,x2,x3,...,x20 ? At fist my list was x=[x1,x2,...,x20] and with command *x I converted it to sequencex1,x2,...,x20 and now for continuing my program I need to take this sequence as variable.

jared
  • 4,165
  • 1
  • 8
  • 31

2 Answers2

0

Since you say that you used *x to convert the sequence to variables (and you can only do *x in a function call or function signature like:

def f(x1,x2):
    print(x1, x2)
x = var('x1 x2')
>>> f(*x)
(x1, x2)

it seems like you mean that "once in the function, can I get the list of variables again?" The answer is "yes" -- the same way that you made them in the first place:

def f(x1, x2):
    y = x1, x2
    print(y)
>>> f(*x)
(x1, x2)

But maybe you mean "without having to retype the list?" The solution is to pass them as a tuple instead of unpacking them with the "*" operator:

def f(y):
    print(y)
>>> f(x)  # passing as tuple, not unpacking
(x1, x2)

(It is always better to add a little code so there is less guessing what you mean.)

jared
  • 4,165
  • 1
  • 8
  • 31
smichr
  • 16,948
  • 2
  • 27
  • 34
0

Yes, you can indeed treat a sequence of symbols as separate variables in Python. Your initial approach of having a list x = [x1, x2, ..., x20] and then unpacking it using the * operator is correct to create a sequence. Suppose your initial list is: x = [value1, value2, ..., value20] You can unpack this list directly into variables using tuple unpacking: x1, x2, x3, ..., x20 = x Now, x1, x2, x3, ..., x20 are independent variables with the corresponding values from the original list. If you want to continue working with these variables as a sequence, you can simply reference them in a tuple or list again: y = x1, x2, x3, ..., x20 # This creates a tuple