我试图从一组字符串中删除元素,但找不到它们。 这个集合被生成为一个frozenset(由一个超出问题范围的库生成),我将它转换为一个正则集。你知道吗
>>> geneset = model.reactions[2915].genes
>>> geneset
frozenset(['YGL080W', 'YGR243W', 'YHR162W', 'AND', 'OR'])
>>> geneset_mutable = set(geneset)
>>> geneset_mutable
set(['YGR243W', 'OR', 'YGL080W', 'AND', 'YHR162W'])
>>> 'OR' in geneset_mutable
False
>>> "OR" in geneset_mutable
False
>>> geneset_mutable.remove('OR')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'OR'
集合可编辑:
>>> geneset_mutable.add('OR')
>>> "OR" in geneset_mutable
True
>>> geneset_mutable.remove('OR')
>>> "OR" in geneset_mutable
False
>>> geneset_mutable
set(['YGR243W', 'AND', 'YHR162W', 'OR', 'YGL080W'])
>>> geneset_mutable.remove('OR')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'OR'
为什么找不到元素? 有什么特别的方法可以调试这个吗?你知道吗
记录在案:
>>> sys.getdefaultencoding()
python -V
Python 2.7.5
如果您要直接在集合中键入,它可以正常工作:
我的结论是
model.reactions[2915].genes
并没有返回字符串,它返回的基因的repr看起来像“OR”,但却是不同类型的对象(与您的查询不匹配)。你知道吗这很容易确认,只需运行如下测试:
解决方法是创建一个基因对象,该对象可以与集合中的成员完全匹配。如何做到这一点取决于模型的实现方式,但应该如下所示:
希望这能让你重新走上基因分析的道路:-)
当我逐字复制您的命令时,
'OR' in geneset_mutable
返回True
。我想知道你的集合中的元素后面是否有一些不可打印的字符。请尝试以下操作:'OR' in new_set
现在计算为True
吗?你知道吗如果这不起作用,我也会尝试
set(geneset).add('OR')
。您很可能会在集合中看到'OR'
两次,表明它不是您认为的“或”。你知道吗最后,因为Python是强类型的,所以可以尝试打印geneset中每个元素的类型。你知道吗
相关问题 更多 >
编程相关推荐