如何将所有输出列表合并为一个列表

How to merge all of the output lists into one list

我正在为我的项目尝试以下代码片段:

scrap=['Theodore von Kármán Lecture', 'Science Journey: "The Origin of Photosynthesis: A Window into the Intricate Relationship Between Life and the World Around It"', 'Science Journey: "Addressing Energy Storage Challenges: Making Electrolytes That Can Help Batteries Store More Energy"', 'Science Journey: "Defects in Crystals: The Tiniest Engineering Tool"']

for i in scrap:
    words_in_scrap = i.split() 
    # print("words_in_scrap=",words_in_scrap) 
    for j in words_in_scrap:
      words = j.lower()
      # print(words)
      for k in words: 
        # print(k)
        if k in punc:
          words = words.replace(k,"")
          # print(words)
      # print(k)
      clean = words      #corrected lot
      # print(clean)
      c_list = []
      c_list.append(clean)
      print(c_list)

我得到了这个输出:

['theodore']
['von']
['kármán']
['lecture']
['science']
['journey']
['the']
['origin']
['of']
['photosynthesis']
['a']
['window']
['into']
['the']
['intricate']
['relationship']
['between']
['life']
['and']
['the']
['world']
['around']
['it']
['science']
['journey']
['addressing']
['energy']
['storage']
['challenges']
['making']
['electrolytes']
['that']
['can']
['help']
['batteries']
['store']
['more']
['energy']
['science']
['journey']
['defects']
['in']
['crystals']
['the']
['tiniest']
['engineering']
['tool']

我希望将所有这些输出列表放入一个列表中,例如:

['theodore','von','karman','science',.............]

我知道这些单独的输出是由于循环而产生的,我尝试为此执行 .append() 并以 c_list 结束,结果它们是独立的列表。对此的任何解决方案都将受到高度重视和赞赏。谢谢!

如果你追加没关系,因为上面的行把 lsit 擦干净了。像 c_list = [] 一样去掉这个,把它放在主循环之前,把 print(c_list) 放在主 for 循环之外和之后,看看是否可行。

让我们看看这 3 个语句,每个语句都在循环的每次迭代中执行:

c_list = []
c_list.append(clean)
print(c_list)

因此,对于每个单词,您都在创建一个新列表,将单词放入其中,然后打印出来。相反,您想在循环之前创建一个新列表,放入您想要的所有单词,然后在循环完成后打印所有这些单词。

scrap=['Theodore von Kármán Lecture', 'Science Journey: "The Origin of Photosynthesis: A Window into the Intricate Relationship Between Life and the World Around It"', 'Science Journey: "Addressing Energy Storage Challenges: Making Electrolytes That Can Help Batteries Store More Energy"', 'Science Journey: "Defects in Crystals: The Tiniest Engineering Tool"']
punc = [".","/",":"]
c_list = []
for i in scrap:
    words_in_scrap = i.split() 
    # print("words_in_scrap=",words_in_scrap) 
    for j in words_in_scrap:
      words = j.lower()
      # print(words)
      for k in words: 
        # print(k)
        if k in punc:
          words = words.replace(k,"")
          # print(words)
      # print(k)
      clean = words      #corrected lot
      # print(clean)

      c_list.append(clean)

print(c_list)

我假设 punc 是标点符号列表。

这适合你吗?

输出:

['theodore', 'von', 'kármán', 'lecture', 'science', 'journey:', '"the', 'origin', 'of', 'photosynthesis:', 'a', 'window', 'into', 'the', 'intricate', 'relationship', 'between', 'life', 'and', 'the', 'world', 'around', 'it"', 'science', 'journey:', '"addressing', 'energy', 'storage', 'challenges:', 'making', 'electrolytes', 'that', 'can', 'help', 'batteries', 'store', 'more', 'energy"', 'science', 'journey:', '"defects', 'in', 'crystals:', 'the', 'tiniest', 'engineering', 'tool"']