Python list (modify, add, delete, sort)

Python list (List)

Python basic data types are integers, floating point, boolean, string, which is the most basic data. In actual programming, we should always organize a collection of many basic data composed of these collections is organized differently: data structures, today talking about the data structure of the Python list (list). The data structure is obtained by combining some of the data, "complex" data type.

Python list (modify, add, delete, sort)

Python built-in data structures are:

  • List (list)
  • Tuple (tuple)
  • Dictionary (dict)
  • Collection (set)

In Python language, data structures, and the above four kinds of basic data types (integer, floating point, etc.) collectively referred to as "built-in type" (Built-in Types).

First, what is a list of

Python sequence is the most basic data structure. Each element in the sequence is assigned a number - its position, or index, the first index is 0, the second index is 1, and so on.

Python has a built-in type 6 sequences, but the most common are lists and tuples.

The sequence of operations can be carried out, including indexing, slicing, add, multiply, check members.

Further, Python already built determined sequence length and determining the maximum and minimum element method.

Python is a list of the most common types of data, it can appear as a comma-separated values ​​in square brackets.

Data items list need not have the same type

(1) Create a list, as long as the comma-delimited data items different brackets can be used. As follows:

# 定义列表
name = ['Tom','Jack','John']
pet = ['cat','dog','bird']

# 打印列表
print(name)
print(pet)

Python list (modify, add, delete, sort)

(2) to access a list element with index

The subscript index to access the values ​​in the list, you can also use the same form of brackets taken character, as follows:

# 通过索引读取列表中的元素,索引从0开始,-1代表最后一个元素

print(name[0])     #查看列表中第一个
print(pet[2])      #查看列表中第二个
print(name[-1])    #查看列表中最后一个
print(pet[-2])    #查看列表中倒数第二个
print (name[0:2])  #查看索引2之前的元素

Python list (modify, add, delete, sort)

Second, the basic operation

1, element by element cited acquired, modified

#修改列表的元素
name[1] = 'Sean'   #修改name列表,索引1的内容为Sean
print(name)        #打印列表

Python list (modify, add, delete, sort)

2, adding to the list of elements inside

Elements are added to the list of python inside there are three main methods:

(1)append()

append () for the operation of the main list is added last to achieve a particular element in the list, and to add an element only once, and only at the end of the list;

name.append(元素A)

# 在列表末尾添加新元素
name.append('Bob')
print(name)

Python list (modify, add, delete, sort)

(2)extend()

extend () for a list of the main operation to achieve is for the expansion and growth of a specific list, you can add one more element, but can only be added to the end of the list;

name.extend([元素A,元素B,……])

name.extend(['Xgp','Wsd'])
print(name)

Python list (modify, add, delete, sort)

(3)insert()

insert () operation for a list of specific elements are added mainly in the specific location you want to add a list of the more commonly used, the specific location here is the location where the index number of elements in the list, should be noted that this index number is starting from 0, not 1 of this need special attention.

pet.insert(A,元素B):表示在列表m里面的第A+1处加入元素B

# 在列表指定位置添加新元素
print(pet)
pet.insert(0,'penguin')   #在列表最前面添加数据
print(pet)
pet.insert(-2,'pig')      #在列表倒数第三个添加数据
print(pet)

Python list (modify, add, delete, sort)

3, deletion of some of the elements in the list;

And add an element python list before opposed the deletion of some elements of the list there are also three ways:

(1)del pet[n]

Its role is to delete the list inside the index number for the position of n elements to note here is that del is an operating statement.

del m[n]

# 根据索引从列表中删除元素
print(pet)
del pet[0]      #删除开头的元素
print (pet)

Python list (modify, add, delete, sort)

(2)pet.pop()

Its role is the last element of the list m of return, and are removed on the basis of

Temp=pet.pop() %这里temp就会直接等于吗列表里最后一个元素。

Print(pet) %这里再次输出m的时候已经是删掉最后一个元素的m列表

pop (): pop end of the list of elements

print(pet)
new_pet=pet.pop()
print(new_pet)

Python list (modify, add, delete, sort)

Pop elements specified location

# 弹出指定位置的元素
print(pet)
pet.pop(2)
print(pet)

Python list (modify, add, delete, sort)

(3)pet.remove()

M.remove off action is to remove certain elements inside the list m;

m.remove(元素A)

# 根据元素的值进行删除:remove()
print(pet)            #查看源列表
pet.remove('cat')     #删除cat
print(pet)            #打印列表

Python list (modify, add, delete, sort)

Third, ordering

(1) permanent sort (positive index, from start to finish)

sort () sort method: This function method to forward a list of contents sorted, the new sorted list will overwrite the original list (id unchanged), which is sort sorting method is to directly modify the original list list sorting method.

(2) temporary sort (positive index, from start to finish)

sorted () method: that you can retain the original list, but also to get the list has been sorted

(3) reverse order (from the end of head start to finish)

reverse reverse the sort list: is the order of elements in the original list from left to right away again without a parameter list will be collated. If you need to sort the list of parameters, we need to use another way to sort the list of sort positive sequence order.

(4) list length

len () method returns the number of list elements.

operating

# 定义列表:汽车的品牌
print('原始排序:')
brand = ['audi','bmw','toyota','luhu']
print(brand)

#临时排序
print('临时排序:')
print(sorted(brand))

# 永久排序: sort()
print('正序排序:')
brand.sort()
print(brand)

# 倒序排序
print('倒序排序:')
brand.sort(reverse=True)
print(brand)

# 获取列表长度
print('列表长度:')
print(len(brand))

Python list (modify, add, delete, sort)

Fourth, small practice

List exercise (a)

  • Define a list of names stored in five subjects
kemu = ['语文','数学','英语','地理','生物']
print(kemu)

Python list (modify, add, delete, sort)

  • New subjects (end new)
kemu.append('化学')
print(kemu)

Python list (modify, add, delete, sort)

  • Modify Account
kemu[2] = '计算机'
print(kemu)

Python list (modify, add, delete, sort)

  • Delete subjects, and at the time of printing the list of subjects, which can delete the account
print(kemu)
new_kemu=kemu.pop(3)
print(new_kemu)

Python list (modify, add, delete, sort)

  • Delete the first two subjects
kemu.pop(1)
print(kemu)

Python list (modify, add, delete, sort)

  • Specify the new location
kemu.insert(0,'科学')
print(kemu)

Python list (modify, add, delete, sort)

  • Delete the specified account name
kemu.remove('生物')
print(kemu)

Python list (modify, add, delete, sort)

A list of exercises (two)

  • The name is stored in five cities to the list, and ensures that the name is not in alphabetical order
city = ['北京','上海','广州','深圳','山西']
print(city)

Python list (modify, add, delete, sort)

  • Print out the original list of cities Information
print(city)

Python list (modify, add, delete, sort)

  • Use the order sorted () method to print a list of cities in alphabetical order, but do not modify the elements of the list
print(sorted(city))

Python list (modify, add, delete, sort)

  • Print the list, the name of the city confirmed the order has not been modified
print(city)

Python list (modify, add, delete, sort)

  • Use sort () method arrange a city name, modify the order to ensure permanent
city.sort()
print(city)

Python list (modify, add, delete, sort)

Guess you like

Origin blog.51cto.com/14320361/2477291