Python字符串的截取

字符串元素的截取
Python中的字符串用单引号 ’ 或双引号 " 括起来,同时使用反斜杠 \ 转义特殊字符。

字符串的截取的语法格式如下:

变量[头下标:尾下标]

索引值以 0 为开始值,-1 为从末尾的开始位置。
在这里插入图片描述
加号 + 是字符串的连接符, 星号 * 表示复制当前字符串,紧跟的数字为复制的次数。实例如下:

#!/usr/bin/python3
 
str = 'Runoob'
 
print (str)          # 输出字符串
print (str[0:-1])    # 输出第一个到倒数第二个的所有字符
print (str[0])       # 输出字符串第一个字符
print (str[2:5])     # 输出从第三个开始到第五个的字符
print (str[2:])      # 输出从第三个开始的后的所有字符
print (str * 2)      # 输出字符串两次
print (str + "TEST") # 连接字符串

执行以上程序会输出如下结果:

Runoob
Runoo
R
noo
noob
RunoobRunoob
RunoobTEST

列表元素的截取
List(列表) 是 Python 中使用最频繁的数据类型。

列表可以完成大多数集合类的数据结构实现。列表中元素的类型可以不相同,它支持数字,字符串甚至可以包含列表(所谓嵌套)。

列表是写在方括号 [] 之间、用逗号分隔开的元素列表。

和字符串一样,列表同样可以被索引和截取,列表被截取后返回一个包含所需元素的新列表。

列表截取的语法格式如下:

变量[头下标:尾下标]

索引值以 0 为开始值,-1 为从末尾的开始位置。
在这里插入图片描述

#!/usr/bin/python3
 
list = [ 'abcd', 786 , 2.23, 'runoob', 70.2 ]
tinylist = [123, 'runoob']
 
print (list)            # 输出完整列表
print (list[0])         # 输出列表第一个元素
print (list[1:3])       # 从第二个开始输出到第三个元素
print (list[2:])        # 输出从第三个元素开始的所有元素
print (tinylist * 2)    # 输出两次列表
print (list + tinylist) # 连接列表

以上实例输出结果:

['abcd', 786, 2.23, 'runoob', 70.2]
abcd
[786, 2.23]
[2.23, 'runoob', 70.2]
[123, 'runoob', 123, 'runoob']
['abcd', 786, 2.23, 'runoob', 70.2, 123, 'runoob']

列表解析

列出1~10所有数字的平方
L=[]
for i in range(1,11):
    L.append(i**2)
print(L)
结果为:
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

语法:
[expression for iter_val in iterable]
[expression for iter_val in iterable if cond_expr]

L=[i**2 for i in range(1,11)]
print L
结果为:
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

map
例如:map函数。此时lambda函数用于指定对列表中每一个元素的共同操作。例如map(lambda x: x+1, [1, 2,3])将列表[1, 2, 3]中的元素分别加1,其结果[2, 3, 4]。

bob=['Bob Smith',42,30000,'software']
sue=['Sue Jones',45,40000,'hardware']
people=[bob,sue]
pays=map((lambda x:x[2]),people)
list(pays)

在循环中检查字段的名字得到我们所需要的。

bob=[['name','Bob Smith'],['age',42],['pay',10000]]
def field(record,label):
    for (fname,fvalue) in record:
        if fname==label:
            return fvalue
field(bob,'name')
结果为:
'Bob Smith'

字典
使用zip函数将名/值列表链在一起

names=['name','age','pay','job']
values=['Sue Jones',45,4000,'hdw']
list(zip(names,values))
结果为:
[('name', 'Sue Jones'), ('age', 45), ('pay', 4000), ('job', 'hdw')]

sue=dict(zip(names,values))
sue
结果为:
{'age': 45, 'job': 'hdw', 'name': 'Sue Jones', 'pay': 4000}

通过一个键序列和所有键的可选初始值来创建字典(便于初始化空字典)

fields=('name','age','job','pay')
record=dict.fromkeys(fields,'?')
record
结果为:
{'age': '?', 'job': '?', 'name': '?', 'pay': '?'}

map的具体用法:
通过Python中help可以查看map()的具体用法

help(map)

Help on built-in function map in module builtin:

map(…)
map(function, sequence[, sequence, …]) -> list

Return a list of the results of applying the function to the items of
the argument sequence(s).  If more than one sequence is given, the
function is called with an argument list consisting of the corresponding
item of each sequence, substituting None for missing values when not all
sequences have the same length.  If the function is None, return a list of
the items of the sequence (or a list of tuples if more than one sequence).

map接收一个函数和一个可迭代对象(如列表)作为参数,用函数处理每个元素,然后返回新的列表。
在这里插入图片描述
sum函数的具体用法:

help(sum)

Help on built-in function sum in module builtin:
sum(…)
sum(sequence[, start]) -> value

Return the sum of a sequence of numbers (NOT strings) plus the value
of parameter 'start' (which defaults to 0).  When the sequence is

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_36717487/article/details/89380291