欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python列表list的split()用法详解

程序员文章站 2024-01-13 11:33:58
Python列表list的split() 方法: split()函数 语法:str.split(str=" ",num=string.count(str))...

Python列表list的split() 方法:

split()函数

语法:str.split(str=" ",num=string.count(str))[n]

参数说明:

str: 表示为分隔符,默认为空格,但是不能为空( ”)。若字符串中没有分隔符,则把整个字符串作为列表的一个元素 num:表示分割次数。如果存在参数num,则仅分隔成 num+1 个子字符串,并且每一个子字符串可以赋给新的变量 [n]: 表示选取第n个分片

注意:当使用空格作为分隔符时,对于中间为空的项会自动忽略


str="hello boy<[www.doiido.com]>byebye"

print(str.split("[")[1].split("]")[0])
www.doiido.com

print(str.split("[")[1].split("]")[0].split("."))
['www', 'doiido', 'com']

一个简单的Example:

目的是在混杂有文本和数字的txt文件中读取数字,作图。(其实是在用sklearn做神经网络训练过程的一个输出)

import pandas as pd
import numpy as np
data=pd.read_table('learning.txt',header=None)
data = data.drop([i for i in range(1,254,2)])
data=np.array(data[0]).tolist()
x=[]
y=[]
x_=[]
y_=[]
for i in data:
    a=i.split(',',1)[0]
    b=i.split(',',1)[1]
    x_.append(a)
    y_.append(b)

for i in y_:
    a=i.split(' ',-1)[-1]
    y.append(a)
for i in x_:
    a=i.split(' ',-1)[-1]
    x.append(a)
x=[int(i) for i in x]
y=[float(i) for i in y]

import matplotlib.pyplot as plt
plt.figure(figsize=(16,9))
ax=plt.gca()
plt.plot(x, y)
ax.tick_params(labelcolor='k', labelsize='20', width=3)
plt.legend(labels=['learning curve'],loc=0,prop={'size': 20})
ax.set_xlabel('Iterations', size='20')
ax.set_ylabel('Loss', size='20')
plt.show()

learning.txt 文件内容:

Iteration 1, loss = 1.07707919

Validation score: -0.759675

Iteration 2, loss = 0.86785333

Validation score: -0.382800

Iteration 3, loss = 0.65217935

Validation score: -0.036673

Iteration 4, loss = 0.47041240

Validation score: 0.245380

Iteration 5, loss = 0.33352409

Validation score: 0.448944

Iteration 6, loss = 0.24185446

Validation score: 0.586900

Iteration 7, loss = 0.18681933

Validation score: 0.670878

Iteration 8, loss = 0.15894719

Validation score: 0.712551

Iteration 9, loss = 0.14840941

Validation score: 0.730509

Iteration 10, loss = 0.14607523

Validation score: 0.740826

Iteration 11, loss = 0.14493495

Validation score: 0.751460

Iteration 12, loss = 0.14051347

Validation score: 0.765123

Iteration 13, loss = 0.13127751

Validation score: 0.783078

Iteration 14, loss = 0.11785379

Validation score: 0.803541

Iteration 15, loss = 0.10211765

Validation score: 0.823697

Iteration 16, loss = 0.08606523

Validation score: 0.842771

Iteration 17, loss = 0.07125665

Validation score: 0.859080

Iteration 18, loss = 0.05864895

Validation score: 0.871903

Iteration 19, loss = 0.04862303

Validation score: 0.881497

Iteration 20, loss = 0.04109700

Validation score: 0.888700

Iteration 21, loss = 0.03573981

Validation score: 0.894126

Iteration 22, loss = 0.03191231

Validation score: 0.898618

Iteration 23, loss = 0.02912329

Validation score: 0.902683

Iteration 24, loss = 0.02703483

Validation score: 0.906685

Iteration 25, loss = 0.02530589

Validation score: 0.910762

Iteration 26, loss = 0.02365941

Validation score: 0.914878

Iteration 27, loss = 0.02202639

Validation score: 0.919035

Iteration 28, loss = 0.02040115

Validation score: 0.923112

Iteration 29, loss = 0.01878876

Validation score: 0.927080

Iteration 30, loss = 0.01724594

Validation score: 0.930809

Iteration 31, loss = 0.01582957

Validation score: 0.934184

Iteration 32, loss = 0.01462510

Validation score: 0.936995

Iteration 33, loss = 0.01359889

Validation score: 0.939518

Iteration 34, loss = 0.01274997

Validation score: 0.941784

Iteration 35, loss = 0.01205514

Validation score: 0.943816

Iteration 36, loss = 0.01148151

Validation score: 0.945663

Iteration 37, loss = 0.01098952

Validation score: 0.947369

Iteration 38, loss = 0.01054425

Validation score: 0.948967

Iteration 39, loss = 0.01012376

Validation score: 0.950490

Iteration 40, loss = 0.00970200

Validation score: 0.951954

Iteration 41, loss = 0.00926981

Validation score: 0.953357

Iteration 42, loss = 0.00882367

Validation score: 0.954697

Iteration 43, loss = 0.00837260

Validation score: 0.955966

Iteration 44, loss = 0.00792918

Validation score: 0.957140

Iteration 45, loss = 0.00750475

Validation score: 0.958220

Iteration 46, loss = 0.00710750

Validation score: 0.959206

Iteration 47, loss = 0.00674201

Validation score: 0.960103

Iteration 48, loss = 0.00641247

Validation score: 0.960921

Iteration 49, loss = 0.00611715

