1、自定义字符串的count函数
>>> s = "Abcdb"
>>> s.count("b")
2
>>> s.count("bc")
1
>>> s.count("bd")
0
>>>
#可以包含重叠的字符
def str_count(string,sub_str):
if (not isinstance(string,str)) or (not isinstance(sub_str,str)):
return "string或sub_str不是字符串类型"
sub_count = 0
sub_len = len(sub_str)
#遍历
for i in range(len(string)):
if string[i:i+sub_len] == sub_str:
sub_count += 1
return sub_count
print(str_count("Abcbcb","b"))
print("Abcbcb".count("b"))
print(str_count("Abcbcb","bc"))
print("Abcbcb".count("bc"))
print(str_count("bcbcb","bcb"))
print("Abcbcb".count("bcb"))
print(str_count("bcbcb123","123"))
print("bcbcb123".count("123"))
print(str_count("bcbcb123",int("123")))
print("bcbcb123".count(int("123")))
>>> def str_count(string,sub_str):
... if (not isinstance(string,str)) or (not isinstance(sub_str,str)):
... return "string或sub_str不是字符串类型"
... sub_count = 0
... sub_len = len(sub_str)
... #遍历
... for i in range(0,len(string)):
... if string[i:i+sub_len] == sub_str:
... sub_count += 1
... return sub_count
...
>>> print(str_count("Abcbcb","b"))
3
>>> print("Abcbcb".count("b"))
3
>>> print(str_count("Abcbcb","bc"))
2
>>> print("Abcbcb".count("bc"))
2
>>> print(str_count("bcbcb","bcb"))
2
>>> print("Abcbcb".count("bcb"))
1
>>> print(str_count("bcbcb123","123"))
1
>>> print("bcbcb123".count("123"))
1
>>> print(str_count("bcbcb123",int("123")))
string或sub_str不是字符串类型
>>> print("bcbcb123".count(int("123")))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: must be str, not int
>>>
-----------------------------------------------------------------------
#优化后:不包含重叠字符
def str_count(string,sub_str):
if (not isinstance(string,str)) or (not isinstance(sub_str,str)):
return "string或sub_str不是字符串类型"
sub_count = 0
sub_len = len(sub_str)
#遍历
i = 0
while i < len(string):
if string[i:i+sub_len] == sub_str:
sub_count += 1
i += sub_len
else:
i += 1
return sub_count
print(str_count("Abcbcb","b"))
print("Abcbcb".count("b"))
print(str_count("Abcbcb","bc"))
print("Abcbcb".count("bc"))
print(str_count("bcbcb","bcb"))
print("Abcbcb".count("bcb"))
print(str_count("bcbcb123","123"))
print("bcbcb123".count("123"))
print(str_count("bcbcb123",int("123")))
print("bcbcb123".count(int("123")))
>>> def str_count(string,sub_str):
... if (not isinstance(string,str)) or (not isinstance(sub_str,str)):
... return "string或sub_str不是字符串类型"
... sub_count = 0
... sub_len = len(sub_str)
... #遍历
... i = 0
... while i < len(string):
... if string[i:i+sub_len] == sub_str:
... sub_count += 1
... i += sub_len
... else:
... i += 1
... return sub_count
...
>>> print(str_count("bcbcb","bcb"))
1
>>> print("Abcbcb".count("bcb"))
1
>>>
-----------------------------------------------------------------------
def count(s,letter):
if not isinstance(s,str) or not isinstance(letter,str):
return 0
else:
sum=0
for i in s:
if i==letter:
sum+=1
return sum
print(count("123sdf1sf","s"))
>>> def count(s,letter):
... if not isinstance(s,str) or not isinstance(letter,str):
... return 0
... else:
... sum=0
... for i in s:
... if i==letter:
... sum+=1
... return sum
...
>>> print(count("123sdf1sf","s"))
2
>>>
2、求100以内的素数之和
prime_sum = 0
def is_prime(num):
for n in range(2,num):
if num % n == 0:
return False
return True
for num in range(2,100):
if is_prime(num):
prime_sum += num
print("100以内的的素数和: ",prime_sum)
>>> prime_sum = 0
>>> def is_prime(num):
... for n in range(2,num):
... if num % n == 0:
... return False
... return True
...
>>> for num in range(2,100):
... if is_prime(num):
... prime_sum += num
...
>>> print("100以内的的素数和: ",prime_sum)
100以内的的素数和: 1060
>>>
def fun(num):
sum = 0
for num in range(2, num):
n = int(pow(num, 0.5)) + 1
for i in range(2, n):
if num % i == 0:
break
else:
sum += num
return sum
sum = fun(100)
print(sum)
print(fun(100))
>>> def fun(num):
... sum = 0
... for num in range(2, num):
... n = int(pow(num, 0.5)) + 1
... for i in range(2, n):
... if num % i == 0:
... break
... else:
... sum += num
... return sum
...
>>> sum = fun(100)
>>> print(sum)
1060
>>> print(fun(100))
1060
>>>
#先求100以内的质数/素数
#1、
num=[]
i=2
for i in range(2,100):
j=2
for j in range(2,i):
if i % j == 0:
break
else:
num.append(i)
print(num)
#2、
import math
def func_get_prime(n):
return list(filter(lambda x: not [x%i for i in range(2, int(math.sqrt(x))+1) if x%i ==0], range(2,n+1)))
print (func_get_prime (100))
#3、
import math
N = 100
print (list(p for p in range(2, N) if 0 not in ( p%i for i in range(2,int(math.sqrt(p))+1))))
#再求和
#1、使用 reduce 函数求和
from functools import reduce
sum = reduce(lambda x,y:x+y,num)
print(sum)
#2、使用递归求和
prime_num = list(num)
print(prime_num)
def sum(list,size):
if size == 0:
return 0
else:
return list[size-1] + sum(list,size-1)
total = sum(prime_num,len(prime_num))
print("素数/质数之和为:",total)
#3、使用for循环求和
total = 0
prime_num = list(num)
print(prime_num)
for i in range(0,len(prime_num)):
total += prime_num[i]
print("素数/质数之和为:",total)
#4、使用while循环求和
total = 0
i = 0
prime_num = list(num)
print(prime_num)
while i < len(prime_num):
total += prime_num[i]
i += 1
print("列表元素之和为: ", total)
----------------------------------------------------------------------------------------
#执行结果:
>>> print(prime_num)
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
>>> print("列表元素之和为: ", total)
列表元素之和为: 1060
3、求以下矩阵四边元素之和
l = [
[1,2,3,4,5],
[1,2,3,4,5],
[1,2,3,4,5],
[1,2,3,4,5],
[1,2,3,4,5],
]
#大矩阵减去小矩阵
l = [
[1,2,3,4,5],
[1,2,3,4,5],
[1,2,3,4,5],
[1,2,3,4,5],
[1,2,3,4,5],
]
out_sum = 0
inner_sum = 0
for i in range(len(l)):
for j in range(len(l)):
out_sum += l[i][j]
for i in range(0,4):
for j in range(0,4):
inner_sum += l[i][j]
print("列表4边元素之和:",out_sum - inner_sum)
>>> l = [
... [1,2,3,4,5],
... [1,2,3,4,5],
... [1,2,3,4,5],
... [1,2,3,4,5],
... [1,2,3,4,5],
... ]
>>>
>>> out_sum = 0
>>> inner_sum = 0
>>> for i in range(len(l)):
... for j in range(len(l)):
... out_sum += l[i][j]
...
>>> for i in range(0,4):
... for j in range(0,4):
... inner_sum += l[i][j]
...
>>> print("列表4边元素之和:",out_sum - inner_sum)
列表4边元素之和: 48
----------------------------------------------------------------
l = [
[1,2,3,4,5],
[1,2,3,4,5],
[1,2,3,4,5],
[1,2,3,4,5],
[1,2,3,4,5],
]
fourside_sum = 0
for i in range(len(l)):
if i in (0,4):
fourside_sum += sum(l[i])
else:
fourside_sum += (l[i][1] + l[i][3])
print("列表4边元素之和:",fourside_sum)
>>> l = [
... [1,2,3,4,5],
... [1,2,3,4,5],
... [1,2,3,4,5],
... [1,2,3,4,5],
... [1,2,3,4,5],
... ]
>>>
>>> fourside_sum = 0
>>> for i in range(len(l)):
... if i in (0,4):
... fourside_sum += sum(l[i])
... else:
... fourside_sum += (l[i][1] + l[i][3])
...
>>> print("列表4边元素之和:",fourside_sum)
列表4边元素之和: 48
>>>