gpt4 book ai didi

python - 在 Python 中将列表连接到字符串时出错

转载 作者:行者123 更新时间:2023-12-01 05:14:25 25 4
gpt4 key购买 nike

我正在尝试将类元素传递给方法。元素是动态形成的,在其中插入当前时间。我的类看起来像这样:

class MineContact(dict):
def __init__(self, **kwargs):
# set your default values
import time
curr_time = repr(time.time()).replace('.', '')
self['givenName'] = ['name%s' % curr_time[10:]]
...

因此,我创建了此类的对象,现在我想将其作为方法参数插入:

    contact = MineContact()
extra_text = "-%d" % (self.iteration)
new_contact.insert_given_name(contact.givenName + extra_text)

当我运行此脚本时,出现此类错误:

TypeError: can only concatenate list (not "str") to list

那么,有人知道我哪里错了吗?

最佳答案

givenName 似乎是一个列表。您可以附加另一个列表,如下所示:

new_contact.insert_given_name(contact.givenName + [extra_text])

关于python - 在 Python 中将列表连接到字符串时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23512160/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com