Home Backend Development Python Tutorial Python常用列表数据结构小结

Python常用列表数据结构小结

Jun 16, 2016 am 08:43 AM
python list data structure

本文汇总了Python列表list一些常用的对象方法,可供初学者参考或查询,具体如下:

1.list.append(x)

把元素x添加到列表的结尾,相当于a[len(a):] =[x],代码如下:

1

2

3

4

5

6

>>> a=[1,2,3,4,5]

>>> a

[1, 2, 3, 4, 5]

>>> a.append(-2)

>>> a

[1, 2, 3, 4, 5, -2]

Copy after login

2. list.extend(L)

将一个列表中的所有元素都添加到另一个列表中,相当于 a[len(a):] = L,代码如下:

1

2

3

4

5

6

7

8

>>> a

[1, 2, 3, 4, 5, -2]

>>> L=[5,9,7]

>>> L

[5, 9, 7]

>>> a.extend(L)

>>> a

[1, 2, 3, 4, 5, -2, 5, 9, 7]

Copy after login

3. list.insert(i,x)

将元素x,插到索引号i之前,代码如下:

1

2

3

4

5

6

7

8

>>> a

[1, 2, 3, 4, 5, -2, 5, 9, 7]

>>> a.insert(0,-3)

>>> a

[-3, 1, 2, 3, 4, 5, -2, 5, 9, 7]

>>> a.insert(len(a),10)

>>> a

[-3, 1, 2, 3, 4, 5, -2, 5, 9, 7, 10]

Copy after login

4. list.remove(x)

删除元素x(第一次出现的),代码如下:

1

2

3

4

5

6

7

8

>>> a

[-3, 1, 2, 3, 4, 5, -2, 5, 9, 7, 10]

>>> a.append(1)

>>> a

[-3, 1, 2, 3, 4, 5, -2, 5, 9, 7, 10, 1]

>>> a.remove(1)

>>> a

[-3, 2, 3, 4, 5, -2, 5, 9, 7, 10, 1]

Copy after login

5. list.count(x)

计算元素x出现的次数,代码如下:

1

2

3

4

>>> a

[-3, 2, 3, 4, 5, -2, 5, 9, 7, 10, 1]

>>> a.count(3)

1

Copy after login

6. list.sort()

对列表元素进行排序,代码如下:

1

2

3

>>> a.sort()

>>> a

[-3, -2, 1, 2, 3, 4, 5, 5, 7, 9, 10]

Copy after login

7. list.reverse()

倒排列表中元素,代码如下:

1

2

3

4

5

>>> a

[-3, -2, 1, 2, 3, 4, 5, 5, 7, 9, 10]

>>> a.reverse()

>>> a

[10, 9, 7, 5, 5, 4, 3, 2, 1, -2, -3]

Copy after login

8. list.index(x)

返回表中第一个出现值为x的索引,代码如下:

1

2

3

4

>>> a

[10, 9, 7, 5, 5, 4, 3, 2, 1, -2, -3]

>>> a.index(9)

1

Copy after login

9. list.pop(i)

从列表指定位置i删除元素,并将此元素返回,若未指定位置则删除列表最后一位元素,并将此元素返回。代码如下:

1

2

3

4

5

6

7

8

>>> a

[10, 9, 7, 5, 5, 4, 3, 2, 1, -2, -3]

>>> a.pop(0)

10

>>> a

[9, 7, 5, 5, 4, 3, 2, 1, -2, -3]

>>> a.pop()

-3

Copy after login
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? Apr 01, 2025 pm 11:15 PM

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

Can Python parameter annotations use strings? Can Python parameter annotations use strings? Apr 01, 2025 pm 08:39 PM

Alternative usage of Python parameter annotations In Python programming, parameter annotations are a very useful function that can help developers better understand and use functions...

How do Python scripts clear output to cursor position at a specific location? How do Python scripts clear output to cursor position at a specific location? Apr 01, 2025 pm 11:30 PM

How do Python scripts clear output to cursor position at a specific location? When writing Python scripts, it is common to clear the previous output to the cursor position...

Why can't my code get the data returned by the API? How to solve this problem? Why can't my code get the data returned by the API? How to solve this problem? Apr 01, 2025 pm 08:09 PM

Why can't my code get the data returned by the API? In programming, we often encounter the problem of returning null values ​​when API calls, which is not only confusing...

How does Uvicorn continuously listen for HTTP requests without serving_forever()? How does Uvicorn continuously listen for HTTP requests without serving_forever()? Apr 01, 2025 pm 10:51 PM

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

How to dynamically create an object through a string and call its methods in Python? How to dynamically create an object through a string and call its methods in Python? Apr 01, 2025 pm 11:18 PM

In Python, how to dynamically create an object through a string and call its methods? This is a common programming requirement, especially if it needs to be configured or run...

How to use Go or Rust to call Python scripts to achieve true parallel execution? How to use Go or Rust to call Python scripts to achieve true parallel execution? Apr 01, 2025 pm 11:39 PM

How to use Go or Rust to call Python scripts to achieve true parallel execution? Recently I've been using Python...

Where to download Python .whl files under Windows? Where to download Python .whl files under Windows? Apr 01, 2025 pm 08:18 PM

Python binary library (.whl) download method explores the difficulties many Python developers encounter when installing certain libraries on Windows systems. A common solution...

See all articles