python 需求清单_Python中的清单

python 需求清单

Introduction of Lists in Python:

Python中列表的介绍:

There are six built-in types of sequences. Lists and tuples are the most common one. In this article we will see how to use Lists in python and how we can utilize it while doing our own program. In general we can also say like the place holder where a number of elements can be stored in a given order. 

有六种内置的序列类型。 列表和元组是最常见的。 在本文中,我们将看到如何在python中使用Lists以及如何在执行自己的程序时利用它。 通常,我们也可以说就像占位符一样,可以按给定顺序存储许多元素。

We can use lists for indexing , adding , multiplying or checking for the member of that sequence. We can also check the lenght by identifying the minimum and maximum elements present in the lists. 

我们可以使用列表来索引,添加,乘或检查该序列的成员。 我们还可以通过确定列表中存在的最小和最大元素来检查长度。

Lists are called as the compound data types of Python. The lists are separated by commas and enclosed by square brackets i.e. [].

列表被称为Python的复合数据类型。 列表用逗号分隔,并用方括号括起来,即[]。

Lists are very similar to arrays in C programming language but the difference is that Python lists can be of different data types.

列表与C编程语言中的数组非常相似,但是区别在于Python列表可以具有不同的数据类型。

Slice operators are used to access the values from a list for example ( [] ] OR [] : ] ) . The indexes starts with zero i.e. "0" from beginning and if we starts the index from

切片运算符用于访问列表中的值,例如([]]或[]:])。 索引从零开始,即从零开始,如果我们从

the end then it starts with negative one i.e. "-1" . We can use a plus sign (+) for concatenate a list and an asterisk sign (*) to repeat the list.

然后从负数开始,即“ -1”。 我们可以使用加号(+)连接列表,并使用星号(*)重复列表。

Let's try to see how a simple list can look like :

让我们尝试看看一个简单的列表如何:

login as: sloba
sloba@*********'s password:
Welcome to Linux Mint 17.2 Rafaela (GNU/Linux 3.16.0-38-generic x86_64)

Welcome to Linux Mint
 * Documentation:  http://www.linuxmint.com
Last login: Mon Jul 13 14:18:53 2015 from 172.27.66.246
sloba@sloba-VirtualBox ~ $ python
Python 2.7.10 (default, Jul 13 2015, 14:15:32)
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> list =[1,2,3,4]
>>> list
[1, 2, 3, 4]
>>>

We can print the values by just typing the variable that was declared to create a list or we can use the print option to display the values as shown below:

我们可以通过键入声明为创建列表的变量来打印值,也可以使用print选项显示值,如下所示:

>>> print list
[1, 2, 3, 4]
>>>
>>>
sloba@sloba-VirtualBox ~ $ python
Python 2.7.10 (default, Jul 13 2015, 14:15:32)
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> list1 = [1, 2,A,B]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'A' is not defined
>>>

If we are using different data types then we should use a string within single quotes else we can end up with an error as above . So let’s try with some proper defined values within a list.

如果我们使用不同的数据类型,则应在单引号内使用字符串,否则可能会导致上述错误。 因此,让我们尝试使用列表中的一些正确定义的值。

sloba@sloba-VirtualBox ~ $ python
Python 2.7.10 (default, Jul 13 2015, 14:15:32)
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> list1 = [1, 2,A,B]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'A' is not defined
>>>
>>> list1 = [1, 2,'A','B']
>>> list1
[1, 2, 'A', 'B']
>>>

Now let's see how we can use concatenation of lists in Python with an example, from the above we have defined two lists such as "list" which

现在,让我们来看一下如何在Python中使用列表的连接以及一个示例,从上面我们定义了两个列表,例如“ list”,其中

is only having numbers and "list1" which is having numbers and strings.

仅具有数字,而“ list1”具有数字和字符串。

>>> list
[1, 2, 3, 4]
>>> list1
[1, 2, 'A', 'B']
>>> list + list1
[1, 2, 3, 4, 1, 2, 'A', 'B']
>>>

From the above example we can see that the plus symbol helps us to concatenate two different lists having different data types to join and display or print all the values.

从上面的示例中,我们可以看到加号帮助我们连接两个具有不同数据类型的不同列表,以合并并显示或打印所有值。

If we want to see certain values from the list we can use the index and list it out.

如果我们想从列表中看到某些值,我们可以使用索引并将其列出。

For example if we have the list called "indexlist" and having values like []1,2,3,'AA','BB', 4].

例如,如果我们有一个名为“ indexlist”的列表,并且具有类似[] 1,2,3,'AA','BB',4]的值。

And we want to pull the values starting from third value till end then we have to execute the something like below :

我们想要从第三个值开始将值拉到结束,然后我们必须执行以下操作:

>>> indexlist
[1, 2, 3, 'AA', 'BB', 4]
>>>
>>> indexlist[2:]
[3, 'AA', 'BB', 4]

Now say we want the values starting from second till fourth value then we have to execute the something like below:

现在说我们想要从第二个值到第四个值的值,然后我们必须执行以下操作:

>>> indexlist[1:4]
[2, 3, 'AA']
>>> indexlist[0]
1
>>>

Now if we want the values to be pulled from end till first then we have to write as shown in below exmaple :

