4. List of Python data types

Source: Internet
Author: User

List
List common actions
1. Index value

Name_list = [' Wang ', ' Zhou ', ' Li ', ' Hu ', ' Wu ', ' Zhao ']print (name_list[0]) print (name_list[-1])

2. List slices

#将列表中第一个到第二元素切片出来, form a sub-list name_list = [' Wang ', ' Zhou ', ' Li ', ' Hu ', ' Wu ', ' Zhao ']print (Name_list[1:3])

3, get the length of the list

Name_list = [' Wang ', ' Zhou ', ' Li ', ' Hu ', ' Wu ', ' Zhao '] #h获取长度print (len (name_list)) #输出所有的元素print (Name_list[0:len (name_ List)])

4. For loop print out all elements of the list

Name_list = [' Wang ', ' Zhou ', ' Li ', ' Hu ', ' Wu ', ' Zhao ']for i in Name_list:    print (i)

5. Delete list Specify location element

Name_list = [' Wang ', ' Zhou ', ' Li ', ' Hu ', ' Wu ', ' zhao '] #删除第一个元素name_list = [' Wang ', ' Zhou ', ' Li ', ' Hu ', ' Wu ', ' Zhao ']del name_ List[1]print (name_list) #删除第0个到第一个元素name_list = [' Wang ', ' Zhou ', ' Li ', ' Hu ', ' Wu ', ' Zhao ']del name_list[0:2]print (name_ List

List method

Name_list = [' Wang ', ' Zhou ', ' Li ', ' Hu ', ' Wu ', ' Zhao '] #获取列表的所有方法, including the built-in method print (dir (name_list)) #外部方法 [' Append ', ' Clear ', ' Copy ', ' Count ', ' extend ', ' index ', ' Insert ', ' pop ', ' remove ', ' reverse ', ' sort ']

Append

#在列表尾部追加元素name_list = [' Wang ', ' Zhou ', ' Li ', ' Hu ', ' Wu ', ' Zhao ']name_list.append (' hehe ') print (name_list)

Clear

#将列表中元素清空, form an empty list name_list = [' Wang ', ' Zhou ', ' Li ', ' Hu ', ' Wu ', ' Zhao ']name_list.clear () print (name_list)

Count

#统计列表中某一个元素出现的次数name_list = [' Wang ', ' Zhou ', ' Li ', ' Hu ', ' Wu ', ' Zhao ']num=name_list.count (' li ') print (num)

Extend

#将一个列表全部元素追加到另一个列表中name_list = [' Wang ', ' Zhou ', ' Li ', ' Hu ', ' Wu ', ' zhao ']num_list = [1,2,34,56,32]name_list.extend (num_ List) print (name_list)

Index

#h获取列表中某个元素的索引name_list = [' Wang ', ' Zhou ', ' Li ', ' Hu ', ' Wu ', ' Zhao ']print (name_list.index (' Wang '))

Insert

#在列表中某个位置插入一个元素name_list = [' Wang ', ' Zhou ', ' Li ', ' Hu ', ' Wu ', ' Zhao ']name_list.insert (3, ' good ') print (name_list) print ( Name_list.index (' good '))

Pop

#删除列表中最后一个元素, and you can assign the element as a string to a variable name_list = [' Wang ', ' Zhou ', ' Li ', ' Hu ', ' Wu ', ' Zhao ']a=name_list.pop () print (name_ List) print (a)

Remove

#移除列表中指定元素name_list = [' Wang ', ' Zhou ', ' Li ', ' Hu ', ' Wu ', ' Zhao ']name_list.remove (') ' Print (name_list)

Reverse

#将列表中所有元素翻转过来name_list = [' Wang ', ' Zhou ', ' Li ', ' Hu ', ' Wu ', ' Zhao ']name_list.reverse () print (name_list)

Sort

#列表元素排序num =[23,123,45,12,456,323,1,34]num.sort () print (num)

4, List of Python data types

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.