python初学者列表

Lists are the single most used data structure in Python. It is crucial that you learn it well, as lists are used pretty much everywhere in Python programs that you will come across. If there is a topic in Python you will want to spend time on, List is the ONE

列表是Python中最常用的数据结构。 掌握列表至关重要,因为您将在Python程序中几乎无处不在使用列表。 如果Python中有一个主题,您将需要花费时间,列表就是一个

什么是清单?(What is a list?)

A list is a special data structure in Python that is used to store collections in a determined order. A list can change over time(mutable). Python lists are written using square brackets notation. Like everything else in Python, a list is an object.

列表是Python中的一种特殊数据结构,用于以确定的顺序存储集合。 列表可以随时间变化(可变)。 Python列表使用方括号表示法编写。 像Python中的其他所有内容一样,列表是一个对象。

如何建立清单 (How to create a list)

Let’s look at some examples on how you can define a list

让我们看一些有关如何定义列表的示例

Creates a list of integers/numbers:

创建一个整数/数字列表:

list1 = [1,2,3,4]

Creates a list of strings:

创建一个字符串列表:

list2 = [“one”,”two”,”three”]

Creates a list of strings/integers:

创建一个字符串/整数列表:

list3 = [“one”, 1, “two”, 2, “three”, 3]

Creates a list of lists:

创建列表列表:

list4 = [["one","two","three","four"],[1,2,3,4,5]]

As you probably have realized by now, a list in python is a very flexible data type. Unlike more strict languages Python allows free for all with lists.

您可能已经意识到,python中的列表是一种非常灵活的数据类型。 与更严格的语言不同,Python允许所有人免费使用列表。

如何访问列表中的元素 (How to access elements in a list)

When you create a list in Python, elements in the list are numbered starting from index 0. To access elements in a list you can use the index if you know where your elements are located. For example:

在Python中创建列表时,列表中的元素从索引0开始编号。如果知道元素的位置,则可以使用索引来访问列表中的元素。 例如:

element = list3[0]
print(element)
>one

Or, if you want the last element:

或者,如果您想要最后一个元素:

element = list3[5]
print(element)
>3

If you want to get the last element without counting(can get quite tedious), you can also do:

如果您想不计入最后一个元素(可能会非常乏味),也可以执行以下操作:

element = list4[-1]
print(element)
>3

Nice isn’t it?

很好,不是吗?

You could also want the second last item and so on:

您可能还需要倒数第二个,依此类推:

element = list5[-2]
print(element)
>three

You can do so much more useful things with Lists, using slicing. Like the name suggests you can take slices out of the list using a special pythonic notation. You got to love it!

使用切片,您可以使用列表做更多有用的事情。 顾名思义,您可以使用特殊的pythonic表示法从列表中删除片段。 你一定喜欢它!

Without wanting to steal from our next lesson, let me just give you an example:

不想从我们的下一堂课中窃取,让我举一个例子:

elements = list1[1:3]
print(elements)
>[2,3]

Using this type of notation you need to specify the start index and the stop index. The result will be a sublist containing elements starting with index start and ending with index stop -1.

使用这种表示法时,您需要指定起始索引和终止索引。 结果将是一个子列表,其中包含以索引start开头和索引stop -1结束的元素

Rest assured that we will dedicate a lesson to slicing as it deserves.

请放心,我们将竭尽全力为您提供切片服务。

如何替换列表中的元素 (How to replace Elements in a List)

You can change the elements in a list easily.

您可以轻松更改列表中的元素。

For example

例如

list1[0]="one"
print(list1)
>["one",2,3,4]

You can replace more than one element at the same time as well

您也可以同时替换多个元素

list2[0:2]=[1,2]
print(list2)
>[1,2,"three"]

如何将项目添加到列表 (How To Add Items to a List)

To add items to a list, you can use the append() method

要将项目添加到列表中,可以使用append()方法

list2.append("four")
print(list2)
>[1,2,"three","four"]

Likewise, you can remove elements from a list using the remove() method

同样,您可以使用remove()方法从列表中删除元素

list2.remove("four")

The remove method removes the first occurrence of the element given as a parameter from the list.

remove方法从列表中删除首次出现的作为参数给出的元素。

There are other ways in which you can delete elements from a list. For example the pop() method or the del() method. At this point it will be a good exercise to consult the python API documentation as you can see a comprehensive list of methods that you can use in a List.

您还可以通过其他方式从列表中删除元素。 例如pop()方法或del()方法。 此时,这是一个很好的练习,请查阅python API文档,因为您会看到可以在List中使用的方法的完整列表。

如何遍历列表中的元素 (How to loop over elements in a List)

Now, we get to the meat of lists. Lists are especially useful for looping. You can iterate over a list in a variety of ways.

现在,我们来看看清单的内容。 列表对于循环特别有用。 您可以通过多种方式遍历列表。

Here is the simplest:

这是最简单的:

list=[1,2,3,4,5]
for element in list:
print(element)>1
>2
>3
>4
>5

Also, you can iterate over lists using List Comprehensions.

另外,您可以使用列表推导遍历列表。

list=[1,2,3,4,5]
list_comprehension = [ element + "," for element in list ]
print(list_compreheension)

We will cover List comprehensions in a separate lesson!

我们将在单独的课程中介绍列表理解

There is so much more to learn about lists. So don’t stop here in your reading about Python lists.

关于列表,还有很多要学习。 因此,不要在您对Python列表的阅读中停下来。

翻译自: https://medium.com/@armindo_93441/python-for-beginners-lists-998fc0e69704

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值