python 使用技巧(不断更新中)

版权声明:原创博客未经允许请勿转载! https://blog.csdn.net/holmes_MX/article/details/82594604

0. 写作目的

好记性不如烂笔头。

1. list使用技巧

1.1 对list的进行排序

import operator

a = [[1, 1, 1, 1, 0.98], [2, 2, 2, 2, 0.99], [3, 3, 3, 3, 0.89]]

a.sort(key=operator.itemgetter(4), reverse=True)
## result:  [[2, 2, 2, 2, 0.99], [1, 1, 1, 1, 0.98], [3, 3, 3, 3, 0.89]]

a.sort(key=operator.itemgetter(4), reverse=False)
## result: [[3, 3, 3, 3, 0.89], [1, 1, 1, 1, 0.98], [2, 2, 2, 2, 0.99]]

1.2 如果对list中的两个属性进行排序

import operator

a = [[1,1,1,1,'a',0.98], [2,2,2,2,'b', 0.96], [3,3,3,3,'a', 0.99], [4, 4,4,4,'b', 0.94]]

## first sort according x[4] 
## then sort according x[5] based on x[4]
a.sort(key=lambda x:(x[4], x[5]), reverse=True)
## result: 
#[[2, 2, 2, 2, 'b', 0.96], [4, 4, 4, 4, 'b', 0.94], [3, 3, 3, 3, 'a', 0.99], [1, 1, 1, 1, #'a', 0.98]]

1.3 列表推导式的书写

a = [1.0, 0.3, 0.4, 0.4, 0]

## 输出 > 0 为1, 其余为0
b = [1 if x > 0 else 0 for x in a]

## output: [1 1 1 1 0]

2 有关json的读写

## read json

import json

targetData = [1,2, 3,4]
##targetData = [ {'1': 1}, {'2':2} ]

with open('jsonData.json', 'w') as f:
    json.dump( targetData, f )



## load json data

import json

dataDir = 'jsonData.json'
with open(dataDir, 'r') as f:
    loadData = json.load( f )

3. 有关CSV文件的操作(pandas)

3.1 列数较少时,csv文件的写

### write data
import pandas as pd

a = [1, 1, 1]
b = ['2', '2', '2']
c = ['ds', 'asdf', 'afgew']

dataPD = pd.DataFrame( {'column1': a, 'column2':b, 'column3': c} )
dataPD.to_csv('writeCSV.csv', index = False)



3.2 列数较多时,csv文件的写

import pandas as pd

dataList = [['row1', 1, 2, 3, 'featuredata-4'], ['row2', 2,3, 4, 'featuredata-4']]

nameList = ['column1', 'column2', 'column3', 'column4']

dataPD = pd.DataFrame( dataList, columns = nameList )
dataPD.to_csv( 'writeCSV.csv', index=False)

3.3 csv文件的读取与部分操作

import pandas as pd

csvDir = 'writeCSV.csv'

dataPD = pd.read_csv( csvDir, encoding='utf-8' )

## return the shape of dataPD: turple type 
dataPD.shape

## the first row data
temp = dataPD.loc[0,:]  ## this data including the column-name
temp = dataPD.loc[0]    ## this data including the column-name
## visit each data in this row
for ii in range(temp.shape[0]):
    rowData = temp[ii]

## return the column data
dataPD['column-name'] ## return a Serise

(dataPD['column-name']).values ## return a list for this column

 

[Reference]

[1] json读写:https://www.cnblogs.com/bigberg/p/6430095.html

猜你喜欢

转载自blog.csdn.net/holmes_MX/article/details/82594604