Python 字符串、列表、元组、索引、切片

简介:

一、简要概述

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
what is list?
1、用[ 和 ]括起来,用逗号间隔每个数据项
2、数据项可是同类型数据也可以是不同类型数据(数字、字符串、浮点型)
3、list里面可以有list作为其数据项
4、数据项对应的位置为索引编号(index)。默认第一个是0
5、有序的数据集合
 
what is string?
1、用单引号、双引号、三引号引起来。
2、字符串是一个常量不可被修改,它的主要用途就是读其元素。
 
what is tuple?
1、元组和字符串均是不可被修改的。但是访问的方式都是一样的。
2、定义格式不一样:列表是用 "[]" ,字符串是用 "" "" ,元组是用 "()"
3、用途也不一样:元组用于多值传回(多值带入),列表用于数据的存储(因其可以被修改)
 
what is range?
1、range是python中的一个内建函数,它的返回值是一个list;默认步长是1。
 
what is index?
1、Python中所有的序列元素都是有编号的,从0开始递增,元素可以通过编号访问。
2、序列的第一个元素的索引始终是0.如果索引是负数呢?Python会从右边,也就是最后一个元素开始计数,最后一个元素的索引始终是-1
3、字符串字面值(其他序列字面值也如此)能直接使用索引,而不需要一个变量引用他们
4、如果一个函数返回一个序列,也可以对返回结果进行索引操作
5、索引的范围{ -len(str)到len(str)-1 }为闭区间

二、脚本案例测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env python
#coding:utf-8
 
#定义字符串变量和查找
print  "#定义字符串变量"
str =  "www.hytyi.com"
print  "str->pos-c->:" ,str.index( "c" )
print  "str->" ,str, "\n"
 
#定义空列表的方法
print  "#定义空列表的方法"
list01 = []
print  "list01->long:" ,len(list01)
print  "list01->type:" , type (list01)
 
list02 = list( "www.hytyi.com" )
print  "list02->:" ,list02
 
#将字符串转换成列表
print  "#将字符串转换成列表"
list03 = list(str)
print  "list03->:" ,list03
 
 
list04 = list03
print  "list04->:" ,list04
list04.reverse()
print  'list04<->' ,list04
print  "list04 long ->:" ,len(list04)
 
 
#将列表list04以逆序的方式赋值给list05的方式有以下4种:
print  "#将列表list04以倒叙的方式赋值给list05的方式有以下几种:"
list05 = []
i = 0
while  i < len(list04):
         list05.insert(0,list04[i])
         i = i + 1
else :
         print  "list05->1->:" ,list05
 
list05 = []
i = len(list04)-1
while  i >= 0:
         list05.append(list04[i])
         i = i - 1
else :
         print  "list05->2->:" ,list05
 
list05 = list04[::-1]
print  "list05->2->:" ,list05
 
list04.reverse()
list05 = list04
print  "list05->3->:" ,list05
 
 
#列表中的正方向切片
print  "#列表中的正方向切片,若只想要hytyi.其它的都不要,该如何实现呢?"
'' '
list = [ 'w' 'w' 'w' '.' 'h' 'y' 't' 'y' 'i' '.' 'c' 'o' 'm' ]
index =  0    1    2    3    4    5    6    7    8    9    10   11   12
'' '
print  "list02-long->" ,len(list02)
 
list06 = list02[4:10:1]
print  "list06->1->:" ,list06
 
list06 = list02[-9:-3:1]
print  "list06->2->:" ,list06
 
list06 = list02[4:-3:1]
print  "list06->3->:" ,list06
 
list06 = list02[-3:-9:-1]
print  "list06->4->No->No:" ,list06
 
 
#将一维列表的所有元组合并为一个字符串
print  "#将一维列表的所有元组合并为一个字符串"
'' '
list02 = [ 'w' 'w' 'w' '.' 'h' 'y' 't' 'y' 'i' '.' 'c' 'o' 'm' ]
str2 =  "www.hytyi.com"
'' '
 
print list02
import  itertools
str2 =  "" . join (itertools.chain(list02))
print  "str2->Now->:" ,str2
 
 
#如果列表中有重复的数据项,则将其删除,只保留第一个。
print  "#如果列表中有重复的数据项,则将其删除,只保留第一个。"
#方法[1]
list08 = list02 * 2
print   "" "count list08(" y ")->:" "" ,list08.count( "y" )
print   "list08->:" ,list08
 
s = list08.count( "y" )-1
i = 0
while  i < s:
         list08.remove( 'y' )
         i += 1
 
print list08.count( "y" )
print list08
 
#方法[2]
list09 = []
list10 = list02 * 2
 
col1 = len(list10)
print  "count list10->:" ,col1
 
col3 = list10.index( "y" )
col2 = 0
i = 0
while  i < col1:
         if  i == col3 or list10[i] !=  "y" :
                 list09.append(list10[i])
         col2 = col2 + 1
         i += 1
else :
         print  "count list09->:" ,len(list09)
         print  "list09->" ,list09
 
#方法[3]
list11 = list02 * 2
list12 = []
i = col4 = 0
while  i < col1:
         if  list11[i] ==  "y" :
                 if  col4 < 1:
                         list12.append(list11[i])
                 col4 += 1
         else :
                 list12.append(list11[i])
         i += 1
 
print  "count list012->:" ,len(list12)
print  "list12->" ,list12

