python实用技巧
作者:互联网
join
将序列中的元素以指定的字符连接生成一个新的字符串。
>>> seq = ("a", "b", "c")
>>> print('-'.join(seq)) #参数:连接的元素序列
a-b-c
>>> print(''.join(seq))
abc
anagram
所谓anagram,就是两个词所用的字母及其个数都是一样的,但是它们字母的位置不一样
>>> from collections import Counter
>>> a = 'abc'
>>> b = 'cba'
>>> Counter(a) == Counter(b)
True
>>> Counter(a)
Counter({'a': 1, 'b': 1, 'c': 1})
>>> Counter(b)
Counter({'c': 1, 'b': 1, 'a': 1})
倒转
"""字符串倒转"""
>>> L1 = 'abcdefg'
>>> L2 = L1[::-1] # 切片法,a[::-1]就相当于a[尾:头:-1]
>>> print(L1,L2)
abcdefg gfedcba
"""整数字倒转"""
>>> N1 = 123456789
>>> N2 = int(str(N1)[::-1])
>>> print(N2)
987654321
"""列表倒转"""
>>> L1 = ['a','b','c','d','e']
>>> L2 = [L for L in reversed(L1)] # 内置函数reversed(),用于list倒转
>>> print(L2)
['e', 'd', 'c', 'b', 'a']
标签:实用技巧,seq,python,Counter,倒转,L2,L1,print 来源: https://www.cnblogs.com/bigcarcar/p/16216482.html