python 需求清单_Python中的清单

python 需求清单

In the last section, we discussed about strings and the various properties and functions of strings, like how it can be thought of as a collection of various characters just like a list. In other words, the list is an ordered set of values enclosed in square brackets [ ].

在上一节中,我们讨论了字符串以及字符串的各种属性和功能,例如如何将其视为类似于列表的各种字符的集合。 换句话说,列表是括在方括号[ ] 的值的有序集合

An important difference to be noted is that list is mutable, i.e. it's values can be modified.

要注意的一个重要区别是list可变的 ,即它的值可以修改。

As it is nothing but a set of values, we can use the index in square brackets [ ] to identify an value belonging to the list.

由于只不过是一组值,因此我们可以使用方括号[ ]index来标识属于该列表的值。

The values that make up a list are called its elements, and they can be of any type. We can also say that list data type is a container that holds a number of elements in a given order. For accessing an element in the list, indexing is used, which can be called from both forward and backward direction (similar to strings).

组成列表的值称为其元素 ,它们可以是任何类型。 我们也可以说list数据类型是一个容器,以给定的顺序包含许多元素。 为了访问列表中的元素,使用了索引,可以从正向和反向调用(类似于字符串)。

构造清单 (Constructing a List)

As already explained, the list is a collection of similar type of data. This data can be an integer, a float, a complex number, a string or any legitimate datatype in python. The list is always provided with some name, similar to a variable, and each element is accessed by their index number. Python has both forward and backwards index number, so for each element there exists one positive and one negative number to access that element (discussed in the next topic). While defining a list, we have to enclose each element of the list within square brackets, and each element is separated by commas.

如前所述,该list是相似类型数据的集合。 该数据可以是整数浮点数复数字符串或python中的任何合法数据类型。 列表总是提供一些名称,类似于变量,并且每个元素都通过其索引号进行访问。 Python具有向前和向后的索引号,因此每个元素都有一个正号和一个负号来访问该元素(在下一主题中讨论)。 定义列表时,我们必须将列表的每个元素括在方括号内,并且每个元素都用逗号分隔。

Suppose, if you want to create an empty list (which is possible in case you want to add the elements later in the program or when user gives the input), then you can initialize it by declaring empty square brackets,

假设,如果您要创建一个空列表(在以后要在程序中添加元素或当用户提供输入时可以这样做),则可以通过声明空方括号来对其进行初始化,

>>> myEmptyList = []

For example, list of some integers will be,

例如,一些整数的列表将是,

>>> myIntegerList = [9, 4, 3, 2, 8]

A list of float values,

浮点值列表,

>>> myFloatList = [2.0, 9.1, 5.9, 8.123432]

A list of characters,

字符列表,

>>> myCharList = ['p', 'y', 't', 'h', 'o', 'n']

A list of strings,

字符串列表,

>>> myStringList = ["Hello", "Python", "Ok done!"]

Live Example →

现场示例→

These were some basic methods to create a list. Now let's try some other methods.

这些是创建列表的一些基本方法。 现在让我们尝试其他方法。

从另一个列表派生 (Deriving from another List)

Suppose there is an existing list,

假设有一个现有列表,

>>> myList1 = ['first', 'second', 'third', 'fourth', 'fifth']

And now you want to create a new list which consists some or all the elements of myList1, then you can you do a direct assignment for complete copying of elements or use slicing, and take only some elements.

现在,您想创建一个包含myList1部分或全部元素的新列表,然后就可以直接分配元素以完成元素的完全复制或使用切片,并且仅采用某些元素。

For complete copying,

为了完成复制,

>>> myList2 = myList1

Use slicing, for copying only the first three elements,

使用切片,仅复制前三个元素,

>>> myList2 = myList1[0:3]

Live Example →

现场示例→

For copying last three elements,

要复制最后三个元素,

>>> myList2 = myList1[-3]

在列表中添加序列号 (Adding Serial Numbers in a List)

Suppose you want to add serial whole numbers (i.e., 0, 1, 2, 3, ...) into your list and just don't want to write all of them one by one, do not worry, there is a shortcut for doing this.

假设您要将序列号(即0、1、2、3,...)添加到列表中,只是不想一一写完,不用担心,这里有一个快捷方式这样做。

For storing 0 to (n-1) numbers in a list use range(n) function.

要在列表中存储0(n-1)数字,请使用range(n)函数。

>>> myList1 = range(9)

This will create a list with numbers 0 to 8 inside it.

这将创建一个列表,其中包含数字0到8

>>> myList1 = range(5, 9)

Live Example →

现场示例→

