Python 在问答频道中刷题积累到的小技巧(八)

x33g5p2x  于2022-07-26 转载在 Python  
字(3.0k)|赞(0)|评价(0)|浏览(260)

1. 输入值限制在一个区间之内

如输入值超出区间之外,则用区间两端的值替代。

>>> x = min(10, max(int(input()), 0))
3
>>> x
3
>>> x = min(10, max(int(input()), 0))
-2
>>> x
0
>>> x = min(10, max(int(input()), 0))
11
>>> x
10

2. 找出列表中成对出现的元素:

>>> from random import randint as rnd
>>> a = [rnd(1,20) for _ in range(30)]
>>> list(set(filter(lambda x:a.count(x)==2,a)))
[9, 12, 15, 18, 19]
>>> #出见多次的:
>>> list(set(filter(lambda x:a.count(x)>1,a)))
[9, 11, 12, 13, 15, 18, 19, 20]
>>> list(set(filter(lambda x:a.count(x)>2,a)))
[11, 20, 13]
>>> list(set(filter(lambda x:a.count(x)>3,a)))
[20, 13]
>>>
>>> #通常用集合set()去重:
>>> list(set(a))
[2, 4, 6, 7, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
>>> #字典dict()也能完成去重:
>>> list(dict(zip(a,a)))
[15, 13, 9, 20, 11, 17, 18, 12, 2, 14, 19, 16, 6, 4, 7, 10]
>>> list({}.fromkeys(a))
[15, 13, 9, 20, 11, 17, 18, 12, 2, 14, 19, 16, 6, 4, 7, 10]
>>> list(sorted({}.fromkeys(a)))
[2, 4, 6, 7, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
>>>
>>> #不用filter()的其它方法
>>> b = set(a); t = [b.discard(x) for x in a if a.count(x)!=2]
>>> list(b)
[9, 12, 15, 18, 19]

3. replace()被忽略的第三参数count

不一定非要全部替代,count可以指定替代次数。

>>> 'abacadae'.replace('a','b',1)
'bbacadae'
>>> 'abacadae'.replace('a','b',2)
'bbbcadae'
>>> 'abacadae'.replace('a','b',3)
'bbbcbdae'
>>> 'abacadae'.replace('a','b',-1)
'bbbcbdbe'
>>> 'abacadae'.replace('a','b')
'bbbcbdbe'
>>> 'abacadae'.replace('a','b',0)
'abacadae'
>>> 'abacadae'.replace('a','b',100)
'bbbcbdbe'

help(''.replace)
Help on built-in function replace:

replace(old, new, count=-1, /) method of builtins.str instance
    Return a copy with all occurrences of substring old replaced by new.
    
      count
        Maximum number of occurrences to replace.
        -1 (the default value) means replace all occurrences.
    
    If the optional argument count is given, only the first count occurrences are
    replaced.

4. 从id获取对象的值

>>> from _ctypes import PyObj_FromPtr as getVal
>>> a = 1.515
>>> b = 'abcde'
>>> c = [1,2,3,5]
>>> address1 = id(a)
>>> address2 = id(b)
>>> address3 = id(c)
>>> getVal(address1)
1.515
>>> getVal(address2)
'abcde'
>>> getVal(address3)
[1, 2, 3, 5]

5. 带key参数的函数

网上搜“带key参数的函数”多数写4个,其实有5个:max, min, map, filter, sorted(与列表类中自带的方法.sort()类似)。这些函数之前分开都有讲过,这里再总结一下。

**6. 汉明距离:**两个等长二进制数对应bit位不相等的个数
汉明距离是理查德·卫斯里·汉明在误差检测与校正码的基础性论文中首次引入这个概念。在信息论中,两个等长字符串之间的汉明距离是两个字符串对应位置的不同字符的个数。换句话说,它就是将一个字符串变换成另外一个字符串所需要替换的字符个数。在图像处理领域也有这广泛的应用,是比较二进制图像非常有效的手段;在通信中,是累计定长二进制字中发生翻转的错误数据位,所以它也被称为信号距离。另外在信息论、编码理论、密码学等领域都有应用。
计算一个数字的比特位包含1的个数有个小技巧:value &= value - 1这个运算的结果就是把value最后一个1去掉,循环进行运算直到value等于0(所有的1都被去掉)就可以知道vaule拥有多少个1了。

>>> def count1(i):
	count = 0
	while i:
		count += 1
		i &= i - 1
	return count

>>> i = 123456789
>>> count1(i)
16
>>> bin(i).count('1')  #验证
16
>>> j = 987654321
>>> count1(i^j)
15
>>> bin(i^j).count('1')
15
>>> 
>>> def hmdistance(x,y):
	count = 0
	xor = x^y
	while xor:
		count += 1
		xor &= xor - 1
	return count

>>> hmdistance(i,j)
15
>>> hmdistance(j,i)
15

相关文章