python 小技巧

1.获取列表中出现频率最多的值

a = [1, 2, 3, 1, 5, 4]

print (max(set(a), key=a.count))
# 3


from collections import Counter

cnt = Counter(a)
print cnt.most_common(1)
# (3,5)

2. 判断翻转字符串是否相等

str1 = '12345'
str2 = '54321'

from collections import Counter

print (Counter(str1) == Counter(str2))
# True

3. 翻转字符串或数字、列表

a = 'abcdefghigklmnopqrstuvwxyz'
print (a[::-1])
# zyxwvutsrqponmlkgihgfedcba

print ''.join(list(reversed(a)))
# zyxwvutsrqponmlkgihgfedcba

num =123456789
print (int(str(num)[::-1]))
# 987654321

a=[1,2,3,4,5]
print(a[::-1])
# [5,1]


相关文章

Python中的函数(二) 在上一篇文章中提到了Python中函数的定...
Python中的字符串 可能大多数人在学习C语言的时候,最先接触...
Python 面向对象编程(一) 虽然Python是解释性语言,但是它...
Python面向对象编程(二) 在前面一篇文章中谈到了类的基本定...
Python中的函数(一) 接触过C语言的朋友对函数这个词肯定非...
在windows下如何快速搭建web.py开发框架 用Python进行web开发...