기본적으로 내 프로그램에서 나를 위해 여러 변수 (무제한)를 만들 수 있어야합니다.이 변수를 정의하지 않아도 코드를 통해 조작 할 수 있습니다.Python의 변수 중첩
저는 a1과 같은 변수 이름으로 문자와 숫자를 가지고 있고, 그 숫자에 1을 더하는 새로운 변수를 만드는 프로그램이 있다고 생각했습니다. 그러면 a1에서 a30 정도가됩니다. 어떻게하면 좋을까요?
내 프로그램은 다항식을 추가 할 것이고 변수 (또는 지금 목록)는 다른 단항 기호를 분리하는 것이며, 다항식에 얼마나 많은 단항 기호가 있을지 모르기 때문에 나는 다항식을 만들 수있는 방법이 필요했다. 유동적 인 숫자이므로 모노매일, 엑스트라 및 그 이하의 공간에 대한 정확한 정보를 얻을 수 있습니다. 나는 결국이 루프 할 것
# Sample polynomial set to x, the real code will say x = (raw_input("Enter a Polynomial")).
x = '(5xx + 2y + 2xy)+ (4xx - 1xy)'
# Isdigit command set to 't' to make the code easier to write.
t = str.isdigit
# Defining v for later use.
v = 0
# Defining 'b' which will be the index number that the program will look at.
b = 1
# Creating 'r' to parse the input to whatever letter is next.
r = x [b]
# Defining n which will be used later to tell if the character is numeric.
n = 0
# Defining r1 which will hold one of the monomials, (**will be replaced with a list**)
#This was the variable in question.
r1 = ''
# Setting 'p' to evaluate if R is numeric (R and T explained above).
p = t(r)
# Setting 'n' to 1 or 0 to replace having to write True or False later.
if p == True:
n = 1
else:
n = 0
# Checking if r is one of the normal letters used in Algebra, and adding it to a variable
if r == 'x':
v = 'x'
c = 1
elif r == 'y':
v = 'y'
c = 1
elif r == 'z':
v = 'z'
c = 1
# If the character is a digit, set c to 0, meaning that the program has not found a letter yet (will be used later in the code).
elif n == 1:
v = r
c = 0
# Adding what the letter has found to a variable (will be replaced with a list).
r1 = r1 + v
b = b + 1
:
여기에 코드입니다.
코드에 주석을 추가하여 이해하기 쉽습니다.
명이 배열을 발명 한 이유는 ... 파이썬에서 – none
는, 이것이''list'' 또는''set''입니다 :
여기에 코드입니다. 더 이상 시도하기 전에 언어의 기초를 배우는 것이 좋습니다 - 데이터 구조는 매우 기본적인 개념입니다. –
목록과 변수의 차이점을 알고 있습니다. 변수가 필요합니다. 질문을 읽었습니까? 기본 사항을 알고 있습니다. 목록에 대한 모든 것을 읽었습니다. – Nick