This will create a list having numbers 5, 6, 7, 8 i.e., argument's first number to the (argument's last number - 1).

这将创建一个具有数字5、6、7、8的列表即, 参数的第一个数字(参数的最后一个数字-1)

This range() function can be used for various usecases.

这个range()函数可以用于各种用例。

Suppose you want to create a list with squares of all whole numbers. Although there exists various programming approaches for this problem but here is a one line method in python. We will be introducing something new, so it can get a little tricky.

假设您要创建一个包含所有整数平方的列表。 尽管存在针对此问题的各种编程方法,但是这是python中的单行方法。 我们将介绍一些新内容,因此可能会有些棘手。

>>> myQuickList = [x**2 for x in range(5)]

The final list will contain elements like [0, 1, 4, 9, 16], i.e. x**2 where x is varying from 0 to (5-1).

最终列表将包含[0, 1, 4, 9, 16] 0,1,4,9,16]之类的元素,即x**2 ,其中x0(5-1)

for is the keyword here that you may not be familiar with. for is one of the keywords that makes programming do a lot of mundane and redundant task for a programmer, for example, it can create loops of execution. Although there will be a whole chapter dedicated to this, the important thing to note here is that for is the keyword which is responsible for iterating (or repeating) x in range 0 to 4 and finally storing the iterated value's square i.e. (x2) in the list.

for是您可能不熟悉的关键字。 for是使编程对程序员执行许多平凡和多余任务的关键字之一,例如,它可以创建执行循环。 尽管将有一整章专门讨论此问题,但此处要注意的重要一点是, for关键字负责迭代(或重复) 0到4之间的 x ,并最终存储迭代值的平方,即(x 2 )在列表中。

追加到清单 (Appending to a List)

Appending basically means adding to the existing. If you remember we told you how you can create an empty list in python by just declaring an empty square bracket. Now, what to do when you actually have to insert some elements inside the list? This is where append function comes in use. You can add any number of elements to the end of the list in one line without any hassle.

基本上,追加意味着增加现有的 。 如果您还记得,我们告诉过您如何通过仅声明一个空方括号来在python中创建一个空列表。 现在,当您实际上必须在列表中插入一些元素时该怎么办? 这是使用append功能的地方。 您可以在一行中将任意数量的元素添加到列表的末尾,而无需任何麻烦。

>>> emptyList = []
>>> emptyList.append('The Big Bang Theory')
>>> emptyList.append('F.R.I.E.N.D.S')
>>> emptyList.append('How I Met Your Mother')
>>> emptyList.append('Seinfeld')

Live Example →

现场示例→

So, that will basically create a list of some of the best sitcoms (and isn't empty anymore). If you print the list.

因此,这基本上将创建一些最佳情景喜剧的列表(并且不再是空的)。 如果您打印列表。

>>>print (emptyList)

['The Big Bang Theory', 'F.R.I.E.N.D.S', 'How I Met Your Mother', 'Seinfeld']

[“大爆炸理论”,“朋友”,“我如何遇见母亲”,“塞恩菲尔德”]

元素索引 (Indexing of elements )

We have already covered about using index to access the sequence of characters in a string in the string tutorial. Similarly, in python, index numbers can be used to access the elements of a list too. Let's create a list of strings and try to access individual strings using index numbers. Here is an example,

字符串教程中,我们已经介绍了如何使用索引访问字符串中的字符序列 。 同样,在python中,索引号也可以用于访问列表的元素。 让我们创建一个字符串 列表 ,并尝试使用索引号访问单个字符串。 这是一个例子

>>> fruitsList = ["Orange", "Mango", "Banana", "Cherry", "Blackberry", "Avocado", "Apple"]

As you can see in the code above, we have a list with 7 elements. To find the number of elements in a list, us the function len just like for strings.

如您在上面的代码中看到的,我们有一个包含7个元素的列表。 要查找列表中元素的数量,请使用函数len就像字符串一样。

>>> print (len(fruitsList));

7

7

Live Example →

现场示例→

For every element in the list, following are the index numbers that we should use: (try to understand how we assigned the index numbers.)

对于列表中的每个元素,以下是我们应该使用的索引号:(尝试了解我们如何分配索引号。)

ElementForward indexBackward index
Orange0-7
Mango1-6
Banana2-5
Cherry3-4
Blackberry4-3
Avocado5-2
Apple6-1
元件 前进指数 后退指数
橙子 0 -7
芒果 1个 -6
香蕉 2 -5
樱桃 3 -4
黑莓 4 -3
鳄梨 5 -2
苹果 6 -1

翻译自: https://www.studytonight.com/python/lists-in-python

python 需求清单

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值