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

发布于 2025-01-09 10:57:44 字数 1468 浏览 1 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

绝情姑娘 2025-01-16 10:57:44
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 是标点符号的选项卡列表。

这对你有用吗?

输出 :

['西奥多', '冯', '卡门', '讲座', '科学', '旅程:',
''','起源','的','光合作用:','a','窗口','进入',
'那个'、'错综复杂'、'关系'、'之间'、'生活'、'和'、'那个'、
'世界','周围','它'','科学','旅程:','“寻址”,
“能源”、“存储”、“挑战:”、“制造”、“电解质”、“那个”、
'可以'、'帮助'、'电池'、'存储'、'更多'、'能源''、'科学'、
'旅程:','“缺陷”,'在','晶体:','这个','最小的',
'工程', '工具'']

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)

I assumes punc is tab list of punctuation.

Is this working for you ?

output :

['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"']

早茶月光 2025-01-16 10:57:44

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

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

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

Let's look at those 3 statements, each executed on each iteration of the loop:

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

So for every word, you are creating a new list, putting that word into it and then printing it. Instead you want to create a new list before the loop, put all the words you want and then print all of them after the loop is finished.

等数载,海棠开 2025-01-16 10:57:44

如果您追加也没关系,因为上面的行将 lsit 擦除干净。像 c_list = [] 一样摆脱它,并将其放在主循环之前,然后将 print(c_list) 放在主 for 循环之外和之后,看看是否有效。

Doesnt matter if you append since the line above wipes the lsit clean. Get rid of this like c_list = [] and put it before the main loop, and put the print(c_list) outside and after the main for loop and see if that works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文