1.查看变量所占内存大小
1 import sys
2 a,b,c= 'I','love','python'
3 print(sys.getsizeof(a)) # 50 ,查看变量占用的内存,不同的字符串消耗不同的内存
2.翻转字符串
1 List = ['I','love','python']
方法1
List.reverse() # 翻转,自身的改变 ['python','love','I']
方法2
List = List[::-1] # ['python','love','I']
3,字符串切片
1 List = List[::1] # ['I', 'love', 'python']
2 List = List[::2] # ['I', 'python'] 切片,步长为2
3 List = List[::3] # ['I'] 步长为3
4.字符串连接
print(' '.join(List)) # I love python 使用空格组合列表中的字符串
5.转换嵌套列表
import itertools
List = [[1,2],[3,4],[5,6]]
l=list(itertools.chain.from_iterable(List))
print(l)# [1, 2, 3, 4, 5, 6]
6.转置矩阵
matrix = [[1,2,3],[4,5,6]]
print(list(zip(*matrix))) # [(1, 4), (2, 5), (3, 6)]
7.比较列表,取交集,差集比较来比较
a = ['I','love','PYTHON','love']
b = ['I','love','python']
print(set(a))# {'PYTHON', 'love', 'I'} 去重
print(set(a).difference(set(b))) # {'PYTHON'}
print(set(a).intersection(b)) # {'love', 'I'}
8.牛客上标准的输入
import sys
n = sys.stdin.readline()