267 lines
8.4 KiB
Python
267 lines
8.4 KiB
Python
# -*- coding: utf-8 -*-
|
||
# @Time : 2024/2/3 12:45
|
||
# @Author : len
|
||
# @File : 数据处理1.py
|
||
# @Software: PyCharm
|
||
# @Comment :
|
||
|
||
|
||
# 统计字符串中的空格数、大写字母数和小写字母数。
|
||
def count_chars(input_str):
|
||
"""
|
||
计算输入字符串中的空格数、大写字母数和小写字母数。
|
||
参数:
|
||
input_str (str): 输入的字符串。
|
||
返回:
|
||
tuple: 包含三个整数,分别是空格数、大写字母数和小写字母数。
|
||
"""
|
||
# 初始化计数器
|
||
space_count = 0
|
||
uppercase_count = 0
|
||
lowercase_count = 0
|
||
|
||
# 遍历输入字符串中的每个字符
|
||
for char in input_str:
|
||
# 如果字符是空格
|
||
if char == ' ':
|
||
space_count += 1
|
||
# 如果字符是大写字母
|
||
elif 'A' <= char <= 'Z':
|
||
uppercase_count += 1
|
||
# 如果字符是小写字母
|
||
elif 'a' <= char <= 'z':
|
||
lowercase_count += 1
|
||
|
||
# 返回计数结果
|
||
return space_count, uppercase_count, lowercase_count
|
||
|
||
# 示例用法
|
||
input_str = "Hello World!" # 可以替换为任意字符串
|
||
space_count, uppercase_count, lowercase_count = count_chars(input_str)
|
||
print(f"原字串:{input_str}, 空格: {space_count}, 大写字母: {uppercase_count}, 小写字母: {lowercase_count}")
|
||
|
||
|
||
# 计一个字符串在另一个字符串中出现的次数。
|
||
def count_substring_occurrence(str1, str2):
|
||
"""
|
||
统计一个字符串在另一个字符串中出现的次数。
|
||
|
||
参数:
|
||
str1 (str): 主字符串,将在此字符串中搜索子字符串。
|
||
str2 (str): 子字符串,需要统计在主字符串中出现的次数。
|
||
|
||
返回:
|
||
int: 子字符串在主字符串中出现的次数。
|
||
"""
|
||
# 初始化计数器
|
||
count = 0
|
||
# 获取两个字符串的长度
|
||
len_str1 = len(str1)
|
||
len_str2 = len(str2)
|
||
|
||
# 遍历主字符串
|
||
for i in range(len_str1 - len_str2 + 1):
|
||
# 如果在当前位置找到子字符串,则计数器加一
|
||
if str1[i:i + len_str2] == str2:
|
||
count += 1
|
||
|
||
return count
|
||
|
||
# 示例用法
|
||
str1 = "Hello World, Hello Python!"
|
||
str2 = "Hello"
|
||
occurrence = count_substring_occurrence(str1, str2)
|
||
print(f"'{str2}' 在 '{str1}' 中出现了 {occurrence} 次")
|
||
|
||
|
||
# 对字符串进行排序
|
||
def sort_string_characters(input_str, ascending=True):
|
||
"""
|
||
对输入字符串中的字符进行排序(使用选择排序算法),并根据用户选择进行升序或降序排序。
|
||
|
||
参数:
|
||
input_str (str): 输入的字符串。
|
||
ascending (bool): 排序顺序标志,True为升序,False为降序,默认为升序。
|
||
|
||
返回:
|
||
str: 字符排序后形成的新字符串。
|
||
"""
|
||
char_list = list(input_str) # 将字符串转换为字符列表
|
||
n = len(char_list)
|
||
|
||
for i in range(n):
|
||
# 根据ascending标志,选择适当的比较逻辑
|
||
target_index = i
|
||
for j in range(i + 1, n):
|
||
if (ascending and char_list[target_index] > char_list[j]) or \
|
||
(not ascending and char_list[target_index] < char_list[j]):
|
||
target_index = j
|
||
|
||
# 交换元素
|
||
char_list[i], char_list[target_index] = char_list[target_index], char_list[i]
|
||
|
||
return ''.join(char_list)
|
||
|
||
# 示例用法
|
||
input_str = "hello"
|
||
sorted_str_asc = sort_string_characters(input_str, ascending=True) # 升序排序
|
||
sorted_str_desc = sort_string_characters(input_str, ascending=False) # 降序排序
|
||
print(f"{input_str} 升序排序后的字符串: {sorted_str_asc}")
|
||
print(f"{input_str} 降序排序后的字符串: {sorted_str_desc}")
|
||
|
||
|
||
# 在指定位置插入字符串
|
||
def insert_string_at_position(original_str, insert_str, position):
|
||
"""
|
||
在指定位置插入字符串。如果位置是负数,则从字符串末尾开始计数。
|
||
|
||
参数:
|
||
original_str (str): 原始字符串。
|
||
insert_str (str): 要插入的字符串。
|
||
position (int): 插入位置的索引,正数从前往后数,负数从后往前数。
|
||
|
||
返回:
|
||
str: 插入字符串后的新字符串。
|
||
"""
|
||
# 调整负数索引
|
||
if position < 0:
|
||
position = len(original_str) + position + 1
|
||
|
||
# 检查调整后的插入位置是否有效
|
||
if position < 0 or position > len(original_str):
|
||
return "插入位置无效"
|
||
|
||
# 在指定位置插入字符串
|
||
new_str = original_str[:position] + insert_str + original_str[position:]
|
||
|
||
return new_str
|
||
|
||
# 示例用法
|
||
original_str = "Hello World"
|
||
insert_str = "Python "
|
||
position = 6
|
||
new_str = insert_string_at_position(original_str, insert_str, position)
|
||
print(f"插入字符串后的结果: {new_str}")
|
||
|
||
|
||
# 统计子字符串在主字符串中出现的次数
|
||
def find_substring_occurrences(main_str, sub_str):
|
||
"""
|
||
手动计算一个字符串在另一个字符串中出现的次数。
|
||
|
||
参数:
|
||
main_str (str): 主字符串,将在此字符串中搜索子字符串。
|
||
sub_str (str): 子字符串,需要统计在主字符串中出现的次数。
|
||
|
||
返回:
|
||
int: 子字符串在主字符串中出现的次数。
|
||
"""
|
||
# 初始化计数器和起始索引
|
||
count = 0
|
||
start = 0
|
||
|
||
# 主字符串的长度和子字符串的长度
|
||
main_length = len(main_str)
|
||
sub_length = len(sub_str)
|
||
|
||
# 当子字符串的长度大于0时才进行搜索
|
||
if sub_length > 0:
|
||
# 遍历主字符串
|
||
while start <= main_length - sub_length:
|
||
# 找到子字符串的下一个匹配位置
|
||
position = main_str.find(sub_str, start)
|
||
|
||
# 如果没有找到,退出循环
|
||
if position == -1:
|
||
break
|
||
|
||
# 如果找到了,增加计数并更新起始索引
|
||
count += 1
|
||
start = position + 1 # 更新起始索引以查找重叠情况
|
||
return count
|
||
|
||
# 示例用法
|
||
main_str = "the quick brown fox jumps over the lazy dog, the dog was not amused."
|
||
sub_str = "the"
|
||
occurrences = find_substring_occurrences(main_str, sub_str)
|
||
print(f"'{sub_str}' 在 '{main_str}' 中出现了 {occurrences} 次")
|
||
|
||
|
||
|
||
# 删除主字符串中所有出现的子字符串
|
||
def remove_substring(original_str, remove_str):
|
||
"""
|
||
不使用replace方法,从原始字符串中删除所有出现的指定子字符串。
|
||
参数:
|
||
original_str (str): 原始字符串。
|
||
remove_str (str): 需要在原始字符串中删除的子字符串。
|
||
返回:
|
||
str: 删除指定子字符串后的新字符串。
|
||
"""
|
||
# 检查删除的子字符串是否为空
|
||
if not remove_str:
|
||
return original_str
|
||
|
||
result_str = "" # 初始化结果字符串
|
||
i = 0
|
||
|
||
while i <= len(original_str) - len(remove_str):
|
||
# 检查当前位置的子字符串是否为需要删除的字符串
|
||
if original_str[i:i + len(remove_str)] == remove_str:
|
||
i += len(remove_str) # 跳过需要删除的字符串
|
||
else:
|
||
result_str += original_str[i] # 将当前字符添加到结果字符串
|
||
i += 1
|
||
|
||
# 添加剩余的字符
|
||
if i < len(original_str):
|
||
result_str += original_str[i:]
|
||
|
||
return result_str
|
||
|
||
# 示例用法
|
||
original_str = "Hello World, Hello Python!"
|
||
remove_str = "Hello "
|
||
result_str = remove_substring(original_str, remove_str)
|
||
print(f"删除'{remove_str}'后的字符串: {result_str}")
|
||
|
||
|
||
def replace_substring(original_str, target_str, replacement_str):
|
||
"""
|
||
在原始字符串中替换所有出现的目标子字符串为新的子字符串。
|
||
|
||
参数:
|
||
original_str (str): 原始字符串。
|
||
target_str (str): 需要被替换的目标子字符串。
|
||
replacement_str (str): 用于替换的新子字符串。
|
||
|
||
返回:
|
||
str: 替换指定子字符串后的新字符串。
|
||
"""
|
||
result_str = "" # 初始化结果字符串
|
||
i = 0
|
||
|
||
while i <= len(original_str) - len(target_str):
|
||
if original_str[i:i + len(target_str)] == target_str:
|
||
result_str += replacement_str # 添加新子字符串到结果字符串
|
||
i += len(target_str) # 跳过目标子字符串的长度
|
||
else:
|
||
result_str += original_str[i] # 将当前字符添加到结果字符串
|
||
i += 1
|
||
|
||
# 添加末尾剩余的字符
|
||
if i < len(original_str):
|
||
result_str += original_str[i:]
|
||
|
||
return result_str
|
||
|
||
# 示例用法
|
||
original_str = "I love programming in Python. Python is great!"
|
||
target_str = "Python"
|
||
replacement_str = "JavaScript"
|
||
result_str = replace_substring(original_str, target_str, replacement_str)
|
||
print(f"替换后的字符串: {result_str}")
|
||
|
||
|
||
|