Basic learning of Python3 lists (with examples)

不言
Release: 2018-12-30 10:13:11
forward
2400 people have browsed it

The content of this article is about the basic learning of Python3 lists (with examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Foreword: Long time no see, I suddenly realized that I haven’t blogged for a long time. Recently, I am obsessed with Python and I can’t help myself. I learned about it. Python is simple and easy to learn, especially for those who have been exposed to Java. Getting started with Python is even better. The threshold is extremely low. In line with the principle of learning and recording, recording while learning is conducive to sorting out the learning results and also conducive to later review. Therefore, the first blog on the Python learning journey today is purely a record.

The simple syntax definition will not be recorded. Starting from the data structure, the most important thing in the program is to operate the data. To learn a programming language, you must undoubtedly master its unique data structure. Start now with List.

List is a data structure that is frequently used in Python programming. It consists of a series of elements arranged in a specific order, represented by [], with commas separating the elements, similar to an array in Java. Lists are created to store data, are dynamic, and crud operations can be performed on the list at any time. Since lists contain multiple elements, they are usually named in the plural form, such as names, letters, etc.

Basic format

fruits = ["apple","bananer","oranger"]
print(fruits)
Copy after login

Accessing list elements

Like most programming languages, access to python list data is also obtained through indexing. The first element is obtained from Starting from 0, the last element index is the total data sum of the list minus one. python also provides another special syntax. You can directly use -1 as the index to get the last element, and then subtract one to get the data in reverse. After obtaining the list data, you can directly use it to perform any operation.

print(fruits[0])//获取第一个元素 `apple`
print(fruits[-1])//获取最后一个元素`oranger`
Copy after login

Modify list elements

Get the element at the corresponding position directly according to the index and reassign it.

fruits[0] = "watermelon"//修改第一个元素
print(fruits)//重新打印列表
Copy after login

At this time the list changes to: ['watermelon', 'bananer', 'oranger']

Add elements

  • Call append() Method adds elements at the end of the list

fruits.append("Plum")
print(fruits)
Copy after login

At this time the list is: ['apple', 'bananer', 'oranger', 'Plum']

  • Call the insert() method to insert data at the specified index

fruits.insert(1,"pear")//在索引为1处插入数据
print(fruits)
Copy after login

The list at this time is: ['apple', 'pear', 'bananer', 'oranger']

Delete data in the list

  • If you know the index, directly use del to delete the data

del fruits[0]//删除第一个数据
print(fruits)
Copy after login

The list at this time is: ['pear', 'bananer', 'oranger']

  • Call pop() to pop up the list data and return the pop-up data. If no parameters are passed, the last element of the list will pop up by default. If the index value is passed, the specified index element will pop up

print(fruits.pop())//弹出最后一个元素,并打印
print(fruits)
print(fruits.pop(0))//弹出第一个元素,并打印
print(fruits)
Copy after login

Note that the result is:

oranger
['pear', 'bananer']
pear
['bananer']
Copy after login
  • If you do not know the index of the element and know the specific element value to be deleted, you can also directly call the remove() method to delete it. Note that after deletion, you can continue to use the element.

fruits = ["apple","bananer","oranger","prea"]
print(fruits)
delete = "bananer"//删除的元素
fruits.remove(delete)//调用方法删除指定元素值
print(fruits)
print(delete)//最后打印删除掉的元素
Copy after login

The result is:

['apple', 'bananer', 'oranger', 'prea']
['apple', 'oranger', 'prea']
bananer
Copy after login

List sorting

  • Call sort() to sort the list elements. The default is Natural order sorting. If you want to sort in reverse, you can pass in the parameter reverse=True. After sorting, the order of the list will be permanently changed.

fruits = ["bananer","apple","oranger","prea"]
print(fruits)
fruits.sort()
print(fruits)
Copy after login

The result is:

['bananer', 'apple', 'oranger', 'prea']
['apple', 'bananer', 'oranger', 'prea']
Copy after login
  • If we want to temporarily change the order of the list, we can use the sorted() method

fruits = ["bananer","apple","oranger","prea"]
print(fruits)
print(sorted(fruits))
print(fruits)
Copy after login

The result is:

['bananer', 'apple', 'oranger', 'prea']
['apple', 'bananer', 'oranger', 'prea']
['bananer', 'apple', 'oranger', 'prea']
Copy after login

It can be seen that the order of the list has not changed.

Use the reserse() method to flip the list elements

  fruits = ["bananer","apple","oranger","prea"]
  print(fruits)
  fruits.reverse()//翻转列表元素
  print(fruits)
Copy after login

The result is:

['bananer', 'apple', 'oranger', 'prea']
['prea', 'oranger', 'apple', 'bananer']
Copy after login

Use the len() method to get the length of the list

fruits = ["bananer","apple","oranger","prea"]
print(len(fruits))
Copy after login

The result is obviously 4.

for loop traverses the list

This is similar to java, the format is for xxx in list name:, after traversing to obtain the list data, we can Perform any operation

fruits = ["bananer","apple","oranger","prea"]
for fruit in fruits:
    print(fruit)
Copy after login

The result is to traverse and print out each element:

bananer
apple
oranger
prea
Copy after login

Note: There are no {} in code blocks in Python, and 4 spaces are used to indent the code. This is true for blocks, for loops, if loops, while loops, and method bodies. When writing code, be sure to pay attention to indentation

Quickly assemble a number list

ranger() The starting value and ending value can generate a series of numbers in order, and then use list() to quickly assemble a list of numbers in any range

numbers = list(range(1,6))//组装列表
print(numbers)
Copy after login

Result: [1, 2, 3, 4, 5] In fact, use traversal It can also be implemented, but this method is more convenient.

List generation formula

Use [expression for variable value in range(x,x) if xxx] to quickly generate a numerical list with just one statement, where the expression is the result of traversing the numerical value To operate, you can also add if conditions.

numbers = [x * x for x in range(1,6)]//求平方数值列表
print(numbers)
Copy after login

The result is: [1, 4, 9, 16, 25] This expression is quite concise, and the original several lines can be solved in one line.

使用切片裁剪获取子列表

使用 列表名[x:y] 裁剪获取对应索引区间的子列。假如省略起始值x,默认从0索引开始裁剪,假如省略结束值y,默认裁剪余下的所有元素。

fruits = ["bananer","apple","oranger","prea"]
print(fruits[0:2])
Copy after login

结果为:['bananer', 'apple']

至此,基本的列表操作差不多都总结完了,顺便记录一下元组

  • 列表中的数据是可变的,我们经常用它来存储可变的数据源

  • 元组跟列表一样也是用来存储数据源的,但是它存储的数据源是不可变的

  • 元组定义的格式也不一样,为(xxx,yyy,zzz).

  • 对于元组的操作,跟列表一样

The above is the detailed content of Basic learning of Python3 lists (with examples). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template