Validation score: 0.961682

Iteration 50, loss = 0.00585395

Validation score: 0.962376

Iteration 51, loss = 0.00561830

Validation score: 0.963028

Iteration 52, loss = 0.00541014

Validation score: 0.963642

Iteration 53, loss = 0.00522108

Validation score: 0.964233

Iteration 54, loss = 0.00504609

Validation score: 0.964804

Iteration 55, loss = 0.00488272

Validation score: 0.965360

Iteration 56, loss = 0.00472770

Validation score: 0.965903

Iteration 57, loss = 0.00458014

Validation score: 0.966434

Iteration 58, loss = 0.00443943

Validation score: 0.966953

Iteration 59, loss = 0.00430580

Validation score: 0.967458

Iteration 60, loss = 0.00417995

Validation score: 0.967952

Iteration 61, loss = 0.00406131

Validation score: 0.968431

Iteration 62, loss = 0.00394928

Validation score: 0.968897

Iteration 63, loss = 0.00384553

Validation score: 0.969347

Iteration 64, loss = 0.00374897

Validation score: 0.969783

Iteration 65, loss = 0.00365821

Validation score: 0.970205

Iteration 66, loss = 0.00357151

Validation score: 0.970614

Iteration 67, loss = 0.00348867

Validation score: 0.971013

Iteration 68, loss = 0.00340915

Validation score: 0.971392

Iteration 69, loss = 0.00333351

Validation score: 0.971756

Iteration 70, loss = 0.00326102

Validation score: 0.972108

Iteration 71, loss = 0.00319100

Validation score: 0.972449

Iteration 72, loss = 0.00312323

Validation score: 0.972780

Iteration 73, loss = 0.00305912

Validation score: 0.973102

Iteration 74, loss = 0.00299757

Validation score: 0.973415

Iteration 75, loss = 0.00293900

Validation score: 0.973718

Iteration 76, loss = 0.00288266

Validation score: 0.974011

Iteration 77, loss = 0.00282841

Validation score: 0.974294

Iteration 78, loss = 0.00277636

Validation score: 0.974566

Iteration 79, loss = 0.00272618

Validation score: 0.974828

Iteration 80, loss = 0.00267802

Validation score: 0.975081

Iteration 81, loss = 0.00263166

Validation score: 0.975324

Iteration 82, loss = 0.00258695

Validation score: 0.975559

Iteration 83, loss = 0.00254460

Validation score: 0.975786

Iteration 84, loss = 0.00250360

Validation score: 0.976006

Iteration 85, loss = 0.00246383

Validation score: 0.976219

Iteration 86, loss = 0.00242525

Validation score: 0.976425

Iteration 87, loss = 0.00238777

Validation score: 0.976625

Iteration 88, loss = 0.00235142

Validation score: 0.976817

Iteration 89, loss = 0.00231608

Validation score: 0.977002

Iteration 90, loss = 0.00228174

Validation score: 0.977181

Iteration 91, loss = 0.00224860

Validation score: 0.977353

Iteration 92, loss = 0.00221649

Validation score: 0.977518

Iteration 93, loss = 0.00218531

Validation score: 0.977678

Iteration 94, loss = 0.00215517

Validation score: 0.977830

Iteration 95, loss = 0.00212549

Validation score: 0.977976

Iteration 96, loss = 0.00209643

Validation score: 0.978116

Iteration 97, loss = 0.00206800

Validation score: 0.978250

Iteration 98, loss = 0.00204020

Validation score: 0.978380

Iteration 99, loss = 0.00201333

Validation score: 0.978504

Iteration 100, loss = 0.00198714

Validation score: 0.978626

Iteration 101, loss = 0.00196159

Validation score: 0.978745

Iteration 102, loss = 0.00193675

Validation score: 0.978858

Iteration 103, loss = 0.00191242

Validation score: 0.978968

Iteration 104, loss = 0.00188890

Validation score: 0.979076

Iteration 105, loss = 0.00186591

Validation score: 0.979181

Iteration 106, loss = 0.00184362

Validation score: 0.979284

Iteration 107, loss = 0.00182207

Validation score: 0.979382

Iteration 108, loss = 0.00180092

Validation score: 0.979478

Iteration 109, loss = 0.00178045

Validation score: 0.979572

Iteration 110, loss = 0.00176040

Validation score: 0.979653

Iteration 111, loss = 0.00174355

Validation score: 0.979727

Iteration 112, loss = 0.00172821

Validation score: 0.979794

Iteration 113, loss = 0.00171420

Validation score: 0.979854

Iteration 114, loss = 0.00170190

Validation score: 0.979908

Iteration 115, loss = 0.00169088

Validation score: 0.979957

Iteration 116, loss = 0.00168098

Validation score: 0.980001

Iteration 117, loss = 0.00167224

Validation score: 0.980040

Iteration 118, loss = 0.00166443

Validation score: 0.980075

Iteration 119, loss = 0.00165744

Validation score: 0.980107

Iteration 120, loss = 0.00165120

Validation score: 0.980136

Iteration 121, loss = 0.00164562

Validation score: 0.980161

Iteration 122, loss = 0.00164062

Validation score: 0.980184

Iteration 123, loss = 0.00163614

Validation score: 0.980205

Iteration 124, loss = 0.00163212

Validation score: 0.980224

Iteration 125, loss = 0.00162852

Validation score: 0.980240

Iteration 126, loss = 0.00162528

Validation score: 0.980255

Iteration 127, loss = 0.00162238

Validation score: 0.980269