def outside():
print(‘I am outside!‘)
def inside():
print(‘I am inside!‘)
inside()
def outside():
print(‘I am outside!‘)
def inside():
print(‘I am inside!‘)
return inside()
outside()
def outside():
var = 5
def inside():
var = 3
print(var)
inside()
outside()
def outside():
var = 5
def inside():
print(var)
var = 3
inside()
outside()
def funOut():
def funIn():
print(‘宾果!你成功访问到我啦!‘)
return funIn()
def funOut():
def funIn():
print(‘宾果!你成功访问到我啦!‘)
return funIn
‘‘‘这里的意思是,funOut 函数不加括号,表示返回它的内存地址
然而加一个括号的情况下,由于funOut函数返回的是内部函数funIn的值
而返回 return funIn 函数没有小括号,因此返回的也是它的内存地址,
所以最终 funOut 函数如果要调用funIn的返回值,需要再加一个小括号‘‘‘
def funX():
x = 5
def funY():
nonlocal x
x += 1
return x
return funY
a = funX()
print(a())
print(a())
print(a())
6
7
8
a = ‘字符串‘
list1 = []
list2 = []
for i in a:
if i not in list1:
if i == ‘\n‘:
print(‘换行‘, a.count(i)) # a.count(i) 方法可以直接统计在字符串a中,字符 i 出现的次数
else:
print(i, a.count(i))
list1.append(i)
if i.isalpha(): # 把字母和汉字提取并写入list2 列表
list2.append(i)
print(‘‘.join(list2)) # 用join方法把list2 列表转字符串
str1 = ‘字符串‘
countA = 0 # 统计前边的大写字母
countB = 0 # 统计小写字母
countC = 0 # 统计后边的大写字母
length = len(str1)
for i in range(length):
if str1[i] == ‘\n‘:
continue
"""
|如果str1[i]是大写字母:
|-- 如果已经出现小写字母:
|-- -- 统计后边的大写字母
|-- 如果未出现小写字母:
|-- -- 清空后边大写字母的统计
|-- -- 统计前边的大写字母
"""
if str1[i].isupper():
if countB:
countC += 1
else:
countC = 0
countA += 1
"""
|如果str1[i]是小写字母:
|-- 如果小写字母前边不是三个大写字母(不符合条件):
|-- -- 清空所有记录,重新统计
|-- 如果小写字母前边是三个大写字母(符合条件):
|-- -- 如果已经存在小写字母:
|-- -- -- 清空所有记录,重新统计(出现两个小写字母)
|-- -- 如果该小写字母是唯一的:
|-- -- -- countB记录出现小写字母,准备开始统计countC
"""
if str1[i].islower():
if countA != 3:
countA = 0
countB = 0
countC = 0
else:
if countB:
countA = 0
countB = 0
countC = 0
else:
countB = 1
countC = 0
target = i
"""
|如果前边和后边都是三个大写字母:
|-- 如果后边第四个字母也是大写字母(不符合条件):
|-- -- 清空记录B和C,重新统计
|-- 如果后边仅有三个大写字母(符合所有条件):
|-- -- 打印结果,并清空所有记录,进入下一轮统计
"""
if countA == 3 and countC == 3:
if i+1 != length and str1[i+1].isupper():
countB = 0
countC = 0
else:
print(str1[target], end=‘‘)
countA = 3
countB = 0
countC = 0
原文:https://www.cnblogs.com/jaderadish/p/14860388.html