Python basics - list (list) and tuple (tuple)

Python contains 6 built-in sequences: lists, tuples, strings, Unicode strings, buffer objects, xrange objects, this article discusses lists and tuples.

1. The list can be modified, but the tuple cannot be modified.

2. In almost all cases, lists can replace tuples.

1. [List (list)]

#1. Grammar: Each element of the list is separated by "," and written in square brackets.

#Such as: use a list to represent the information of a person in the database: name, gender, age

zhangsan=['Zhang San','male',30]

lisi=['Li Si','female',28]

print(zhangsan)

print(type(zhangsan))

#Output: ['Zhang San', 'Male', 30]

# output: <class 'list'>

#2. Lists can contain other lists, such as:

zhangsan=['Zhang San','male',30]

lisi=['Li Si','female',28]

users=[zhangsan,lisi]

print(users)

#Output: [['Zhang San', 'Male', 30], ['Li Si', 'Female', 28]]

#3. All sequence types can be performed: indexing, slicing, addition, multiplication, etc., and built-in functions including checking whether an element exists, sequence length, maximum element, minimum element, etc.

#3.1 Index: The index is incremented from 0; when using a negative index, it will count from the right, and the position number of the last element on the right is -1, not 0

hello="HelloWorld"

print(hello[0]+"-"+hello[-1])

#Output: Hd

#3.2 Fragmentation: To access elements within a certain range through fragmentation, it is realized by two indexes separated by colons. Note: The element of the first index is included in the fragment, and the element of the second index is not included in the fragment

hello="SayHelloWorld"

print(hello[3:8])  

#Output: Hello

#Slice step: The default step is 1, and the slice traverses the sequence elements according to this step, and then returns all elements between the start and end points.

print(hello[3:8:2])  

# Output: Hlo

#3.2 Sequence addition: Only two sequences of the same type can be connected .

a=[1,2,3]

b=[4,5,6]

print(a+b)

#Output: [1, 2, 3, 4, 5, 6]

#3.3 Multiplication: Multiplying a sequence by a number x will result in a new sequence that repeats the elements of the original sequence x times

a=[1,2,3]

print(a*3)

#Output: [1, 2, 3, 1, 2, 3, 1, 2, 3]

#3.4 Check if an element exists: use the in operator

print("H" in hello)

# output: True

zhangsan=['Zhang San','male',30]

lisi=['Li Si','female',28]

users=[zhangsan,lisi]

print(['Zhang San','Male',30] in users)

# output: True

#3.5 len(), max(), min() functions

num=[100,34,678]

print(str(len(num))+"-"+str(max(num))+"-"+str(min(num)))

# output: 3-678-34

#4. List method (function)

#4.1 list() function : Because strings cannot be modified like lists, strings can be converted to lists

hello="HelloWorld"

hellolist=list(hello)

print(hellolist)

# Export: ['H', 'e', ​​'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd']

#On the contrary, to assemble the list into a string, you need to use the join function

hello2="".join(['H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd'])

print(hello2)

#Output: HelloWorld

#4.2 del delete element

names=["zhangsan","lisi","wangwu"]

del names[1]

print(names)

#Output: ['zhangsan', 'wangwu']

#4.3 append() method : append new elements at the end of the list

num=[1,2,3]

num.append(4)

print(number)

#Output: [1, 2, 3, 4]

#4.3 count() method : Count the number of times an element appears in the list

chars=['A','B','A','C','A']

print(chars.count("A"))

# output: 3

#4.4 extend() method : append multiple values ​​in another sequence at one time at the end of the list

#Note : The extend() method modifies the extended sequence, and a+b is the connection operation of two sequences, which returns a new sequence

a=[1,2,3]

b=[4,5,6]

c=a+b

a.extend(b)

print(a)

print(c)

#Output: [1, 2, 3, 4, 5, 6]

#Output: [1, 2, 3, 4, 5, 6]

#4.5 index() method : used to find the index position of the first match of a value in the list

chars=['A','B','A','C','A']

print(chars.index("C"))

# output: 3

#4.6 insert() method : used to insert objects into the list

