File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ # Python 面试题:相同字母异序词
2+
3+ Anagram,翻译成中文为"相同字母异序词"。
4+ 写函数判断二个字符串是不是"相同字母异序词",意思是说,所有字母相同,但前后顺序不一样。
5+
6+
7+ #! /usr/bin/python
8+ # coding:Utf-8
9+
10+
11+ def is_Anagram(s1, s2):
12+ """黄哥python黄哥所写"""
13+ s1, s2 = s1.lower(), s2.lower()
14+ lsts1 = list(s1)
15+ lsts2 = list(s2)
16+ lsts1.sort()
17+ lsts2.sort()
18+ for i, char in enumerate(lsts1):
19+ if lsts2[i] != char:
20+ return False
21+ return True
22+
23+
24+ def is_Anagram_another(s1, s2):
25+ """黄哥python黄哥所写"""
26+ s1, s2 = s1.lower(), s2.lower()
27+ lst1 = [0] * 26
28+ lst2 = [0] * 26
29+ length1, length2 = len(s1), len(s2)
30+ for i in range(length1):
31+ pos = ord(s1[i]) - ord('a')
32+ lst1[pos] += 1
33+
34+ for i in range(length2):
35+ pos = ord(s2[i]) - ord('a')
36+ lst2[pos] += 1
37+
38+ for i in range(26):
39+ if lst1[i] != lst2[i]:
40+ return False
41+ return True
42+
43+
44+ print is_Anagram("Abbc", "cbaba")
45+ print is_Anagram_another("Abbc", "cbaba")
46+ print is_Anagram("JKwwi", "wkjwi")
47+ print is_Anagram_another("JKwwi", "wkjwi")
48+
49+
50+
51+ [ 216小时学会Python] ( https://github.com/pythonpeixun/article/blob/master/python/hours_216.mdown )
52+ [ 感恩!感谢黄哥Python培训学员的支持和肯定。] ( https://github.com/pythonpeixun/article/blob/master/python/thanks.md )
53+
Original file line number Diff line number Diff line change 1+ # 黄哥漫谈Python 闭包
2+
3+ 在计算机科学中,闭包(英语:Closure),又称词法闭包(Lexical Closure)或函数闭包(function closures),是引用了自由变量的函数。这个被引用的自由变量将和这个函数一同存在,即使已经离开了创造它的环境也不例外。所以,有另一种说法认为闭包是由函数和与其相关的引用环境组合而成的实体。闭包在运行时可以有多个实例,不同的引用环境和相同的函数组合可以产生不同的实例。
4+
5+ 在函数中可以(嵌套)定义另一个函数时,如果内部的函数引用了外部的函数的变量,则可能产生闭包。运行时,一旦外部的 函数被执行,一个闭包就形成了,闭包中包含了内部函数的代码,以及所需外部函数中的变量的引用。其中所引用的变量称作上值(upvalue)。
6+
7+ Python 中通俗一点来说,如果在一个函数内部,嵌套了函数,这个内部函数对(非全局作用域)外部作用域的变量进行引用,那么这个内部函数称为闭包。闭包每次运行是能记住引用的外部作用域的变量的值。
8+
9+ 理解下面这个代码,函数返回值为函数,Python 函数为第一类对象,可以赋值给变量,可以作为参数传递,可以从函数中返回。
10+
11+ 被返回的函数为闭包。nonlocal 为Python 3的语法,表示非局部变量。
12+
13+ 下面的代码,是回答这个https://www.zhihu.com/question/48735888 问题。
14+
15+ # coding:utf-8
16+
17+
18+ def foo():
19+ x = 5
20+
21+ def inner():
22+ nonlocal x
23+ x += 1
24+ return x
25+ return inner
26+
27+ p = foo()
28+ print(p())
29+ print(p())
30+ print(p())
31+
32+
33+
34+ 可以从单步调试来观看x值的变化。
35+
36+ ![ Closure1.png]
37+
38+ ![ Closure2.png]
39+
40+
41+ ![ Closure3.png]
42+
43+ ![ Closure4.png]
44+
45+
46+ 闭包作用:
47+
48+ 可以用来编写惰性求值的代码,可以用在函数调用时保持特定状态。
49+
50+ 装饰器中要用到闭包。
51+
52+ 一个对Python初学者难以理解的作用域代码
53+
54+ https://zhuanlan.zhihu.com/p/21528317
55+
56+
57+ [ 216小时学会Python] ( https://github.com/pythonpeixun/article/blob/master/python/hours_216.mdown )
58+
59+ [ 感恩!感谢黄哥Python培训学员的支持和肯定。] ( https://github.com/pythonpeixun/article/blob/master/python/thanks.md )
Original file line number Diff line number Diff line change 1616
17173、黄哥也不是很牛逼的程序员(不然也在bta拿年薪百万,人贵有自知之明,黄哥也在不断学习中。),但黄哥能帮助学员快速的完成0到1(从零基础到自己会写代码解决问题。)的这个过程。
1818
19- [ 感恩!感谢黄哥Python培训学员的支持和肯定。] ( https://github.com/pythonpeixun/article/blob/master/python/thanks.md )
2019
2120
22- [ 216小时学会Python] ( https://github.com/pythonpeixun/article/blob/master/python/hours_216.mdown )
21+ [ 216小时学会Python] ( https://github.com/pythonpeixun/article/blob/master/python/hours_216.mdown )
22+
23+ [ 感恩!感谢黄哥Python培训学员的支持和肯定。] ( https://github.com/pythonpeixun/article/blob/master/python/thanks.md )
You can’t perform that action at this time.
0 commit comments