Python实现 给一个包含1000w字符串的文件,写个程序统计下文件中每个字符串出现的次数,并且按字符串出现的次数排序,可以以伪代码形式输出
时间: 2024-05-09 22:16:44 浏览: 14
以下是可能的伪代码实现:
```
# 创建一个字典来存储字符串和其出现次数
string_count = {}
# 打开文件并逐行遍历
with open('file.txt', 'r') as f:
for line in f:
# 删除每行末尾的换行符
line = line.rstrip('\n')
# 如果字符串已经在字典中,增加其出现次数
if line in string_count:
string_count[line] += 1
# 否则,在字典中添加该字符串并将其出现次数设置为1
else:
string_count[line] = 1
# 按照出现次数对字典进行排序
sorted_string_count = sorted(string_count.items(), key=lambda x: x[1], reverse=True)
# 输出排序后的结果
for string, count in sorted_string_count:
print(string, count)
```
此实现使用了 Python 中的字典(dictionary)来存储每个字符串及其出现次数,并使用了 Python 的 `sorted` 函数以出现次数为键进行排序。
相关问题
帮我写一段python代码,统计文件夹中每个文件中不同字符串出现的次数并统计成表格
以下是一段实现该功能的Python代码:
```python
import os
def count_strings(folder_path):
# 统计不同字符串出现的次数
file_counts = {}
for file_name in os.listdir(folder_path):
file_path = os.path.join(folder_path, file_name)
with open(file_path) as f:
for line in f:
for word in line.strip().split():
if word in file_counts:
file_counts[word][file_name] += 1
else:
file_counts[word] = {file_name: 1}
return file_counts
def print_table(file_counts):
# 打印统计表格
max_file_name_len = max(len(file_name) for file_name in file_counts.values())
max_word_len = max(len(word) for word in file_counts.keys())
print(f'{"Word":<{max_word_len}} | {"Total":<{max_file_name_len}} | ', end='')
for file_name in sorted(file_counts.values()):
print(f'{file_name:<{max_file_name_len}} | ', end='')
print()
print('-' * (max_word_len + max_file_name_len + 4))
for word, file_counts in sorted(file_counts.items()):
print(f'{word:<{max_word_len}} | {sum(file_counts.values()):<{max_file_name_len}} | ', end='')
for file_name in sorted(file_counts.keys()):
print(f'{file_counts[file_name]:<{max_file_name_len}} | ', end='')
print()
if __name__ == '__main__':
folder_path = 'folder_path_here' # 替换为实际的文件夹路径
file_counts = count_strings(folder_path)
print_table(file_counts)
```
该代码首先定义了一个 `count_strings` 函数,它使用 `os` 模块遍历文件夹中的每个文件,统计每个文件中不同字符串出现的次数,并将统计结果保存在一个字典中。该字典的键为字符串,值为另一个字典,其中键为文件名,值为该文件中该字符串出现的次数。
然后,代码定义了一个 `print_table` 函数,它打印统计结果的表格。该函数首先计算出表格中每一列的最大宽度,然后使用字符串格式化来打印表格。表格的第一行显示文件名,后面的行显示每个字符串在每个文件中出现的次数。
最后,在 `if __name__ == '__main__'` 中,代码调用 `count_strings` 和 `print_table` 函数来统计并打印文件夹中每个文件中不同字符串出现的次数的表格。需要将 `folder_path_here` 替换为实际的文件夹路径。
帮我写一段python代码,统计文件夹中某串字符在每个文件中出现的次数并统计成表格
以下是一个基本的Python代码,可以统计文件夹中所有文件中某个字符串的出现次数,并将结果输出为表格:
```python
import os
# 定义要查找的字符串
search_string = "hello"
# 定义要搜索的文件夹路径
folder_path = "/path/to/folder"
# 初始化一个字典,用于存储每个文件中字符串出现的次数
result = {}
# 遍历文件夹中的所有文件
for filename in os.listdir(folder_path):
# 如果是文件而不是文件夹
if os.path.isfile(os.path.join(folder_path, filename)):
# 打开文件
with open(os.path.join(folder_path, filename), "r") as f:
# 读取文件内容
content = f.read()
# 计算字符串在文件中出现的次数
count = content.count(search_string)
# 将结果存储到字典中
result[filename] = count
# 打印结果表格
print("Filename\tCount")
print("--------------------")
for filename, count in result.items():
print("{}\t{}".format(filename, count))
```
你可以将代码中的`search_string`和`folder_path`变量替换为你自己想要搜索的字符串和文件夹路径。代码将遍历文件夹中的所有文件,并针对每个文件计算指定字符串的出现次数。最后,代码将结果输出为一个简单的表格。
相关推荐
![application/x-tar, POSIX](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)