python 技能清单_Python清单

python 技能清单

Today we going to learn about Python List. Earlier we learnt about Python Numbers which can be found here.

今天我们将学习Python列表。 之前我们了解了Python编号,可以在此处找到。

Python清单 (Python List)

List is a versatile datatype available in Python. Basically a python list is comma-separated values which are called items. List in python is written within square brackets. Interestingly it’s not necessary for items in a list to be of same types. For example;

List是Python中可用的通用数据类型。 基本上,python列表是逗号分隔的值,称为项。 python中的列表写在方括号内。 有趣的是,列表中的项目不必是相同类型的。 例如;

#an empty list
empty_list=[]

#a list of strings
str_list=['this', 'is', 'a', 'list']

# a list of integers
int_list=[1,2,3,4,5]

#a list of mixed type of items
mixed_list=['this', 1, 'is', 2, 'a', 3, 'mixed',4, 'list',5]

# to print the lists
print(empty_list)
print(str_list)
print(int_list)
print(mixed_list)

The above code will produce the following output.

上面的代码将产生以下输出。

在Python列表中访问项目 (Accessing Items in Python List)

Each item of a list is assigned a number – it’s position or index. The first index is zero, the second index is one, and so forth.

列表的每个项目都分配有一个数字-它是位置或索引。 第一个索引为零,第二个索引为1,依此类推。

To access items in a list, we can use these index number within a square bracket. For example;

要访问列表中的项目,我们可以在方括号内使用这些索引号。 例如;

#a list of strings
str_list=['this', 'is', 'a', 'list']

#to access first item
print(str_list[0])
#to access second item
print(str_list[1])
#to access 4th element
print(str_list[3])

The above code will produce output like below.

上面的代码将产生如下输出。

Surprising fact is index can be negative. It means to read not from the left rather from the right of the list.

令人惊讶的事实是指数可能为负。 这意味着不要从列表的左侧而是从列表的右侧读取。

#a list of strings
str_list=['this', 'is', 'a', 'list']

#third item from left
print(str_list[2])

#third item from right
print(str_list[-3])

The output of the above code will be like below-

上面代码的输出如下:

更新清单项目 (Update A List Item)

We can update one or more item of a list simply through the index of that item.

我们可以简单地通过该项目的索引来更新列表中的一个或多个项目。

#a list of strings
str_list=['this', 'is', 'a', 'list']

print("before updating the list: ")
print(str_list)
str_list[3]='updated list'
print("after updating the list: ")
print(str_list)

The output will be like below.

输出将如下所示。

删除列表中的项目 (Deleting an item in a list)

To delete an item in a list, there are several methods. Look at the following example to explore it further.

要删除列表中的项目,有几种方法。 请看以下示例,以进一步探索它。

#an empty list
empty_list=[]

#a list of strings
str_list=['this', 'is', 'a', 'list']

#to remove a specific element, like 'is'
str_list.remove('is')
print(str_list)

#to remove an item of a specific index like 2
del str_list[2]
print(str_list)

#there are yet another way to remove an item of a specific index
str_list.pop(0)
print(str_list)

The above code will produce output like below.

上面的代码将产生如下输出。

一些内置的python列表函数 (Some built-in python list functions)

There are some built-in functions to manipulate list in python. Let’s look at the following example for understanding.

有一些内置函数可以在python中操作列表。 让我们看下面的示例以进行理解。

#an empty list
empty_list=[]

#a list of strings
str_list=['this', 'is', 'a', 'list']

# add an element to the end of the list
str_list.append('appended')
print(str_list)

#insert an item at the defined index
str_list.insert(3,'inserted')
print(str_list)

#to get the index of the first matched item
print(str_list.index('a'))

#to count number of a specific element in a list
print(str_list.count('is'))

#to reverse the order of a list
str_list.reverse()
print(str_list)

#to sort the list in ascending order
str_list.sort()
print(str_list)

The output of the above code will be as follows.

上面代码的输出如下。

So this is all about python lists for now. Make sure to run every piece of code on you own. Feel free to leave a comment if you have any doubt.
#happy_coding 🙂

因此,这一切现在都与python列表有关。 确保自己运行每段代码。 如有任何疑问,请随时发表评论。
#happy_coding🙂

Reference: https://docs.python.org/3.6/tutorial/datastructures.html

参考: https : //docs.python.org/3.6/tutorial/datastructures.html

翻译自: https://www.journaldev.com/14353/python-list

python 技能清单

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值