python中访问列表元素_在Python中访问列表及其元素

news/2024/7/19 12:00:44 标签: 列表, python, java, 数据结构, leetcode

python中访问列表元素

访问列表 (Accessing a list)

To access a list – we can simply print the list object and the complete list prints as an output.

要访问列表,我们可以简单地打印列表对象,然后将完整的列表作为输出打印。

    print (list_object)

Example:

例:

In this example, we will declare and assign the list, will print their types, and will print the list. To find the type of an object – we use type() method.

在此示例中,我们将声明并分配列表,打印其类型,并打印列表。 要找到对象的类型–我们使用type()方法 。

# declaring lists
list1 = [10, 20, 30, 40, 50, 10, 60, 10]
list2 = ["Hello", "IncludeHelp"]
list3 = ["Hello", 10, 20, "IncludeHelp"]

# printing the list and its elements
print("list1: ", list1)
print("list2: ", list2)
print("list3: ", list3)

# printing the types
print("Type of list1 object: ", type(list1))
print("Type of list2 object: ", type(list2))
print("Type of list3 object: ", type(list3))

Output

输出量

list1:  [10, 20, 30, 40, 50, 10, 60, 10]
list2:  ['Hello', 'IncludeHelp']
list3:  ['Hello', 10, 20, 'IncludeHelp']
Type of list1 object:  <class 'list'>
Type of list2 object:  <class 'list'>
Type of list3 object:  <class 'list'>

根据索引访问列表元素 (Accessing list elements based on the index)

To access list elements based on the given index – we simply pass the index starting from 0 to length-1 to access the particular element and we can also pass the negative index to access the list elements in the reverse order (-1 to access the last element, -2 to access the second last element, and so on...)

要基于给定的索引访问列表元素 –我们只需将索引从0开始传递到length-1即可访问特定元素,我们还可以传递负索引以相反的顺序访问列表元素( -1可以访问最后一个元素, -2访问第二个最后一个元素,依此类推...)

Syntax:

句法:

    list_object[index]

Example:

例:

# declaring lists
list1 = [10, 20, 30, 40, 50]

# Accessing the elements of a list by its index
print("list1[0]: ", list1[0])
print("list1[1]: ", list1[1])
print("list1[2]: ", list1[2])
print("list1[3]: ", list1[3])
print("list1[4]: ", list1[4])
print() # prints a new line 

# Accessing the elements of a list by its index
# in reverse order

print("list1[-1]: ", list1[-1])
print("list1[-2]: ", list1[-2])
print("list1[-3]: ", list1[-3])
print("list1[-4]: ", list1[-4])
print("list1[-5]: ", list1[-5])

Output

输出量

list1[0]:  10
list1[1]:  20
list1[2]:  30
list1[3]:  40
list1[4]:  50

list1[-1]:  50
list1[-2]:  40
list1[-3]:  30
list1[-4]:  20
list1[-5]:  10

使用列表切片访问元素 (Access elements using List slicing)

We can also access a set of elements by using list slicing by defining the start_index and end_index.

我们还可以通过定义start_indexend_index来使用列表切片来访问一组元素。

Syntax:

句法:

    list_object[[start]:[end])

Note: One of the values start or end may optional – consider the below example.

注意:startend可能是可选的-请考虑以下示例。

Example:

例:

# declaring lists
list1 = [10, 20, 30, 40, 50]

# printing list
print("list1: ", list1)

# printing elements using list slicing

# prints 5 elements from starting
print("list1[:5]: ", list1[:5])
# prints 3 elements from starting
print("list1[:3]: ", list1[:3])

# prints all elements from the index 0
print("list1[0:]: ", list1[0:])
# prints all elements from the index 3
print("list1[3:]: ", list1[3:])

# prints the elements between index 2 to 3
print("list1[2:3]: ", list1[2:3])
# prints the elements between index 0 to 4
print("list1[0:4]: ", list1[0:4])
# prints the elements between index 1 to 4
print("list1[1:4]: ", list1[1:4])

# prints elements in the reverse order
print("list1[ : : -1]: ", list1[ : : -1])

Output

输出量

list1:  [10, 20, 30, 40, 50]
list1[:5]:  [10, 20, 30, 40, 50]
list1[:3]:  [10, 20, 30]
list1[0:]:  [10, 20, 30, 40, 50]
list1[3:]:  [40, 50]
list1[2:3]:  [30]
list1[0:4]:  [10, 20, 30, 40]
list1[1:4]:  [20, 30, 40]
list1[ : : -1]:  [50, 40, 30, 20, 10]






翻译自: https://www.includehelp.com/python/accessing-a-list-and-its-elements.aspx

python中访问列表元素


http://www.niftyadmin.cn/n/1254566.html

相关文章

android 5.1 usb 摄像头,Android 5.1.1默认摄像头在捕获图像后返回onActivityResult中的空意图...

我有以下代码请求用户从照片应用程序中选择图像或通过相机应用程序捕获图像:// Camerafinal List cameraIntents new ArrayList();final Intent captureIntent new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);final PackageManager packageManager fragment…

需求分析心得

需求分析是业务和技术之间的桥梁&#xff0c;需求文档是一种对用户的承诺。在写需求文档的时候&#xff0c;就需要需求分析人员有相当的技术背景&#xff0c;了解每个需求对应的实现途径、难度、和大致工作量&#xff0c;并且能够把它以一种业务和技术人员都能无歧义理解的严谨…

mysql存储xml文件_存储XML文档的方法

mysql存储xml文件There have been several approaches that have been proposed to organize the contents of the XML document basically to facilitate the subsequent querying and retrieval have been proposed. 已经提出了几种方法来基本上组织XML文档的内容&#xff0c…

android 获取浏览器的id,H5能获取到手机设备ID或者手机浏览器唯一识别码吗

content"widthdevice-width, initial-scale1,maximum-scale1,user-scalableno">JS获取手机型号和系统//判断数组中是否包含某字符串Array.prototype.contains function(needle) {for (i in this) {if (this[i].indexOf(needle) > 0)return i;}return -1;}var …

图形学 射线相交算法_计算机图形学中的彩色阴极射线管

图形学 射线相交算法彩色阴极射线管 (Color Cathode Ray Tube) CRT i.e. Cathode Ray Tubes are used to produce Color displays. The basic methodology or principle of producing Colored displays is by combining the three basic colors which are: red, blue and green…

java全文检索技术_自学Java与Java培训一年内懂什么技术才可以达到年薪20W

Java作为老牌编程语言&#xff0c;可谓是居高不下&#xff0c;学习Java的途径无非是自学与培训&#xff0c;不管自学还是培训可谓每个人手里都会自己的一套视频或者学习路线&#xff0c;不过年份上与课程内容上可能就层次不齐了&#xff0c;而且对于学习上来说&#xff0c;其实…

android dialog 源码,Android Dialog源码分析

1.当我们查看android dialog源码&#xff0c;首先查看dialog的构造方法&#xff0c;初始化相关数据。由源码可知Dialog获取当前的themeId,通过 new ContextThemeWrapper(context, themeResId);设置主题返回Context对象。通过new PhoneWindow()创建Window对象&#xff0c;设置Wi…

ctype函数_PHP ctype_punct()函数与示例

ctype函数PHP ctype_punct()函数 (PHP ctype_punct() function) ctype_punct() function is a character type (CType) function in PHP, it is used to check whether a given string contains all punctuation characters (can be considered as special characters) or not.…