将空列表附加到python中的空列表

问题描述 投票:-3回答:1
channel = []
user = []

//让我们在频道列表中添加新的频道名称,如何做到这一点

channel = [document[],help[],more[]]

现在在文档中附加消息,然后附加用户A,B;

user.append("A")
user.append("B")

现在附加为 -

channel = [document[{'A':'hello','B':'hi','A':'are you at work','B':'no'},help[],more[]]

但这不可能是'A':'你好'会丢失。那么用什么?我没有使用数据库,所以我必须在python数据结构中存储信息。

python
1个回答
1
投票

(1)这在python中无效:

channel = [document[],help[],more[]]

你可以这样做:

channel = {'document':[], 'help':[], 'more':[]}

(2)你不能在python字典中有重复的键。所以你被迫使用不同的结构。最接近你想要的是一个元组列表:

channel = {'document':[('A','hello'), ('B','hi'), ('A','are you at work'), ('B','no')], 'help':[], 'more':[]}

您可以像这样构建文档列表:

channel['document'].append(('A', 'hello'))

并读出这样的文件:

for user, message in channel["document"]:
    print(user, message)
© www.soinside.com 2019 - 2024. All rights reserved.