使用输入作为数组和字符串

时间:2017-05-14 21:16:19

标签: 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(np.float)

sigma = math.sqrt(np.var(stat_input))

因此,如果我输入threemonthdata,它将从我的数据库中提取数据并使用它。这很棒。但是,我有一个小问题

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

ax.set_title('stat_input')

这不起作用

ax.set_title(' + stat_input + ') 

这也不是。我希望标题说Threemonthdata。但是,如果我输入twomonthdata,我希望它说twomonthdata,而不是给我一组数字。

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

我从未玩过psycopg的cursor课程。但是,从我可以阅读的内容来看,似乎这个可以帮助您将字符串转换为名称与引用字符串相同的列表。

那么在重写之前定义另一个变量来存储字符串呢?如下

stat_input_title = stat_input.capitalize()
cur.execute('SELECT {} FROM statdata'.format(stat_input))

此后,stat_input_titlestat_input可以与冲突一起使用。

ax.set_title(stat_input_title)

答案 1 :(得分:0)

您面临的问题似乎是您传递coords字符串d0086 <- c(227, 180) d0126 <- c(282, 149) coords <- cbind(d0086,d0126) nb <- dnearneigh(coords,0,1000) nb2listw(nb, style="W") ############### Characteristics of weights list object: Neighbour list object: Number of regions: 2 Number of nonzero links: 2 Percentage nonzero weights: 50 Average number of links: 1 Weights style: W Weights constants summary: n nn S0 S1 S2 W 2 4 2 4 8 ,而不是变量set_title()。您可能只需要使用:

'stat_input'
相关问题