[英]Fixing a Dictionary Comprehension for a Nested For Loop
我正在學習理解,即列表理解和字典理解。 我想使用更多的理解來擴展我的代碼。 現在,我正在嘗試將這種方法轉換為“解析句子”,這是我作為 web 刮擦 class 的一部分編寫的,我想將其重寫為字典理解。
def parse_sentences(text_str):
"""
:param text_str: list of text that needs to be divided by periods
at end of sentence.
:return: list of the individual sentences for every element
of the list
"""
# Define list to hold the individual sentences.
split_text_strings = []
# Loop through each element of the text.
for i in text_str:
split_i = i.split('.')
for j in split_i:
last = len(split_i) - 1
if split_i[last] == '':
split_i.pop(last)
split_text_strings.append(j)
return split_text_strings
基本上,它采用 ['This is a sentence. 這是一個偉大的句子。','你沒有聽說過這句話嗎? 可能是我讀過的最好的句子。'] 並返回另一個列表 split_text_strings,其中每個元素都有一個句子,就像這樣。 ['這是一個句子','這是一個偉大的句子','你沒聽說過這句話','可能是我讀過的最好的句子']。 這是我嘗試將其轉換為字典理解
def parse_sentences(text_str):
"""
:param text_str: list of text that needs to be divided by periods
at end of sentence.
:return: list of the individual sentences for every element
of the list
"""
split_text_strings = {(i, j): i.split('.') for i in text_str for j in text_str[text_str.index(i)]}
return split_text_strings
我知道 '(i, j): i.split('.') for i in text_str' 是外循環,另一個 for 循環是內循環。 但我不知道我做錯了什么。
什么不起作用? (1) 第一個 function 返回一個列表,而第二個 function 返回一個字典。 注意 function 的描述也是字典。 (2) 重復句子或旁邊重復句子存在潛在問題。 在第一種情況下,輸入['I love StackOverflow.', 'I love StackOverflow']
將被處理為['I love StackOverflow', 'I love StackOverflow']
,但可能會導致字典理解的鍵重復。 這會導致數據丟失。 (3) (i, j): i.split('.') for i in text_str for j in text_str[text_str.index(i)]
執行以下操作。 說, text_str = ['b.c']
。 然后, i='b.c'
。 j
首先迭代 'b.c',然后j
取值'b', '.', 'c'
。 那么,結果是
{
('b.c', 'b'): ['b', 'c'],
('b.c', '.'): ['b', 'c'],
('b.c', 'c'): ['b', 'c']
}
這不是你想要的。
你想要做什么我認為你想要的關鍵如下: text_str
中字符串的序號(應該稱為text_arr
,順便說一句)和 substring 中的句子序號。 例如, ['a...', 'b.c']
將表示為
{
(0, 0): 'a',
(1, 0): 'b',
(1, 1): 'c'
}
在這種情況下,我們需要確定字符串的順序。 外循環是
(i, ?): ?? for i, string in enumerate(text_str)
我們馬上填寫問號。 我們需要解析每個字符串,因此我們需要在所有句子中考慮string.split('.')
和 go。 這給了我們以下答案
(i, j): sentence for i, string in enumerate(text_str) for j, sentence in enumerate(string.split('.'))
最后,我們需要過濾掉空字符串,如下所示:
(i, j): sentence for i, string in enumerate(text_str) for j, sentence in enumerate(string.split('.')) if sentence
當代碼清楚地說兩次列表時,不確定為什么要說字典。 無論如何,您總是可以使用列表推導。 for elem in text_str
是外循環, for segment in elem.split('.')
是內循環。 如果有刪除重復項等額外要求,可以考慮使用set
。
split_text_strings = [segment for elem in text_str for segment in elem.split('.')]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.