如何从用户输入调用列表?
How to call a list from a user input?
我正在尝试从我想通过用户输入定义的一系列列表中绘制特定列表。但是我不知道如何引用列表。
from matplotlib import pyplot as plt
x = []
y = []
cd = []
cl = []
xInput = input("Please choose the x-value input: ")
yInput = input("Please choose the y-value input: ")
[...]
data_array = np.asarray(data)
aeroData = data_array.astype(np.float)
for i in range(aeroData.shape[0]):
x.append(aeroData[i][0])
y.append(aeroData[i][1])
cd.append(aeroData[i][2])
cl.append(aeroData[i][3])
plot(xInput, yInput)
plt.title("{} vs. {}".format(xInput, yInput))
plt.xlabel("{}".format(xInput))
plt.ylabel("{}".format(yInput))
plt.show
如果您还没有在我们看不到的其他地方定义,您需要在某处定义 aeroData
。
另外, plot
似乎也没有定义。如果你想在那里定义它,你需要:
def plot(xInput, yInput):
plt.title("{} vs. {}".format(xInput, yInput))
plt.xlabel("{}".format(xInput))
plt.ylabel("{}".format(yInput))
plt.show()
如果它是 pyplot 的一部分,你应该需要 plt.plot(xInput, yInput)
用户输入的是字符串。
如果用户输入值列表:
xInput = [float(i) for i in input("Please choose the x-value input: ").split()]
如果列表已经定义并且用户只是按名称引用变量,则:
xInput = globals().get(input("Please choose the x-value input: "))
我认为更好的方法是使用 for-loop 来解析输入。
下面的代码将解析逗号分隔的字符串和 trim 所有非数字字符、点和破折号,将结果转换为浮点数。然后浮点数将附加到 'numeric' 数组。
x_input_str = input("Please choose the x-value input: ")
y_input_str = input("Please choose the y-value input: ")
xInput = []
for x in x_input_str.split(","):
x = re.sub(pattern=r"([^0-9\.\-])", repl="", string=x.strip())
xInput.append(float(x))
yInput = []
for y in y_input_str.split(","):
y = re.sub(pattern=r"([^\d.-])", repl="", string=y.strip())
yInput.append(float(y))
感谢@Hamish Wormald 的建议。我现在开始工作了!
from matplotlib import pyplot as plt
x = []
y = []
cd = []
cl = []
xInput = input("Please choose the x-value input: ")
yInput = input("Please choose the y-value input: ")
[...]
data_array = np.asarray(data)
aeroData = data_array.astype(np.float)
aeroValues = [x, y, cd, cl]
aeroKeys = ["x", "y", "cd", "cl"]
aeroDict = dict(zip(aeroKeys, aeroValues))
if xInput in aeroKeys:
if yInput in aeroKeys:
# Plot data
plt.plot(aeroDict[xInput], aeroDict[yInput], '-bo')
plt.title("{} vs. {}".format(yInput, xInput))
plt.xlabel("{}".format(xInput))
plt.ylabel("{}".format(yInput))
plt.savefig("{} vs. {}.png".format(yInput, xInput))
plt.show()
我正在尝试从我想通过用户输入定义的一系列列表中绘制特定列表。但是我不知道如何引用列表。
from matplotlib import pyplot as plt
x = []
y = []
cd = []
cl = []
xInput = input("Please choose the x-value input: ")
yInput = input("Please choose the y-value input: ")
[...]
data_array = np.asarray(data)
aeroData = data_array.astype(np.float)
for i in range(aeroData.shape[0]):
x.append(aeroData[i][0])
y.append(aeroData[i][1])
cd.append(aeroData[i][2])
cl.append(aeroData[i][3])
plot(xInput, yInput)
plt.title("{} vs. {}".format(xInput, yInput))
plt.xlabel("{}".format(xInput))
plt.ylabel("{}".format(yInput))
plt.show
如果您还没有在我们看不到的其他地方定义,您需要在某处定义 aeroData
。
另外, plot
似乎也没有定义。如果你想在那里定义它,你需要:
def plot(xInput, yInput):
plt.title("{} vs. {}".format(xInput, yInput))
plt.xlabel("{}".format(xInput))
plt.ylabel("{}".format(yInput))
plt.show()
如果它是 pyplot 的一部分,你应该需要 plt.plot(xInput, yInput)
用户输入的是字符串。
如果用户输入值列表:
xInput = [float(i) for i in input("Please choose the x-value input: ").split()]
如果列表已经定义并且用户只是按名称引用变量,则:
xInput = globals().get(input("Please choose the x-value input: "))
我认为更好的方法是使用 for-loop 来解析输入。
下面的代码将解析逗号分隔的字符串和 trim 所有非数字字符、点和破折号,将结果转换为浮点数。然后浮点数将附加到 'numeric' 数组。
x_input_str = input("Please choose the x-value input: ")
y_input_str = input("Please choose the y-value input: ")
xInput = []
for x in x_input_str.split(","):
x = re.sub(pattern=r"([^0-9\.\-])", repl="", string=x.strip())
xInput.append(float(x))
yInput = []
for y in y_input_str.split(","):
y = re.sub(pattern=r"([^\d.-])", repl="", string=y.strip())
yInput.append(float(y))
感谢@Hamish Wormald 的建议。我现在开始工作了!
from matplotlib import pyplot as plt
x = []
y = []
cd = []
cl = []
xInput = input("Please choose the x-value input: ")
yInput = input("Please choose the y-value input: ")
[...]
data_array = np.asarray(data)
aeroData = data_array.astype(np.float)
aeroValues = [x, y, cd, cl]
aeroKeys = ["x", "y", "cd", "cl"]
aeroDict = dict(zip(aeroKeys, aeroValues))
if xInput in aeroKeys:
if yInput in aeroKeys:
# Plot data
plt.plot(aeroDict[xInput], aeroDict[yInput], '-bo')
plt.title("{} vs. {}".format(yInput, xInput))
plt.xlabel("{}".format(xInput))
plt.ylabel("{}".format(yInput))
plt.savefig("{} vs. {}.png".format(yInput, xInput))
plt.show()