【问题标题】:Python - iterating through list and dictionary to get a nested list outputPython - 遍历列表和字典以获取嵌套列表输出
【发布时间】:2020-07-01 21:47:27
【问题描述】:

我有一个字典mydict,其中包含一些文件名作为键和其中的文本作为值。

我正在从每个文件的文本中提取单词列表。单词存储在列表中mywords

我已经尝试了以下方法。

mydict = {'File1': 'some text. \n Foo extract this. \n Bar extract this', 
'File2': 'more text. \n Bar extract this too.'}
mywords = ['Foo', 'Bar']
mylist= []
for k,v in mydict.items():
        for word in mywords:
            extracted = (re.findall('^ ' + word + ".*", v, flags=re.IGNORECASE|re.MULTILINE))
            mylist.append(extracted[:1])

这给了我

[[' Foo extract this. '],
 [' Bar extract this'],
 [],
 [' Bar extract this too.']]

但是,我希望输出有 2 个嵌套列表(对于每个文件),而不是每次在文件中搜索单词时都有一个单独的列表。

期望的输出:

[[' Foo extract this. '], [' Bar extract this']],
 [[], [' Bar extract this too.']]

【问题讨论】:

  • 为什么你需要每个只有 1 项的列表?

标签: python list loops dictionary for-loop


【解决方案1】:

您可能想尝试制作子列表并将它们附加到您的列表中。这是一个可能的解决方案:

mydict = {'File1': 'some text. \n Foo extract this. \n Bar extract this', 
'File2': 'more text. \n Bar extract this too.'}
mywords = ['Foo', 'Bar']
mylist= []
for k,v in mydict.items():
    sublist = []
    for word in mywords:
        extracted = (re.findall('^ ' + word + ".*", v, flags=re.IGNORECASE|re.MULTILINE))
        sublist.append(extracted[:1])
    mylist.append(sublist)

这个输出:[[[' Foo extract this. '], [' Bar extract this']], [[], [' Bar extract this too.']]]


如果您想要没有周围列表的字符串,请仅在有结果时插入第一个结果:

import re

mydict = {'File1': 'some text. \n Foo extract this. \n Bar extract this', 
'File2': 'more text. \n Bar extract this too.'}
mywords = ['Foo', 'Bar']
mylist= []
for k,v in mydict.items():
    sublist = []
    for word in mywords:
        extracted = (re.findall('^ ' + word + ".*", v, flags=re.IGNORECASE|re.MULTILINE))
        if extracted: # Checks if there is at least one element in the list
            sublist.append(extracted[0])
    mylist.append(sublist)

这个输出:[[' Foo extract this. ', ' Bar extract this'], [' Bar extract this too.']]


如果您希望能够从每个文件中获取多个结果,您可以执行以下操作(注意我在第二个文件中为Foo 放置了另一个匹配项:

import re

mydict = {'File1': 'some text. \n Foo extract this. \n Bar extract this', 
'File2': 'more text. \n Bar extract this too. \n Bar extract this one as well'}
mywords = ['Foo', 'Bar']
mylist= []
for k,v in mydict.items():
    sublist = []
    for word in mywords:
        extracted = (re.findall('^ ' + word + ".*", v, flags=re.IGNORECASE|re.MULTILINE))
        if extracted:
            sublist += extracted
    mylist.append(sublist)

这个输出:[[' Foo extract this. ', ' Bar extract this'], [' Bar extract this too. ', ' Bar extract this one as well']]

【讨论】:

  • 有没有办法把所有的['values']改成'values'
猜你喜欢
  • 2015-12-06
  • 2021-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-10
  • 1970-01-01
  • 2019-03-22
相关资源
最近更新 更多