Python 类型错误:Can';t转换为';列表';对象隐式地访问str

Python 类型错误:Can';t转换为';列表';对象隐式地访问str,python,python-3.x,Python,Python 3.x,我有python类: class Athlete: def __init__(self,fullName,dob,times): self.fullName = fullName self.dob = dob self.times = times ## print('times is ',times) def __str__(self): return ''.join("Athlete[fullName=

我有python类:

class Athlete:
    def __init__(self,fullName,dob,times):
        self.fullName = fullName
        self.dob = dob
        self.times = times
    ##  print('times is ',times)

    def __str__(self):
        return ''.join("Athlete[fullName="+self.fullName +",dob="+self.dob+",sortedTimes="+self.sortedTimes+"]")

    def __repr__(self):
        return self.__str__()
此类的实例作为值存储在map
athleteMap

当我执行
打印(athleteMap)
时,我收到以下错误:

File "D:/Software/ws/python_ws/collections\athleteList.py", line 11, in __str__
    return ''.join("Athlete[fullName="+self.fullName +",dob="+self.dob+",sortedTimes="+self.sortedTimes+"]")
TypeError: Can't convert 'list' object to str implicitly
我需要在print方法中打印
atternate
实例


如何在python中执行此操作?

时间
显式转换为字符串,然后:

return "Athlete[fullName=" + self.fullName  + ",dob=" + self.dob + ",sortedTimes=" + str(self.sortedTimes) + ']'
您不需要
'.join()
在这里

更好的选择是使用字符串格式:

return "Athlete[fullName={0.fullName},dob={0.dob},sortedTimes={0.sortedTimes}]".format(self)

时间
显式转换为字符串,然后:

return "Athlete[fullName=" + self.fullName  + ",dob=" + self.dob + ",sortedTimes=" + str(self.sortedTimes) + ']'
您不需要
'.join()
在这里

更好的选择是使用字符串格式:

return "Athlete[fullName={0.fullName},dob={0.dob},sortedTimes={0.sortedTimes}]".format(self)

时间
显式转换为字符串,然后:

return "Athlete[fullName=" + self.fullName  + ",dob=" + self.dob + ",sortedTimes=" + str(self.sortedTimes) + ']'
您不需要
'.join()
在这里

更好的选择是使用字符串格式:

return "Athlete[fullName={0.fullName},dob={0.dob},sortedTimes={0.sortedTimes}]".format(self)

时间
显式转换为字符串,然后:

return "Athlete[fullName=" + self.fullName  + ",dob=" + self.dob + ",sortedTimes=" + str(self.sortedTimes) + ']'
您不需要
'.join()
在这里

更好的选择是使用字符串格式:

return "Athlete[fullName={0.fullName},dob={0.dob},sortedTimes={0.sortedTimes}]".format(self)

您的
join
呼叫没有意义。你可能想要这样的东西:

def __str__(self):
    return "Athlete[fullName="
        + str(self.fullName)
        + ",dob="
        + str(self.dob)
        + ",sortedTimes="
        + str(self.sortedTimes)
        + "]"

我在每个属性中都添加了
str
,因为我无法确定在其中哪一个属性中放置了
列表。问题从错误中显而易见-列表不能隐式转换为字符串-您需要通过
str()
调用显式标记此转换。你的一个属性(
dob
时间
最有可能)是一个列表。

你的
join
呼叫没有意义。你可能想要这样的东西:

def __str__(self):
    return "Athlete[fullName="
        + str(self.fullName)
        + ",dob="
        + str(self.dob)
        + ",sortedTimes="
        + str(self.sortedTimes)
        + "]"

我在每个属性中都添加了
str
,因为我无法确定在其中哪一个属性中放置了
列表。问题从错误中显而易见-列表不能隐式转换为字符串-您需要通过
str()
调用显式标记此转换。你的一个属性(
dob
时间
最有可能)是一个列表。

你的
join
呼叫没有意义。你可能想要这样的东西:

def __str__(self):
    return "Athlete[fullName="
        + str(self.fullName)
        + ",dob="
        + str(self.dob)
        + ",sortedTimes="
        + str(self.sortedTimes)
        + "]"

我在每个属性中都添加了
str
,因为我无法确定在其中哪一个属性中放置了
列表。问题从错误中显而易见-列表不能隐式转换为字符串-您需要通过
str()
调用显式标记此转换。你的一个属性(
dob
时间
最有可能)是一个列表。

你的
join
呼叫没有意义。你可能想要这样的东西:

def __str__(self):
    return "Athlete[fullName="
        + str(self.fullName)
        + ",dob="
        + str(self.dob)
        + ",sortedTimes="
        + str(self.sortedTimes)
        + "]"

我在每个属性中都添加了
str
,因为我无法确定在其中哪一个属性中放置了
列表。问题从错误中显而易见-列表不能隐式转换为字符串-您需要通过
str()
调用显式标记此转换。你的属性之一(
dob
times
最有可能)是一个列表。

试着在
self.sortedTimes
上调用
str
(或者更好的是,使用
str.format
)和
self.sortedTimes
一起,你的意思是
self.times
,对吧?试着在
self.sortedTimes
上调用
str
(或者更好地使用
str.format
)和
self.sortedTimes
你的意思是
self.times
,对吧?试着在
self.sortedTimes
上调用
str
(或者更好地使用
str.format
)使用
self.sortedTimes
你的意思是
self.times
,对吗?试着在
self.sortedTimes
上调用
str
(或者更好的是,使用
str.format
)和
self.sortedTimes
你的意思是
self.times
,对吗?