在python中将空列表追加到空列表
// 让我们将新频道名称添加到频道列表中,如何操作在python中将空列表追加到空列表,python,Python,// 让我们将新频道名称添加到频道列表中,如何操作 channel = [] user = [] 现在在文档中附加消息作为- 让我们附加用户A,B channel = [document[],help[],more[]] 现在附加为- user.append("A") user.append("B") 但这是不可能的‘A’:‘hello’将丢失。 那么使用什么呢? 我没有在flask中使用数据库,所以我必须在python数据结构中存储信息 (1)这在python中无效: channel =
channel = []
user = []
现在在文档中附加消息作为-
让我们附加用户A,B
channel = [document[],help[],more[]]
现在附加为-
user.append("A")
user.append("B")
但这是不可能的‘A’:‘hello’将丢失。
那么使用什么呢?
我没有在flask中使用数据库,所以我必须在python数据结构中存储信息 (1)这在python中无效:
channel = [document[{'A':'hello','B':'hi','A':'are you at work','B':'no'},help[],more[]]
您可以这样做:
channel = [document[],help[],more[]]
(2) python字典中不能有重复的键。因此,您必须使用不同的结构。最接近您想要的是元组列表:
channel = {'document':[], 'help':[], 'more':[]}
您可以按如下方式建立文档列表:
channel = {'document':[('A','hello'), ('B','hi'), ('A','are you at work'), ('B','no')], 'help':[], 'more':[]}
channel['document'].append(('A', 'hello'))
并从文件中读出如下内容:
channel = {'document':[('A','hello'), ('B','hi'), ('A','are you at work'), ('B','no')], 'help':[], 'more':[]}
channel['document'].append(('A', 'hello'))
文档[]
应该是什么意思?我很确定这是一个语法错误,如果不是,任何像普通集合一样的行为都可能引发TypeError
,列表的名称没有其他内容