数据类型
概述
- 数字,字符串,元祖是不可变数据类型(更改其值是在内存里开辟新的空间来存放新值,原内存地址里的值不变)
- 对于不可变数量类型:先进行修改操作再打印,否则会会返回None
- 列表,字典,集合是可变数据类型(在内存地址不变的基础上可以修改值)
- 字符串,列表,元祖都属于序列(都有索引,可以遍历,可以切片,可以拼接)
- python里括号使用总结
- 小括号(): 用于定义元组; 方法调用; print打印; 函数,如len()
- 中括号(): 用于定义列表; 字符串,列表,元组取下标; 字典取key
- 大括号(): 用于定义字典,集合; format格式化输出用于取代%s这种的占位符也是大括号
字符串str
索引与切片
a = "abcde"
print(a[0])
print(a[1])
print(a[-1])
a
b
e
a = "abcdefghijk"
print(a[0:2])
print(a[1:6])
print(a[0:-1])
print(a[0:])
print(a[:])
ab
bcdef
abcdefghij
abcdefghijk
按步长取
print(a[1:6:2])
print(a[::-1])
print(a[::-2])
bdf
kjihgfedcba
kigeca
shell
[root@server1 ~]
ab
[root@server1 ~]
bcdefg
[root@server1 ~]
ab
[root@server1 ~]
bcdefg
[root@server1 ~]
ab
[root@server1 ~]
bcdefg
[root@server1 ~]
ab
[root@server1 ~]
bcdefg
方法1:
for i in range(5):
print(a[i])
方法2
for i in a:
print(i)
打印字符及其索引
for index, i in enumerate(a):
print(index, i)
str1="abcde"
length=`echo $str1|wc -L`
for i in `seq $length`
do
echo $str1|cut -c$i
done
常见操作
str1 = "Hello, nice to meet you!"
print(len(str1))
print(str1.__len__())
print(str1.capitalize())
print(str1.title())
print(str1.upper())
print(str1.lower())
print(str1.swapcase())
print(str1.center(50, '*'))
print(str1.ljust(50, '*'))
print(str1.rjust(50, '*'))
print(str1.strip())
print(str1.lstrip())
print(str1.rstrip())
print(str1.endswith('you'))
print(str1.startswith('H'))
print(str1.count('l'))
print(str1.find('nice'))
print(str1.rfind('e'))
print(str1.index('nice'))
print(str1.rindex('e'))
print(isinstance(str1, int))
print(str1.isalnum())
print(str1.isalpha())
print(str1.isdigit())
print(str1.islower())
print(str1.isupper())
print(str1.isspace())
print(str1.replace('l', '*', 2))
以空格为分隔符
str1 = "Hello, nice to meet you!"
print(str1.split(' '))
print(str1.split(' ')[1])
['Hello,', 'nice', 'to', 'meet', 'you!']
nice
以:为分隔符
str2 = 'tcpdump:x:72:72::/:/sbin/nologin'
print(str2.split(':'))
['tcpdump', 'x', '72', '72', '', '/', '/sbin/nologin']
以换行符\n为分隔符
str3 = '''hi
girls
!'''
print(str3.splitlines())
print(str3.splitlines()[0:2])
['hi', 'girls', '!']
['hi', 'girls']
列表list
list1 = [1, 2, 'a', 'b', '中文']
print(list1)
print(type(list1))
print(len(list1))
print(list1[0])
print(list1[0:3])
for index, value in enumerate(list1):
print(index, value)
[1, 2, 'a', 'b', '中文']
<class 'list'>
5
1
[1, 2, 'a']
0 1
1 2
2 a
3 b
4 中文
按照索引增
list1.insert(1, "add")
追加(增加到最后)
list1.append("end")
迭代的增
list1.extend([5, 6, 'c'])
[1, 'add', 2, 'a', 'b', '中文']
[1, 'add', 2, 'a', 'b', '中文', 'end']
[1, 'add', 2, 'a', 'b', '中文', 'end', 5, 6, 'c']
按索引删(有返回值)
list1.pop(1)
print(list1)
按索引删(可切片,无返回值;不加索引,会删除整个列表)
del list1[1:3]
print(list1)
按元素删
list1.remove('c')
print(list1)
清空
list1.clear()
print(list1)
[1, 2, 'a', 'b', '中文', 'end', 5, 6, 'c']
[1, 'b', '中文', 'end', 5, 6, 'c']
[1, 'b', '中文', 'end', 5, 6]
[]
list1 = [1, 2, 'a', 'b', '中文']
反转列表
list1.reverse()
print(list1)
按索引修改
list1[0]='**'
print(list1)
['中文', 'b', 'a', 2, 1]
['**', 'b', 'a', 2, 1]
list1 = [1, 2, 'a', 'b', '中文']
统计元素在列表中出现的次数
print(list1.count('a'))
找出(第一个匹配)元素的索引
print(list1.index('b'))
1
3
元祖tuple
tuple1 = ("lisi", "zhangsan", "wanger", "mazi")
print(tuple1)
print(type(tuple1))
print(tuple1[2])
('lisi', 'zhangsan', 'wanger', 'mazi')
<class 'tuple'>
wanger
tuple2 = (["lisi", 1880], ["zhangsan", 2880], ["wanger", 2000], ["mazi", 2400])
print(tuple2[0][0])
print(tuple2[1][0:])
元祖不能修改,但元祖内嵌套的列表可以修改
tuple2[0][0] = "****"
print(tuple2)
lisi
['zhangsan', 2880]
(['****', 1880], ['zhangsan', 2880], ['wanger', 2000], ['mazi', 2400])
字典dict
dict1 = {
"name": "zhangsan",
"age": 18,
"sex": "男",
"hobby": "play"
}
print(dict1)
print(type(dict1))
print(len(dict1))
通过key查value
print(dict1['age'])
{'name': 'zhangsan', 'age': 18, 'sex': '男', 'hobby': 'play'}
<class 'dict'>
4
18
dict1["id"] = 3180906023
print(dict1)
{'name': 'zhangsan', 'age': 18, 'sex': '男', 'hobby': 'play', 'id': 3180906023}
dict1.pop('id')
print(dict1)
清空
dict1.clear()
print(dict1)
{'name': 'zhangsan', 'age': 18, 'sex': '男', 'hobby': 'play'}
{}
dict1 = {
"name": "zhangsan",
"age": 18,
"sex": "男",
"hobby": "play"
}
dict1['hobby'] = 'ball'
print(dict1)
{'name': 'zhangsan', 'age': 18, 'sex': '男', 'hobby': 'ball'}
循环遍历键
for i in dict1.keys():
print(i)
循环遍历值
for i in dict1.values():
print(i)
循环遍历键值对
for i in dict1.items():
print(i)
name
age
sex
hobby
zhangsan
18
男
play
('name', 'zhangsan')
('age', 18)
('sex', '男')
('hobby', 'play')
city={
"北京": {
"东城": "景点",
"朝阳": "娱乐",
"海淀": "大学",
},
"深圳": {
"罗湖": "老城区",
"南山": "IT男聚集",
"福田": "华强北",
},
"上海": {
"黄埔": "xxxx",
"徐汇": "xxxx",
"静安": "xxxx",
},
}
打印北京东城区的说明(也就是打印出"景点")
print(city['北京']['东城'])
修改北京东城区的说明,改为"故宫"
city['北京']['东城'] = '故宫'
print(city['北京']['东城'])
增加北京昌平区及其说明
city['北京']['昌平'] = '说明'
print(city['北京'])
修改北京海淀区的说明,将"大学"改为"清华","北大","北邮"三个学校的列表
city['北京']['海淀'] = ['清华', '北大', '北邮']
print(city['北京']['海淀'])
在大学列表里再加一个"北影"
city['北京']['海淀'].append('北影')
print(city['北京']['海淀'])
循环打印出北京的区名,并在前面显示序号(以1开始)
for index, i in enumerate(city['北京'].keys()):
print(index, i)
循环打印出北京海淀区的大学,并在前面显示序号(以1开始)
for index, i in enumerate(city['北京']['海淀']):
print(index, i)
集合set
set1 = {"zhangsan", "lisi", "wanger", "mazi"}
set2 = [1, 2, 2, 3, 3, 3]
print(set1)
print(set(set2))
{'mazi', 'zhangsan', 'lisi', 'wanger'}
{1, 2, 3}
set1 = {1, 3, 5, 7, 9}
set2 = {2, 3, 5, 8, 10}
交集
print(set1.intersection(set2))
并集
print(set1.union(set2))
补集
print(set1.difference(set2))
print(set2.difference(set1))
{3, 5}
{1, 2, 3, 5, 7, 8, 9, 10}
{1, 9, 7}
{8, 2, 10}
set1 = {"zhangsan", "lisi", "wanger", "mazi"}
set1.add("xiaoming")
print(set1)
{'wanger', 'mazi', 'lisi', 'zhangsan', 'xiaoming'}
set1 = {"zhangsan", "lisi", "wanger", "mazi"}
set1.remove("mazi")
{'lisi', 'zhangsan', 'wanger'}
|