a=[1,2,3]

a.insert(1,"A")

print(a)

#Output: [1, 'A', 2, 3]

#4.7 pop() method : removes an element in the list (default is the last one), and returns the value of the element concurrently

The #pop method is the only list method that can modify the list and return the element value (except None) (similar to a stack, last in first out)

a=[1,2,3]

b=a.pop(1)

print(a)

print(b)

#Output: [1, 3]

# output: 2

#4.8 remove() method : used to remove the first match at a position in the list

a=[1,2,3,'A','B','C']

a.remove('A')

print(a)

#Output: [1, 2, 3, 'B', 'C']

#4.9 reverse() method : used to store the elements in the list in reverse

a=[1,2,3]

a.reverse()

print(a)

#Output: [3, 2, 1]

#4.9 sort() method : used to sort the list in the original position, changing the original list instead of returning a new list

a=[1,7,4,5,3,6,2,8]

b=['E','B','D','F','C','A']

a.sort()

b.sort()

print(a)

print(b)

#Output: [1, 2, 3, 4, 5, 6, 7, 8]

#Output: ['A', 'B', 'C', 'D', 'E', 'F']

#If you just want to get the sorted list instead of modifying the original list, you can use the sorted() method

a=[1,7,4,5,3,6,2,8]

b=['E','B','D','F','C','A']

print(sorted(a))

print(sorted(b))

#Output: [1, 2, 3, 4, 5, 6, 7, 8]

#Output: ['A', 'B', 'C', 'D', 'E', 'F']

Two, tuple

#Tuple is also a sequence like a list, the only difference is that the tuple cannot be modified .

#1. Grammar: expand through (), separate with "," inside.

t=(1,2,3)

print(type(t))

# output: <class 'tuple'>

#2. tuple() function : It is basically the same as the list() function. It takes a sequence as a parameter and converts it into a tuple. If the parameter is a tuple, it returns as it is.

a=[1,2,3]

b=tuple(a)

print(b)

#Output: (1, 2, 3)

full code

#1.Python包含6种内建的序列:列表,元组,字符串,Unicode字符串,buffer对象,xrange对象,本文讨论列表和元组。
#2.列表可以修改,元组则不能修改。
#3.几乎在所有的情况下,列表都可以替代元组。

#【列表】
#1.语法:列表的各个元素用","分割,写在方括号中。,如:用列表表示数据库中一个人的信息:姓名,性别,年龄
zhangsan=['张三','男',30]
lisi=['李四','女',28]
print(zhangsan)
print(type(zhangsan))
#输出:['张三', '男', 30]
#输出:<class 'list'>

#2.序列可以包含其他序列,如:
zhangsan=['张三','男',30]
lisi=['李四','女',28]
users=[zhangsan,lisi]
print(users)
#输出:[['张三', '男', 30], ['李四', '女', 28]]

#3.所有序列类型都可以进行:索引,分片,加,乘等操作,以及包含检查元素是否存在,序列长度,最大元素,最小元素等内建函数。
#3.1 索引:索引从0开始递增,使用负数索引时,会从右边计数,右边最后一个元素位置编号是-1,不是0
hello="HelloWorld"
print(hello[0]+"-"+hello[-1]) 
#输出:H-o

#3.2 分片:通过分片来访问一定范围内的元素,通过冒号相隔的两个索引来实现。注意:第1个索引的元素包含在分片内,第2个索引的元素不包含在分片内
hello="SayHelloWorld"
print(hello[3:8])  
#输出:Hello
#分片步长:默认步长为1,分片按照这个步长遍历序列元素,然后返回开始和结束点之间的所有元素。
hello="SayHelloWorld"
print(hello[3:8:2])  
#输出:Hlo

#3.2 序列相加:两种相同类型的序列才能进行连接操作。
a=[1,2,3]
b=[4,5,6]
print(a+b)
#输出:[1, 2, 3, 4, 5, 6]

#3.3 乘法:用数字x乘以一个序列,会得到将原序列元素重复x次的新序列
a=[1,2,3]
print(a*3)
#输出:[1, 2, 3, 1, 2, 3, 1, 2, 3]

