Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将输入同时用作数组和字符串_Python_Python 2.7 - Fatal编程技术网

Python 将输入同时用作数组和字符串

Python 将输入同时用作数组和字符串,python,python-2.7,Python,Python 2.7,我觉得有一个简单的解决办法,但我有点新的 stat_input= input("Hello Mr. Jenner, what are you interested in tracking today?") 我使用这样的输入,稍后用于调用数据,并使用该数据计算统计数据并生成直方图/正态分布 它工作得很好。下面是一些使用它的示例 cur.execute('SELECT {} FROM statdata'.format(stat_input)) np.array(stat_input).astype

我觉得有一个简单的解决办法,但我有点新的

stat_input= input("Hello Mr. Jenner, what are you interested in tracking today?")
我使用这样的输入,稍后用于调用数据,并使用该数据计算统计数据并生成直方图/正态分布

它工作得很好。下面是一些使用它的示例

cur.execute('SELECT {} FROM statdata'.format(stat_input))
np.array(stat_input).astype(np.float)

sigma = math.sqrt(np.var(stat_input))
因此,如果我键入
threemonthdata
,它将从数据库中提取数据数组并使用它。太棒了。然而,我有一个小问题

我知道
threemonthdata
指的是一个数组。因为我正在创建图表,所以我希望使用输入作为标题,以便图表标题标识我正在绘制和使用的数据(作为将来的参考)

这不管用

ax.set_title(' + stat_input + ') 
这也不是。我希望标题写为
Threemonthdata
。但是如果我输入
twomonthdata
我希望它说
twomonthdata
,而不是给我数字数组


有什么想法吗?

我从来没有上过psycopg的课。但是,从我所读到的,这一个似乎为您完成了将您的字符串转换为一个名称与引用字符串相同的列表的工作

因此,在重写字符串之前,如何定义另一个变量来存储该字符串?如下

stat_input_title = stat_input.capitalize()
cur.execute('SELECT {} FROM statdata'.format(stat_input))
此后,
stat\u input\u title
stat\u input
可以在没有冲突的情况下一起使用

ax.set_title(stat_input_title)

看起来您面临的问题是传递的是
set\u title()
字符串
'stat\u input'
,而不是变量
stat\u input
。您可能只需要使用:

ax.set_title(stat_input) 

您是对的,但是
math.sqrt(np.var(stat_input))
在这种情况下如何工作呢
stat\u input
是一个数组,因为OP说:“我想要标题[…]而不是给我数字数组。”而且,OP似乎认为通过执行
“stat\u input”
,他将获得numpy数组的原始名称作为字符串。实际上,他好像在用stata编写代码,处理本地宏。
ax.set_title(stat_input)