Python---List

Source: Internet
Author: User

list, consisting of a series of elements arranged in a particular order. You can create a list of all the letters, numbers, 0~9, or names of all family members in the alphabet, or you can add anything to the list, where there is no relationship between the elements. In Python, the list is represented by square brackets ([]), and the elements are separated by commas. Given that a list usually contains multiple elements, it is a good idea to give the list a name that represents a complex number (such as letters, digits, or names).

1. Accessing list elements

languages = [' Python ', ' C ', ' C + + ', ' java ', ' php '] #输出列表元素print (languages) #通过索引访问列表元素print (Languages[0]) #将索引指定为-1, You can access the last element of the list print (Languages[-1].title ()) #使用列表中的某个值message = "My favorite languages is" + languages[3].title () + "." Print (message)

2. Modify, add, and delete elements

Cities = [' Xian ', ' Beijing ', ' Shanghai '] #输出列表元素print (cities) #修改元素cities [2] = ' Shenzhen ' Print (cities) #末尾添加元素, method append () facilitates dynamic creation of List cities.append (' Guangzhou ') print (cities) #插入元素cities. Insert (2, ' Chengdu ') print (cities) # Use the DEL statement to delete an element by index del Cities[0]print (cities) #方法pop () to delete the element at the end of the list and use that element poped_cities = Cities.pop () print (cities) print ( poped_cities) #使用pop () to remove elements from any position in the list, simply specify the index of the element to be deleted in parentheses poped_cities = Cities.pop (0) print (cities) print (Poped_ Cities) #方法remove (), delete elements by value #love_city = ' Xian ' cities.remove (' Xian ') print (cities) print ("My Love City is" + love_ City.title () + ".")

3. Sort the list

Letters = [' J ', ' l ', ' R ', ' B ', ' s '] #初始列表print (Letters) #排序后列表, sorted alphabetically, method sort () permanently changed the sort of List Letters.sort () print (Letters #按字母相反顺序排序, simply pass the parameter reverse = True to the sort () method, and similarly permanently change the list sort letters.sort (reverse = true) print (Letters) #使用sorted (), Temporarily sort the list print (sorted (Letters)) #方法reverse (), sort in reverse order of the list Letters.reverse () print (Letters) #方法len (), calculate the list length print (Len ( Letters))

4. Traversing the list

Courses = [' Math ', ' 中文版 ', ' Chinese ', ' music ', ' art ']for course in courses:    print (course)    print ("The Couser is "+ course.title () +". \ n ") print (" That is all of the course. ")

5. List of values

Numbers = Range (1,11) #打印数字1 -10print (Numbers) #函数list () Converts the result of range () to a list numbers = Lists (range (1,6)) print (numbers) # function range (), specifying the step size. Example printing even even_numbers = List (range (2,11,2)) print (even_numbers) in 1-10

6. Simple statistical calculation

digits = [9,4,3,7,1,5] #输出最小值print (min (digits)) #输出最大值print (max (digits)) #求和print (sum (digits))

7. Slicing

Mobile_phones = [' Huawei ', ' apple ', ' Noki ', ' mi ', ' zt ', ' Tianyu '] #输出0, 1 and 2 elements print (Moblie_phones[0:3]) #如果没有指定第一个索引, The print (Mobile_phones[:2]) #如果没有指定第二个索引 is automatically started from the beginning of the list, terminating with print at the end of the list (moblie_phones[1:]) #负数索引返回离列表末尾相应长度的切片print ( Moblie_pones[-2:]) #遍历切片for Moblie_phone in Moblie_phones[1:4]: print (Moblie_phone.title ())

8. Copy the list

My_sports = [' Basketball ', ' Pingbang ', ' Soccer ', ' runing '] #复制列表friend_soorts = My_sports[:]print (my_sports) print ( Friend_sports) my_sports.append (' siwiming ') friend_soirts.append (' biking ') print ("I like Sports is:") print (My_ Sports) Print ("\nmy Friends like Sports is:") print (Friends_sports)

9, meta-group

Python will not be able to modify values called immutable, and immutable lists are called tuples.

Dimensions = (() #输出元组print (Dimensions[0]) print (dimensions[1]) #修改元组变量, Assignment dimensions = (+) print ("\ Nmodified Dimensions: ") #遍历元组for Dimension in dimensions:    print (dimension)

  

Python---List

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.