为什么python在将它们添加到集合时会拆分字符串?

时间:2015-09-16 22:02:47

标签: python

这是我的方法:

def edge(self, f, a, b, s):
        if not (
         isinstance(f, str) and isinstance(a, str)
         and isinstance(b, str) and isinstance(s, str)):
            raise TypeError('ERROR: Edge requires string arguments.')

        elif f in self.A:
             raise ValueError('ERROR: Edge name already taken.')

        elif not trie_methods.is_gen(s):
            raise ValueError('ERROR: Edge requires a dna char for symbol.')

        else:
            self.A.add(f)
            self.dom[f] = a
            self.cod[f] = b
            self.sym[f] = s

这是测试和输出:

class test_method_edge(unittest.TestCase):
...
    def auto_name(self):
        trie = graph.Graph()
        trie.node('A')
        trie.node('B')
        f = trie_methods.name()
        trie.edge(f, 'A', 'B', 't')
        self.assertIn(f, trie.A)
        self.assertTrue(trie.dom[f] =='A')
        self.assertTrue(trie.cod[f] == 'B')
        self.assertTrue(trie.sym[f] == 't')

FAIL: test_auto (__main__.test_method_node)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test_graph.py", line 40, in test_auto
    self.assertIn(n, trie.O)
AssertionError: '82Q3C1' not found in {'3', '2', '8', 'Q', '1', 'C'}

----------------------------------------------------------------------
Ran 6 tests in 0.002s

FAILED (failures=1)

我已经尝试过更新'并且'添加'。我相信字符串是可以清除的,并且集合只能使用可清除的对象。是否对集合的元素有另一个限制?

1 个答案:

答案 0 :(得分:4)

Strings are iterablevoid "first call to index doesn't work."() { given: "My setup ran" when: "I call index" request.method="GET" response.format="xml" controller.index(); then: "I get an XML object back with 3 books" println response.contentAsString response.xml.book*.title.size()==3 } void "2nd call to index works"() { given: "My setup ran" when: "I call index" request.method="GET" response.format="xml" controller.index(); then: "I get an XML object back with 3 books" println response.contentAsString response.xml.book*.title.size()==3 } accepts an iterable作为参数。

set而言,您将其传递给您想要表示为set的某种类型的集合。

解决这个问题的方法是将您传入的内容换成元组或列表。它将使用包装元组或列表作为迭代,并将其放入集合而不是每个单独的字母。

set