python基础-列表、元组、range、列表字符串转换

        • 列表增删改查
        • 排序反转
        • 元组
        • range
        • range切片
        • 练习题
        • 列表拼接
        • 列表字符串转换

列表增删改查

list = [1,"a",True,(1,2,3),{"a":"1"}]
#遍历
for i in list:
    print(i)
#判断1是不是在list中
if 1 in list:
    print("----")


"""增"""

list.append("a")
print(list)

list.insert(0,"ba")
print(list)

#迭代插入
list.extend("cc")
print(list)

list.extend([1,"111"])
print(list)

"""删除"""
#按着索引去删除--有返回值,返回被删掉的元素
list.pop(0)
print(list)
#倒序删除
list.pop(-2)
print(list)

#按着元素删除
list.remove("a")
print(list)

#del按着索引删除
del list[0]
print(list)

#del删除切片可以有步长
del list[0:4:2]
print(list)

"""修改"""
list[0] = "nnn"
print(list)
#先删除,然后切片迭代修改添加  迭代
list[0:3] = "切片修改"
print(list)
#迭代添加
list[0:3] = ["asd","sss","ddd","er"]
print(list)

"""查询"""

"""跟字符串的切片方法一摸一样"""
#集合倒序打印
print(list[-1])
#集合切片(是列表)--字符串切片是字符串
print(list[0::2])
print(list[-1::-2])
print(list)
#循环查询
for a in list:
    print(a)
"""删除整个列表"""
list.clear()
print(list)

del list
print(list)

输出如下:

E:\python\python_sdk\python.exe E:/python/py_pro/safly.py
1
a
True
(1, 2, 3)
{'a': '1'}
----
[1, 'a', True, (1, 2, 3), {'a': '1'}, 'a']
['ba', 1, 'a', True, (1, 2, 3), {'a': '1'}, 'a']
['ba', 1, 'a', True, (1, 2, 3), {'a': '1'}, 'a', 'c', 'c']
['ba', 1, 'a', True, (1, 2, 3), {'a': '1'}, 'a', 'c', 'c', 1, '111']
[1, 'a', True, (1, 2, 3), {'a': '1'}, 'a', 'c', 'c', 1, '111']
[1, 'a', True, (1, 2, 3), {'a': '1'}, 'a', 'c', 'c', '111']
[1, True, (1, 2, 3), {'a': '1'}, 'a', 'c', 'c', '111']
[True, (1, 2, 3), {'a': '1'}, 'a', 'c', 'c', '111']
[(1, 2, 3), 'a', 'c', 'c', '111']
['nnn', 'a', 'c', 'c', '111']
['切', '片', '修', '改', 'c', '111']
['asd', 'sss', 'ddd', 'er', '改', 'c', '111']
111
['asd', 'ddd', '改', '111']
['111', '改', 'ddd', 'asd']
['asd', 'sss', 'ddd', 'er', '改', 'c', '111']
asd
sss
ddd
er
改
c
111
[]
<class 'list'>

Process finished with exit code 0

排序,反转

list = [1,"acb",True,(1,2,3,"acb"),{"a":"1"}]


count = list.count("acb")
print(count)

#正序排序
num = [-9,2,3,7,1,9,6,-2]
num.sort()
print(num)

#倒叙排序
num.sort(reverse=True)
print(num)

#反转
num.reverse()
print(num)

#查找某个元素的索引
print(list.index(1))

输出如下:

E:\python\python_sdk\python.exe E:/python/py_pro/safly.py
1
[-9, -2, 1, 2, 3, 6, 7, 9]
[9, 7, 6, 3, 2, 1, -2, -9]
[-9, -2, 1, 2, 3, 6, 7, 9]
0
Process finished with exit code 0

元组

list = (1,"acbdgg",True,(1,2,3,"acb"),{"a":"1"},[1,2,"sss"])

