python split()用法

split() 方法语法:

str.split(str="", num=string.count(str)).
 

参数说明:

            str:表示为分隔符,默认为空格,但是不能为空('')。若字符串中没有分隔符,则把整个字符串作为列表的一个元素

                 例如:s.split('')是不合法的表达
            num:表示分割次数。如果存在参数num,则仅分隔成 num+1 个子字符串,并且每一个子字符串可以赋给新的变量
            [n]:表示选取第n个分片
 

举例:

url = 'C://aaa/image/my.jpg'

filename = url.split('/')[-1]  #以‘/ ’为分割符,为字符串数组,以[-1]为索引,保留最后一段

print(filename)
 #-*- coding:utf-8 -*-

import math
import numpy as np
import pylab as pl

 #数据集:每三个是一组分别是西瓜的编号,密度,含糖量
data = """
1,0.697,0.46,2,0.774,0.376,3,0.634,0.264,4,0.608,0.318,5,0.556,0.215,
6,0.403,0.237,7,0.481,0.149,8,0.437,0.211,9,0.666,0.091,10,0.243,0.267,
11,0.245,0.057,12,0.343,0.099,13,0.639,0.161,14,0.657,0.198,15,0.36,0.37,
16,0.593,0.042,17,0.719,0.103,18,0.359,0.188,19,0.339,0.241,20,0.282,0.257,
21,0.748,0.232,22,0.714,0.346,23,0.483,0.312,24,0.478,0.437,25,0.525,0.369,
26,0.751,0.489,27,0.532,0.472,28,0.473,0.376,29,0.725,0.445,30,0.446,0.459"""

#数据处理 dataset是30个样本(密度,含糖量)的列表
a = data.split(',')

print(a)

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

                      split()当不带参数时以空格进行分割(不管几个空格全部砍掉),当代参数时,以该参数进行分割。

Python中的split()函数的用法_SailorMoon_sweet的博客-CSDN博客_python中的split函数的用法

你可能感兴趣的:(Python编程,python)