# -*- coding: utf-8 -*- """ Created on Sun Sep 3 16:30:59 2023 """ import numpy as np import pandas as pd # ======================= # list a = [2,3,1,'1',5] #list() a.append(1) #加 print(a) a.remove(1) #值 print(a) b = a.pop(0) #位置 a.pop() 结尾元素弹出并赋值 print(a, b) b = sorted(a) print(a, b) order = sorted(range(len(a)), key=lambda i: a[i]) #索引排序 print(order, np.array(a)[order]) a.sort() #直接对a排序 print(a) print(a[1], a[-1], a[1:], a[:-1]) print(a[:1] + a[-2:]) print([1]*3) b = a.copy() #a[:] b[1] = 1 print(a, b) [i**2 for i in range(5)] [(i,ai) for i,ai in enumerate(a)] # ======================= # dict - dict内元素无序,不一定先入就排在前面 a = {'x':1, 'y':2} #dict() a['z'] = 3 print(a) a['x'] = 0 print(a) del a['z'] print(a, a.keys(), a.values(), a.items()) { key:val**2 for key,val in a.items() } # ======================= # tuple 元组 有序,元素不可变,可以用元组定义dict的key, 但是list不行 a = tuple([1,2,2,3,'3']) print(a) print(a[:1] + a[-2:]) # ======================= # 集合 set,元素无重复,无序 a = set([1,2,2,3,'3']) print(a) a.add('4') print(a) a.pop() #第一个元素弹出 a.remove(1) a.difference(set([1,2])) #差集 a.intersection(set([3,'3',6])) #交集 a.union(set([1,2])) #交集 a.issubset(set([2,3])) a.issuperset(set([2,3])) #string a = '23' a+a a*3 list(a) [i*2 for i in a] ','.join(list(a)) print('ab%sd' % 'c') print('ab%s%sd' % ('c', 'f')) print('ab{}{}d'.format('c', 'f')) print(f'ab{a}{a}') b = ''' as ''' print(b) # ======================= # numpy # ======================= a = np.array([1,2,3]) np.ones_like(a) np.zeros_like(a) np.empty_like(a) a = np.arange(5) np.linspace(-1, 1, 100) a = np.random.randn(5) a.round(2) a.astype(int) a.cumsum() np.prod(a) np.random.rand(3,4) np.ones(3) np.zeros(3,3) np.diag([[2,3],[1,4]]) np.diag([2,3]) a = np.random.randn(5) np.sum(a) a.sum() a.mean() a.std(ddof=1) a.median() np.percentile(a, [25,50,75]) a = np.random.randn(3,3) a+a a-a a*a a/a a**2 a @ a #np.sum(a * a) np.log(a) np.exp(a) np.pi np.abs(a) np.max(a) np.min(a) np.argmax(a) np.argmin(a) np.where(a>0) #原始数据几维,返回的tuple就为几维,可以【0】取出元素值为True的第0维索引 np.where(a>0, 1, -1) np.all(a > 0) np.any(a > 0) np.r_[a,a] np.c_[a,a] np.concatenate((a, a), axis=0) for i in a: print(i) # ======================= # pandas # ======================= path = r'd:\test.csv' #'d:/test.csv' pd.read_csv(path, encoding='utf-8') pd.to_csv(path, index=False) pd.read_clipboard() #header=0, sep='\t' a = pd.Series([1.234,2,3]) a.index a.values a.round(2) a.map(lambda x: x**2) dict(a) a = pd.DataFrame({'x': [1,2], 'y': [3,4]}) a = pd.DataFrame([[1,2],[3,4]], columns=['x','y']) a.index a.columns #可直接赋值改变列名 a.values a.head(5) a.sum(axis=0) a.mean() a.std() a.apply(lambda ser: ser['x'], axis=1) a['x'] #series a[['x']] #data frame a.x a.loc[:, 'x'] a.iloc[:, 0] a['z'] = [3,5] del a['z'] for i,ser in a.iterrows(): print(i, ser)
标签:set,python,list,基础,print,dict,pd,np From: https://www.cnblogs.com/iupoint/p/17675299.html