python - 如何在 Python 中声明单个数组值?

标签 python xmpp

我目前正在玩弄 python-jabberbot并且无法创建发送随机句子的简单方法。我不精通python所以我想知道我哪里出错了。我有一种感觉,我声明数组的方式是我的垮台:

def whatdoyouknow(self, mess, args):
        """random response"""   
        string[0] = 'this is a longish sentence about things'
        string[1] = 'this is a longish sentence about things number 2'
        string[2] = 'this is a longish sentence about things number 3'

        i = random.randint(0, 2)
        return string[i]

最佳答案

通过将元素放在方括号中来定义列表文字:

string = ['this is a longish sentence about things',
          'this is a longish sentence about things number 2',
          'this is a longish sentence about things number 3']

或者,您可以通过定义一个空列表然后附加元素来构建列表:

string = []
string.append('this is a longish sentence about things')
string.append('this is a longish sentence about things number 2')
string.append('this is a longish sentence about things number 3')

我强烈建议您阅读 Python tutorial在继续之前,它解释了构建 Python 类型以及如何为您操作它们。

关于python - 如何在 Python 中声明单个数组值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14563071/

相关文章:

ios - 如何使用 xmpp openfire 发送打字通知

Python-LDAP 集成

python - 变色龙中的断线

python - pandas - 根据仅日期提供的 maxtimestamp 在其他列中查找值

Python:如何动态设置函数闭包环境

iphone - 使用 google 访问 token 进行 Xmpp gtalk 身份验证

java - Android XMPP Smack 在应用程序关闭时接收消息

python - 根据另一列更改 pandas 列

java - 无法向 openfire 服务器发送消息

xmpp - 用户可以使用多少资源登录符合标准的 XMPP 服务器?