بخشی از مجموعه

Several example codes

~1 دقیقه مطالعه • بروزرسانی ۳۱ شهریور ۱۴۰۴

Program Overview

This Python program receives a dictionary-like list of words and calculates the frequency of each word.
It’s useful for text analysis, identifying common terms, or building statistical word profiles.


Python Code:


# Sample list of words
words = ["apple", "banana", "apple", "orange", "banana", "apple", "kiwi"]

# Count word frequency using a dictionary
word_count = {}
for word in words:
    if word in word_count:
        word_count[word] += 1
    else:
        word_count[word] = 1

# Display results
for word, count in word_count.items():
    print(f"{word}: {count}")

Sample Output:


apple: 3  
banana: 2  
orange: 1  
kiwi: 1

Explanation:

- The input is a list of words, treated like a dictionary source
- A loop checks each word and updates its count in the word_count dictionary
- If the word already exists, its count is incremented
- At the end, all word-frequency pairs are printed using items()


نوشته و پژوهش شده توسط دکتر شاهین صیامی