有没有更好的方法迭代多个列表并附加结果?

2024-06-02 06:26:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在计算给定文件中某些单词(在“字典”中)的出现次数。你知道吗

虽然我下面的代码工作得非常好,但这对眼睛来说是痛苦的,而且几乎可以肯定是对Python的禅宗的一种尴尬。你知道吗

我真的很感激任何提示,我可以如何使“歌利亚循环”更干净,更有效。你知道吗

每个分数必须有自己唯一的计数器,每个字典必须有自己唯一的名称。这让我排除了在某种范围内循环的可能性。 完整背景 我有大约14万个文本块和9个“字典”,每个字典的总字数各不相同。对于每个文件,我清理文本,然后计算给定文本文件中与9个字典中的每个单词匹配的单词数。你知道吗

for file in all_files:
    # Extract firm and year identifiers from file names
    cik_identifier = file[70:-4].split('_')[0]
    financial_year = file[70:-4].split('_')[1]
    filing_year = file[70:-4].split('_')[2]
    filing_type = '10K'

    # Conduct final cleaning of text file
    with open(file) as my_file:
        text = my_file.read()
        words = text.split()
        lower_case_words = [word.lower() for word in words]
        alphabetic_only = [word for word in lower_case_words if word.isalpha()]
        cleaned_words = \
            [word for word in alphabetic_only if word not in stop_words]

    # Log length of text doc pre and post clean
    num_words_pre_clean = len(lower_case_words)
    num_words_post_clean = len(cleaned_words)

    # Calculate Sentiment Scores
    first_sentiment_score = 0
    second_sentiment_score = 0
    third_sentiment_score = 0
    fourth_sentiment_score = 0
    fifth_sentiment_score = 0
    sixth_sentiment_score = 0
    seventh_sentiment_score = 0
    eighth_sentiment_score = 0
    ninth_sentiment_score = 0

    # Goliath loop begins
    for word in cleaned_words:
        for first_sentiment_word, second_sentiment_word, third_sentiment_word, \
            fourth_sentiment_word, fifth_sentiment_word, sixth_sentiment_word, \
            seventh_sentiment_word, eighth_sentiment_word, ninth_sentiment_word in itertools.zip_longest(dict_first, dict_second,
                                                   dict_third, dict_fourth,
                                                   dict_fifth, dict_sixth,
                                                   dict_seventh, dict_eighth, dict_ninth):
                if first_sentiment_word == word:
                    first_sentiment_score += 1
                elif second_sentiment_word == word:
                    second_sentiment_score += 1
                elif third_sentiment_word == word:
                    third_sentiment_score += 1
                elif fourth_sentiment_word == word:
                    fourth_sentiment_score += 1
                elif fifth_sentiment_word == word:
                    fifth_sentiment_score += 1
                elif sixth_sentiment_word == word:
                    sixth_sentiment_score += 1
                elif seventh_sentiment_word == word:
                    seventh_sentiment_score += 1
                elif eighth_sentiment_word == word:
                    eighth_sentiment_score += 1
                elif ninth_sentiment_word == word:
                    ninth_sentiment_score += 1


    # Append identifier, num words, and trust score to df
    sentiment_analysis_data = {'cik' : cik_identifier,
                           'financial_year_end' : financial_year,
                           'filing_year_end' : filing_year,
                           'filing_type' : filing_type,
                           'num_words_pre_clean' : num_words_pre_clean,
                           'num_words_post_clean' : num_words_post_cean,
                           'first_sentiment_score' : first_sentiment_score,
                           'second_sentiment_score' : second_sentiment_score,
                           'third_sentiment_score' : third_sentiment_score,
                           'fourth_sentiment_score' : fourth_sentiment_score,
                           'fifth_sentiment_score' : fifth_sentiment_score,
                           'sixth_sentiment_score' : sixth_sentiment_score,
                           'seventh_sentiment_score' : seventh_sentiment_score,
                           'eighth_sentiment_score' : eighth_sentiment_score,
                           'ninth_sentiment_score' : ninth_sentiment_score}

    all_scores.append(sentiment_analysis_data)

Tags: inyearnumdictwordfilefirstscore
1条回答
网友
1楼 · 发布于 2024-06-02 06:26:46

计数器列表仍然是一组唯一的计数器。你知道吗

sentiment_scores = [0] * 9

而一个字典列表仍然是一组独特的字典。你知道吗

dicts = [dict_one, dict_two, ...]  # etc

现在,您可以用一种不太可能使您失明的方式编写循环。你知道吗

# Goliath loop begins
for word in cleaned_words:
    for sentiment_words in itertools.zip_longest(*dicts):
        for i, sentiment_word in enumerate(sentinment_words):
            if sentiment_word == word:
                sentiment_score[i] += 1


# Append identifier, num words, and trust score to df
sentiment_analysis_data = {'cik' : cik_identifier,
                   'financial_year_end' : financial_year,
                   'filing_year_end' : filing_year,
                   'filing_type' : filing_type,
                   'num_words_pre_clean' : num_words_pre_clean,
                   'num_words_post_clean' : num_words_post_cean,
                   'first_sentiment_score' : sentiment_score[0],
                   'second_sentiment_score' : sentiment_score[1],
                   'third_sentiment_score' : sentiment_score[2],
                   'fourth_sentiment_score' : sentiment_score[3],
                   'fifth_sentiment_score' : sentiment_score[4],
                   'sixth_sentiment_score' : sentiment_score[5],
                   'seventh_sentiment_score' : sentiment_score[6],
                   'eighth_sentiment_score' : sentiment_score[7],
                   'ninth_sentiment_score' : sentiment_score[8]}

理想情况下,sentimenat_analysis_data可以使用一个映射到分数列表的键'sentiment_scores',但是从问题中不清楚您可以在哪里进行更改。你知道吗

相关问题 更多 >