Skip to main content
 首页 » 编程设计

python 使用 join() 合并列表

2025年05月04日34xing901022

我有以下列表:

lst = [['a','!b'], ['91','q'], ['!t','3','p']] 

我想要以下内容作为输出:

test = a and !b or 91 and q or !t and 3 and p 

子列表中的各个文字被制成单个文字并使用“和”连接,然后子列表使用“或”连接在一起

我尝试了以下方法,有人可以更正我的代码吗?

   def output(self): 
      temp = ['and'.join(sublist[i]) for i in sublist] 
      test = ['or'.join(sublist) for sublist in self.lst] 
      return self.test 

请您参考如下方法:

一次全部:

def output(lst): 
    return " or ".join(" and ".join(s) for s in lst)