三、测试结果展示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
root@python 20141107] # python list.py 
#定义字符串变量
str->pos-c->: 10
str-> www.hytyi.com 
 
#定义空列表的方法
list01->long: 0
list01-> type : < type  'list' >
list02->: [ 'w' 'w' 'w' '.' 'h' 'y' 't' 'y' 'i' '.' 'c' 'o' 'm' ]
#将字符串转换成列表
list03->: [ 'w' 'w' 'w' '.' 'h' 'y' 't' 'y' 'i' '.' 'c' 'o' 'm' ]
list04->: [ 'w' 'w' 'w' '.' 'h' 'y' 't' 'y' 'i' '.' 'c' 'o' 'm' ]
list04<-> [ 'm' 'o' 'c' '.' 'i' 'y' 't' 'y' 'h' '.' 'w' 'w' 'w' ]
list04 long ->: 13
#将列表list04以倒叙的方式赋值给list05的方式有以下几种:
list05->1->: [ 'w' 'w' 'w' '.' 'h' 'y' 't' 'y' 'i' '.' 'c' 'o' 'm' ]
list05->2->: [ 'w' 'w' 'w' '.' 'h' 'y' 't' 'y' 'i' '.' 'c' 'o' 'm' ]
list05->2->: [ 'w' 'w' 'w' '.' 'h' 'y' 't' 'y' 'i' '.' 'c' 'o' 'm' ]
list05->3->: [ 'w' 'w' 'w' '.' 'h' 'y' 't' 'y' 'i' '.' 'c' 'o' 'm' ]
#列表中的正方向切片,若只想要hytyi.其它的都不要,该如何实现呢?
list02-long-> 13
list06->1->: [ 'h' 'y' 't' 'y' 'i' '.' ]
list06->2->: [ 'h' 'y' 't' 'y' 'i' '.' ]
list06->3->: [ 'h' 'y' 't' 'y' 'i' '.' ]
list06->4->No->No: [ 'c' '.' 'i' 'y' 't' 'y' ]
#将一维列表的所有元组合并为一个字符串
[ 'w' 'w' 'w' '.' 'h' 'y' 't' 'y' 'i' '.' 'c' 'o' 'm' ]
str2->Now->: www.hytyi.com
#如果列表中有重复的数据项,则将其删除,只保留第一个。
count list08( "y" )->: 4
list08->: [ 'w' 'w' 'w' '.' 'h' 'y' 't' 'y' 'i' '.' 'c' 'o' 'm' 'w' 'w' 'w' '.' 'h' 'y' 't' 'y' 'i' '.' 'c' 'o' 'm' ]
1
[ 'w' 'w' 'w' '.' 'h' 't' 'i' '.' 'c' 'o' 'm' 'w' 'w' 'w' '.' 'h' 't' 'y' 'i' '.' 'c' 'o' 'm' ]
count list10->: 26
count list09->: 23
list09-> [ 'w' 'w' 'w' '.' 'h' 'y' 't' 'i' '.' 'c' 'o' 'm' 'w' 'w' 'w' '.' 'h' 't' 'i' '.' 'c' 'o' 'm' ]
count list012->: 23
list12-> [ 'w' 'w' 'w' '.' 'h' 'y' 't' 'i' '.' 'c' 'o' 'm' 'w' 'w' 'w' '.' 'h' 't' 'i' '.' 'c' 'o' 'm' ]







     本文转自zys467754239 51CTO博客,原文链接:http://blog.51cto.com/467754239/1574116 ,如需转载请自行联系原作者

相关文章
|
23天前
|
索引 Python 存储
Python 04 之变量【列表,元组,集合,字典,字符串】
Python 04 之变量【列表,元组,集合,字典,字符串】
53 0
Python 04 之变量【列表,元组,集合,字典,字符串】
|
3月前
|
Python
python元组切片
python元组切片
67 3
|
7月前
|
索引 Python 容器
Python序列操作指南:列表、字符串和元组的基本用法和操作
Python序列操作指南:列表、字符串和元组的基本用法和操作
125 0
|
7月前
|
索引 Python
python 中的列表,元组,字典,集合
python 中的列表,元组,字典,集合
75 1
|
8月前
|
存储 数据挖掘 Linux
Python学习笔记丨数据类型基础与易错点总结,列表、字典、集合、数值、字符串、元组
Python学习笔记丨数据类型基础与易错点总结,列表、字典、集合、数值、字符串、元组
|
9月前
|
索引 Python
【python】列表、字典、元组的相关操作
【python】列表、字典、元组的相关操作
|
10月前
|
索引 Python
11.从入门到精通:Python元组,访问,修改,删除,元组运算符,元组索引,截取,元组内置函数
11.从入门到精通:Python元组,访问,修改,删除,元组运算符,元组索引,截取,元组内置函数
|
11月前
|
存储 Python 容器
Python 空元组、空列表、空字典、空集合
Python 空元组、空列表、空字典、空集合
354 0
|
索引 Python
Python 编程 | 连载 09 - 列表、元组和字符串切片
Python 编程 | 连载 09 - 列表、元组和字符串切片
Python 编程 | 连载 09 - 列表、元组和字符串切片
|
存储 索引 Python
Python序列类型--列表、元组
列表类型是包含0个或多个元素的有序序列,属于序列类型。列表可以进行元素的增加、删除、替换、查找等操作。列表没有长度限制,无素类型可以不同,不需要预定长度。 。
Python序列类型--列表、元组