Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/hadoop/6.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_Set - Fatal编程技术网

Python 将字符串转换为集合时出现编译时错误?

Python 将字符串转换为集合时出现编译时错误?,python,set,Python,Set,我在上一个断言语句中出错,但我不知道为什么请帮助我 def test_set_creation(): """ sets can be created from any sequence like list or a tuple. """ test_list = [1, 2, 1, 3] set1 = set(test_list) assert {1,2,3} == set1 test_string = "apple" set2 =

我在上一个断言语句中出错,但我不知道为什么请帮助我

def test_set_creation():
    """
    sets can be created from any sequence like list or a tuple.
    """
    test_list = [1, 2, 1, 3]
    set1 = set(test_list)
    assert {1,2,3} == set1

    test_string = "apple"
    set2 = set(test_string)
    assert {} == set2
{}创建空字典,而不是空集。你的断言总是失败的

使用set创建一个空的set对象,您可能应该测试set2是否为空:

从:

集合显示用大括号表示,与字典显示不同的是,集合显示缺少分隔键和值的冒号

[……]

不能用{}构造空集;此文本构造一个空字典


您正在断言字典和集合,这在最后一行中是错误的

>>> type({})
<type 'dict'>
>>> type(set())
<type 'set'>
>>>

这里没有“编译时错误”。您最多只能得到一个断言异常。在assert的位置是否有任何条件set2@TirupatiRao:你希望在这里断言什么?你在问题中没有提到这一点;代码应该做什么。
>>> type({})
<type 'dict'>
>>> type(set())
<type 'set'>
>>>