#3.4 检查元素是否存在:使用in运算符
print("H" in hello)
#输出:True
zhangsan=['张三','男',30]
lisi=['李四','女',28]
users=[zhangsan,lisi]
print(['张三','男',30] in users)
#输出:True

#3.5 len,max,min 函数
num=[100,34,678]
print(str(len(num))+"-"+str(max(num))+"-"+str(min(num)))
#输出:3-678-34

#4.列表方法(函数)
#4.1 list函数:因为字符串不能像列表一样被修改,可以将字符串转换为列表
hello="HelloWorld"
hellolist=list(hello)
print(hellolist)
#输出:['H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd']

#反之,将列表组装成字符串,需要用到join函数
hello2="".join(['H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd'])
print(hello2)
#输出:HelloWorld

#4.2 删除元素
names=["zhangsan","lisi","wangwu"]
del names[1]
print(names)
#输出:['zhangsan', 'wangwu']

#4.3 append()方法:在列表末尾追加新元素
num=[1,2,3]
num.append(4)
print(num)
#输出:[1, 2, 3, 4]

#4.3 count()方法:统计某个元素在列表中出现的次数
chars=['A','B','A','C','A']
print(chars.count("A"))
#输出:3

#4.4 extend()方法:在列表的末尾一次性追加另一个序列中的多个值
#注意:extend()方法修改了被扩展的序列,而a+b是两个序列的连接操作,它返回的是一个新的序列
a=[1,2,3]
b=[4,5,6]
c=a+b
a.extend(b)
print(a)
print(c)
#输出:[1, 2, 3, 4, 5, 6]
#输出:[1, 2, 3, 4, 5, 6]

#4.5 index()方法:用于在列表中找出某个值第一个匹配的索引位置
chars=['A','B','A','C','A']
print(chars.index("C"))
#输出:3

#4.6 insert()方法:用于将对象插入到列表中
a=[1,2,3]
a.insert(1,"A")
print(a)
#输出:[1, 'A', 2, 3]

#4.7 pop()方法:会移除列表中的一个元素(默认是最后一个),并发返回该元素的值
#pop方法是唯一一个既能修改列表,又能返回元素值(除了None)的列表方法(类似栈,后进先出)
a=[1,2,3]
b=a.pop(1)
print(a)
print(b)
#输出:[1, 3]
#输出:2

#4.8 remove()方法:用于移除列表中某个位置的第一个匹配项
a=[1,2,3,'A','B','C']
a.remove('A')
print(a)
#输出:[1, 2, 3, 'B', 'C']

#4.9 reverse()方法:用于将列表中的元素反向存放
a=[1,2,3]
a.reverse()
print(a)
#输出:[3, 2, 1]

#4.9 sort()方法:用于在原位置对列表进行排序,改变原来的列表,而不是返回新的列表
a=[1,7,4,5,3,6,2,8]
b=['E','B','D','F','C','A']
a.sort()
b.sort()
print(a)
print(b)
#输出:[1, 2, 3, 4, 5, 6, 7, 8]
#输出:['A', 'B', 'C', 'D', 'E', 'F']

#如果只是想获取排序后的列表,而不是修改原列表,可以使用sorted()方法
a=[1,7,4,5,3,6,2,8]
b=['E','B','D','F','C','A']
print(sorted(a))
print(sorted(b)) 
#输出:[1, 2, 3, 4, 5, 6, 7, 8]
#输出:['A', 'B', 'C', 'D', 'E', 'F']

#二、元组
#元组与列表一样,也是一种序列,唯一不同的是元组不能修改。
#1.语法:通过()扩起来,内部用","隔开。
t=(1,2,3)
print(type(t))
#输出:<class 'tuple'>

#2.tuple()函数:与list()函数基本上是一样的,以一个序列为参数,并把它转换为元组,如果参数是元组,则原样返回。
a=[1,2,3]
b=tuple(a)
print(b)
#输出:(1, 2, 3)










 

Guess you like

Origin blog.csdn.net/a497785609/article/details/131937521