现在,如果我们希望将值从末尾拉到第一个,那么我们必须编写如下所示的exmaple:

>>> reversedlist =  indexlist[::-1]
>>> reversedlist
[4, 'BB', 'AA', 3, 2, 1]

Another method to achieve the above list using a for loop as shown below :

另一种使用for循环来实现上述列表的方法,如下所示:

>>> reversedlist
[4, 'BB', 'AA', 3, 2, 1]
>>> reversedlist.reverse()
>>> for i in reversedlist : print i
...
1
2
3
AA
BB
4
>>> a =[1,2]
>>> a
[1, 2]
>>> a *2
[1, 2, 1, 2]

Updating list in Python :

更新Python中的列表:

We can update single or multiple values of the lists by using slice on the assignment operators.

我们可以通过使用赋值运算符上的slice来更新列表的单个或多个值。

Here is a very simple example for updating a existing lists:

这是更新现有列表的非常简单的示例:

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

We can also add values to existing lists for example we have a list called "a" now lets add "5" as value to the list . 

我们还可以将值添加到现有列表中,例如,我们有一个名为“ a”的列表,现在可以将“ 5”作为值添加到列表中。

>>> a
[4, 2, 3]
>>> a.append(5)
>>> a
[4, 2, 3, 5]
>>>

If we want to add within the list like to add multiple values to the list then we have to use extend not append [Append is only used for adding single value to a list].

如果要在列表中添加,例如要向列表中添加多个值,则必须使用扩展而不是追加[追加仅用于将单个值添加到列表中]。

For example :  

例如 :

>>> list
['A', 'B', 'C']
>>> list.extend(['D','E'])
>>> list
['A', 'B', 'C', 'D', 'E']
>>>

If add a values in between a list by using insert function but note insert is used to insert only single value to an existing list.  

如果使用插入功能在列表之间添加值,但注释插入仅用于将单个值插入现有列表。

>>> list
['A', 'B', 'C', 'D', 'E']
>>> list.insert(1,"insert")
>>> list
['A', 'insert', 'B', 'C', 'D', 'E']
>>>

Delete elements from List:

从列表中删除元素:

To delete an element from a list can used by using a function remove. 

要删除列表中的元素,可以使用功能remove。

>>> list
['A', 'insert', 'B', 'C', 'D', 'E']
>>> list.remove("insert")
>>> list
['A', 'B', 'C', 'D', 'E']
>>>

From the above example we can see that we have to pass the value which we want to remove from the list. 

从上面的示例中,我们可以看到我们必须传递要从列表中删除的值。

>>> list
['A', 'B', 'C', 'D', 'E']
>>>
>>>
>>> list.append('A')
>>> list
['A', 'B', 'C', 'D', 'E', 'A']
>>> list.remove("A")
>>> list
['B', 'C', 'D', 'E', 'A']
>>>

The above example shows that we have two identical value i.e. "A" and when we use remove , we could see the first occurrence is removed not the second one.

上面的示例显示我们有两个相同的值,即“ A”,当我们使用remove时,可以看到第一次出现的是被删除的,而不是第二次出现的。

If we want to remove the last element from the list then we can also make use of "pop" .

如果我们想从列表中删除最后一个元素,那么我们也可以使用“ pop”。

>>> list
['B', 'C', 'D', 'E', 'A']
>>> list.pop()
'A'
>>> list
['B', 'C', 'D', 'E']
>>>

Length of list :

清单长度:

We can also check the length of the list . For example: 

我们还可以检查列表的长度。 例如:

>>> list =['A', 'B', 'C', 'D', 'E']
>>> list
['A', 'B', 'C', 'D', 'E']
>>> len(list)
5
>>>

So there are five elements or values in our list which defined as shown in above example. If we want to count the number of times the element is repeated then we can use something like

因此,清单中有五个元素或值的定义如上例所示。 如果我们想计算元素重复的次数,则可以使用类似

below:

下面:

>>> list
['A', 'B', 'C', 'D', 'E']
>>> list.count('A')
1
>>>

To see the elements in reverse we can call "reverse" after the list as shown in below example :

要查看反向元素,我们可以在列表后调用“ reverse”,如下例所示:

>>> list
['A', 'B', 'C', 'D', 'E']
>>> list.reverse()
>>> list
['E', 'D', 'C', 'B', 'A']
>>>

From the above examples and details we will be able to use Python list to add and remove the elements from the sequence. 

从上面的示例和细节中,我们将能够使用Python列表在序列中添加和删除元素。

To know more about lists in Python can found from python.org the official website for Python language. 

要了解有关Python列表的更多信息,请访问Python语言的官方网站python.org

Thank you for reading my article. Please feel free to leave me some feedback or to suggest any future topics. 

感谢您阅读我的文章。 请随时给我一些反馈或建议任何将来的主题。

Please 'Vote this article as helpful' if you liked on the bug green button at the bottom of this article.

如果您喜欢本文底部的错误绿色按钮,请“将此文章评论为有帮助”。

Looking forward to hear from you - Swadhin Ray (Sloba) -( LinkedIn ) ( Twitter )

期待收到您的消息-Swadhin Ray(Sloba)-( LinkedIn )( Twitter

翻译自: https://www.experts-exchange.com/articles/18660/Lists-in-Python.html

python 需求清单

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值