Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/348.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_String_Set_Quote - Fatal编程技术网

将字符串添加到不带引号的集合(Python)

将字符串添加到不带引号的集合(Python),python,string,set,quote,Python,String,Set,Quote,所以我有两个班,一个是学生班,另一个是单班。我想让UniClass的str方法给出一个集合的字符串,该集合包含由Student的str方法给出的多个字符串。 我为此编写的代码是: def __str__(self): """Gives back a String in the form 'name [imt_name] in Semester semester'""" return '{} [{}] in Semester {}'.format(self.name

所以我有两个班,一个是学生班,另一个是单班。我想让UniClass的str方法给出一个集合的字符串,该集合包含由Student的str方法给出的多个字符串。 我为此编写的代码是:

def __str__(self):
        """Gives back a String in the form 'name [imt_name] in Semester semester'"""
        return '{} [{}] in Semester {}'.format(self.name, self.imt_name, self.semester)
为学生和班级

def __str__(self):
        """without a student object, "set()" is returned, with one or multiple student objects, a string containing of the strings from the student class is returned"""
        if self._students == set():
            return "set()"
        else:
            rückgabe = set()
            for student in self._students:
                rückgabe.add(str(student))
            return str(rückgabe)
如果我调用UniClass的对象,为了得到字符串

例如,如果我有一个student对象,带有
name=“David”
imt\u name=“D3123”
sterm=3
,student的
\uu str\uuuu
方法应该返回字符串
“David[D3123]在第3学期”
,而UniClass应该返回字符串
“{David D3123]在第3学期”
。如果一个单类对象中有多个学生对象,则应以逗号分隔的形式返回

虽然一切正常,但我唯一的问题是UniClass类返回的字符串的形式是
{'David[D3123]在第2学期','Jonas[J312344]在第3学期'}
,这是不好的,因为字符串将被测试,具有上段不带引号的形式


有没有一种方法可以在不使用strip方法的情况下让字符串在集合中表示为不带引号的字符串(因为我已经尝试过了,而且字符串根本没有改变)?

返回','。join(rückgabe)
工作吗?那么您只需要在格式字符串中添加额外的
{}
?就行了,非常感谢!然而,我不认为这项任务需要通过这种方式来解决。下周我会去问我的导师:)我的意思是。。。表面上。但同时-你的其他选择是使用你的集合中的
\uuuu repr\uuuuuu
\uuu str\uuuuu
方法,我怀疑这是一个好方法。