String 如何从嵌套列表中删除特定长度的字符串?
我有一个嵌套的字符串列表,语料库由不同长度的列表组成。我只想保留长度大于2的字符串 从中的类似问题中,我尝试了所有允许我指出条件长度>2的答案 代码 这是当前尝试的输出,与前两次尝试相同 初始语料库此处String 如何从嵌套列表中删除特定长度的字符串?,string,python-3.x,nested,nested-lists,String,Python 3.x,Nested,Nested Lists,我有一个嵌套的字符串列表,语料库由不同长度的列表组成。我只想保留长度大于2的字符串 从中的类似问题中,我尝试了所有允许我指出条件长度>2的答案 代码 这是当前尝试的输出,与前两次尝试相同 初始语料库此处 [['extracting', 'opinions'], ['soo', 'min', 'kim', 'and'], ['abstract'], ['this', 'paper', 'presents', 'method', 'for', 'identifying', 'an'], ['thi
[['extracting', 'opinions'],
['soo', 'min', 'kim', 'and'],
['abstract'],
['this', 'paper', 'presents', 'method', 'for', 'identifying', 'an'],
['this', 'section', 'reviews', 'previous', 'works', 'in'],
['subjectivity', 'detection', 'is'],
['work', 'is', 'similar', 'to', 'ours', 'but', 'different']]
语料库包含长度为2或更小的任何字符串
[['extracting', 'opinions'],
['soo', 'min', 'kim', 'and'],
['abstract'],
['this', 'paper', 'presents', 'method', 'for', 'identifying', 'an'],
['this', 'section', 'reviews', 'previous', 'works', 'in'],
['subjectivity', 'detection', 'is'],
['work', 'is', 'similar', 'to', 'ours', 'but', 'different']]
我需要什么
使用第二个版本的语料库而不使用任何长度为2或更小的字符串的最快方法:
语料库没有任何长度为2或更小的字符串
[['extracting', 'opinions'],
['soo', 'min', 'kim', 'and'],
['abstract'],
['this', 'paper', 'presents', 'method', 'for', 'identifying'],
['this', 'section', 'reviews', 'previous', 'works'],
['subjectivity', 'detection'],
['work','similar','ours', 'but', 'different']]
谢谢。@Vera,您可以尝试下面的代码。它使用诸如列表理解,lambda函数,映射(),过滤器等概念 使用列表理解,lambda函数,映射(),过滤器(),减少()等是一种以更简单、高效和简洁的方式解决问题的Pythonic方法 您可以检查和等,查看与这些概念相关的给定示例并进行解释
这仅仅是字符串的2D列表吗?通过嵌套进行迭代并检查长度,如果长度小于2,则删除项。谢谢,这样可以工作,但不是更快的方法吗?真正的语料库有数百万个单词。对于语料库中的句子:对于句子中的x:如果len(x)@Vera,使用map()、filter()、lambda函数、列表理解来解决。它不使用关键字和if,你的代码看起来更专业。为什么
lambda
+map
+过滤器比列表理解快?你有没有对性能进行计时以检查它是否真的更快?@jpp,是的,我检查了时间[[subelt for subelt in elt if len(subelt)>2]for elt in corpus]
是其他解决方案中速度最快的,这应该是因为使用map()、filter()、lambda函数等只会增加很少的时间。当我测试时,100万次的语料库又花了3秒钟,也就是说,corpus*1000000
。当然,+1,它确实更快。这就是我需要的。谢谢顺便说一句,这个可以关闭。
[['extracting', 'opinions'],
['soo', 'min', 'kim', 'and'],
['abstract'],
['this', 'paper', 'presents', 'method', 'for', 'identifying'],
['this', 'section', 'reviews', 'previous', 'works'],
['subjectivity', 'detection'],
['work','similar','ours', 'but', 'different']]
import json
corpus = [['extracting', 'opinions'],
['soo', 'min', 'kim', 'and'],
['abstract'],
['this', 'paper', 'presents', 'method', 'for', 'identifying', 'an'],
['this', 'section', 'reviews', 'previous', 'works', 'in'],
['subjectivity', 'detection', 'is'],
['work', 'is', 'similar', 'to', 'ours', 'but', 'different']]
new_corpus = list( map(lambda words: list(filter(lambda word: len(word)> 2, words)), corpus))
# Pretty printing list of lists of words of length > 2
print(json.dumps(new_corpus, indent=2))
"""
[
[
"extracting",
"opinions"
],
[
"soo",
"min",
"kim",
"and"
],
[
"abstract"
],
[
"this",
"paper",
"presents",
"method",
"for",
"identifying"
],
[
"this",
"section",
"reviews",
"previous",
"works"
],
[
"subjectivity",
"detection"
],
[
"work",
"similar",
"ours",
"but",
"different"
]
]
"""