【发布时间】:2013-06-09 18:41:24
【问题描述】:
我在 Mac OSX Lion 上使用 Python 2.7。我将IPython 与Pandas 0.11.0、Numpy 和Statsmodels 包一起使用。
我正在编写一个函数,允许用户对文件进行逻辑回归,指定在构建模型时要使用哪些变量,哪些应该转换为虚拟变量,哪些变量应该是自变量。
例如,当我执行以下操作时:
cols_to_keep = []
print (df.columns)
i = eval(raw_input('How many of these variables would you like to use in logistic regression?: '))
while i != 0:
i = i - 1
print (df.columns)
addTo = raw_input('Enter a variable for this list that you would like to keep and use in logistic regression.: ')
cols_to_keep.append(addTo)
我最终会遇到问题。特别是当我要求用户从列表中指定因变量,然后需要将该变量从训练变量列表中取出时:
print (df.columns)
dependent = raw_input('Which of these columns would you like to be the dependent variable?: ')
training.remove(dependent)
我发现,插入打印语句后,添加到训练变量列表中的变量如下所示:
('these are the traing variables: ', ['access', u'age_age6574', u'age_age75plus', u'sex_male', u'stage_late', u'death_death'])
似乎u 已放置在每个用户指定的变量之前。
我的问题是:为什么会这样以及如何修复/解决这个问题,以便当用户指定因变量时,它实际上已从列表中删除。这也发生在用户指定变量并将其添加到列表的所有其他情况下,如果我需要用户观察列表,则会造成混乱。
【问题讨论】:
-
将其保留为 unicode,您可以美化您的打印方式,否则这不是问题
标签: python list python-2.7 ipython