Skip to content

Commit 4b2be3a

Browse files
author
guozhen3
committed
three examples
1 parent bffb5b3 commit 4b2be3a

9 files changed

Lines changed: 98 additions & 0 deletions

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ Python小例子、小Demo一网打尽。Python基础、Web开发、数据科学
6161
30. [链式比较](链式比较.md)
6262
31. [不用else和if实现计算器](不用else和if实现计算器.md)
6363
32. [Python链式操作](Python链式操作.md)
64+
33. [检查list是否有重复元素](检查list是否有重复元素.md)
65+
34. [求列表中所有重复元素](求列表中所有重复元素.md)
66+
35. [是否相同字母但顺序不同](是否相同字母但顺序不同.md)
67+
36. [求字符串的字节长度](求字符串的字节长度.md)
68+
6469

6570

6671
更多例子,正在每天更新...

src/anagram.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from collections import Counter
2+
3+
# 检查两个字符串是否 相同字母异序词,简称:互为变位词
4+
5+
6+
def anagram(str1, str2):
7+
return Counter(str1) == Counter(str2)
8+
9+
10+
anagram('eleven+two', 'twelve+one') # True 这是一对神器的变位词
11+
anagram('eleven', 'twelve') # False

src/find_all_duplicates.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from collections import Counter
2+
3+
4+
def find_all_duplicates(lst):
5+
c = Counter(lst)
6+
return list(filter(lambda k: c[k] > 1, c))
7+
8+
9+
find_all_duplicates([1, 2, 2, 3, 3, 3]) # [2,3]

src/has_duplicates.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
def has_duplicates(lst):
2+
return len(lst) == len(set(lst))
3+
4+
5+
x = [1, 1, 2, 2, 3, 2, 3, 4, 5, 6]
6+
y = [1, 2, 3, 4, 5]
7+
has_duplicates(x) # False
8+
has_duplicates(y) # True

src/str_byte_len.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def str_byte_len(mystr):
2+
return (len(mystr.encode('utf-8')))
3+
4+
5+
str_byte_len('i love python') # 13(个字节)
6+
str_byte_len('字符') # 6(个字节)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
title: C是否相同字母但顺序不同
3+
tags: Counter
4+
---
5+
6+
```python
7+
from collections import Counter
8+
9+
# 检查两个字符串是否 相同字母异序词,简称:互为变位词
10+
11+
12+
def anagram(str1, str2):
13+
return Counter(str1) == Counter(str2)
14+
15+
16+
anagram('eleven+two', 'twelve+one') # True 这是一对神器的变位词
17+
anagram('eleven', 'twelve') # False
18+
```

检查list是否有重复元素.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
title: 检查list是否有重复元素
3+
tags: list,set
4+
---
5+
6+
```python
7+
def has_duplicates(lst):
8+
return len(lst) == len(set(lst))
9+
10+
11+
x = [1, 1, 2, 2, 3, 2, 3, 4, 5, 6]
12+
y = [1, 2, 3, 4, 5]
13+
has_duplicates(x) # False
14+
has_duplicates(y) # True
15+
```

求列表中所有重复元素.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
title: 查找列表中所有重复元素
3+
tags: list,Counter
4+
---
5+
```python
6+
from collections import Counter
7+
8+
9+
def find_all_duplicates(lst):
10+
c = Counter(lst)
11+
return list(filter(lambda k: c[k] > 1, c))
12+
13+
14+
find_all_duplicates([1, 2, 2, 3, 3, 3]) # [2,3]
15+
```

求字符串的字节长度.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
title: 求字符串的字节长度
3+
tags: str
4+
---
5+
```python
6+
def str_byte_len(mystr):
7+
return (len(mystr.encode('utf-8')))
8+
9+
10+
str_byte_len('i love python') # 13(个字节)
11+
str_byte_len('字符') # 6(个字节)

0 commit comments

Comments
 (0)