#只有查询方法,按着索引
print(list[1][2:4])
#切片出来也是元组
print(list[1:4])
#遍历
for i in list:
    print(i)

#孙子可以修改,但是list[3]是元组,不能修改,list[5]可以修改
list[5][2] = "aaaaa"
print(list)

输出如下:

E:\python\python_sdk\python.exe E:/python/py_pro/safly.py
bd
('acbdgg', True, (1, 2, 3, 'acb'))
1
acbdgg
True
(1, 2, 3, 'acb')
{'a': '1'}
[1, 2, 'sss']
(1, 'acbdgg', True, (1, 2, 3, 'acb'), {'a': '1'}, [1, 2, 'aaaaa'])

Process finished with exit code 0

range

#range先当于可迭代对象
for i in range(1,10,2):
    print(i)
print("-----------")

#含头不含尾,步长为1
for i in range(1, 10):
    print(i)
print("-----------")
for i in range(10,1,-2):
    print(i)

print("========嵌套循环输出所有的信息========")
li = [1,2,3,4,[1,2,"太白","alex"],2]

for i in range(0,len(li)):
    if i == 4:
        for j in li[i]:
            print(j)
    else:
        print(li[i])


输出如下:

E:\python\python_sdk\python.exe E:/python/py_pro/safly.py
1
3
5
7
9
-----------
1
2
3
4
5
6
7
8
9
-----------
10
8
6
4
2
=================
1
2
3
4
1
2
太白
alex
2

Process finished with exit code 0

range切片

# print(3>2>2)

l = range(10)

s = l[0:2]

for i in s:
    print(i)

print(l[-2])

输出如下:

0
1
8

练习题

5)通过对li列表的切片形成新的列表l5,l5 = [‘c’]
li = [1,3,2,’a’,4,’b’,5,’c’]


li = [1,3,2,'a',4,'b',5,'c']
#正正
print(li[len(li)-1::])

print(li[len(li)-1:len(li)-2:-1])

print(li[-1::1])

print(li[-1:-2:-1])
如下:

输出如下:

['c']
['c']
['c']
['c']

列表拼接

str1 = ["123","add"]
str2 = ["ee","gb",True]
#通过+方式进行列表的添加
newStr = str1 + str2
print(newStr)

#append添加
newStr.append("bgg")
print(newStr)
#extend添加
newStr.extend("nnn")
print(newStr)
#extend迭代添加
newStr.extend(["gjf","dert4"])
print(newStr)

#以下的方式不行
# newStr.append("fsf","af")
# print(newStr)

输出如下:

['123', 'add', 'ee', 'gb', True]
['123', 'add', 'ee', 'gb', True, 'bgg']
['123', 'add', 'ee', 'gb', True, 'bgg', 'n', 'n', 'n']
['123', 'add', 'ee', 'gb', True, 'bgg', 'n', 'n', 'n', 'gjf', 'dert4']

列表字符串转换

print("-------将str以*相连----")
s = "alex"
temp = "*".join(s)
print(temp)


print("-------list转str----")
listTemp = ["ad","gg","ee"]
temp = "".join(listTemp)
print(temp)

print("-------list转str str()----")
lis = str([2,3,'k'])
print(lis,type(lis))

for i in lis:
    print(i)

lis = lis.replace('k','100')
print(lis)


print("------将str ---> list   split-----")
s="wefjjlajoij"
temp = s.split("a")
print(temp)

print("------将str ---> list   list-----")
s = "abd"
aaa = list(s)
print(aaa)


输出如下:

E:\python\python_sdk\python.exe E:/python/py_pro/safly.py
-------将str以*相连----
a*l*e*x
-------list转str----
adggee
-------list转str str()----
[2, 3, 'k'] <class 'str'>
[
2
,

3
,

'
k
'
]
[2, 3, '100']
------将str ---> list   split-----
['wefjjl', 'joij']
------将str ---> list   list-----
['a', 'b', 'd']

你可能感兴趣的:(Python基础)