In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Sequence is the most basic data structure in Python. Each element in the sequence is assigned a number-its position, or index, with the first index 0, the second index 1, and so on.
Python has built-in types for six sequences, but the most common are lists and tuples.
Operations that can be performed in a sequence include indexing, slicing, adding, multiplying, and checking members.
In addition, Python has built-in methods to determine the length of the sequence and to determine the largest and smallest elements.
The list is the most commonly used Python data type and can appear as a comma-separated value in square brackets.
The data items of the list do not need to have the same type
To create a list, simply enclose the different data items separated by commas in square brackets. As follows: list1 = ['physics',' chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5] list3 = ["a", "b", "c", "d"]
Like the index of a string, the list index starts at 0. Lists can be intercepted, combined, and so on.
Values in the access list
Use the subscript index to access the values in the list, and you can also intercept characters in square brackets, as shown below:
Instance (Python 2.0 +)
#! / usr/bin/python list1 = ['physics',' chemistry', 1997, 2000] list2 = [1,2,3,4,5,6,7] print "list1 [0]:", list1 [0] print "list2 [1:5]:", list2 [1:5]
The output result of the above example:
List1 [0]: physicslist2 [1:5]: [2, 3, 4, 5] update list
You can modify or update the data items in the list, or you can use the append () method to add list items, as shown below:
Instance (Python 2.0 +)
#! / usr/bin/python #-*-coding: UTF-8-*-list = [] # # empty list list.append ('Google') # # add element list.append (' Runoob') print list using append ()
Note: we will discuss the use of the append () method in the following sections
The output result of the above example:
['Google',' Runoob'] Delete list elements
You can use the del statement to delete elements of a list, as shown in the following example:
Instance (Python 2.0 +)
#! / usr/bin/python list1 = ['physics',' chemistry', 1997, 2000] print list1 del list1 [2] print "After deleting value at index 2:" print list1
The output result of the above example:
['physics',' chemistry', 1997, 2000] After deleting value at index 2: ['physics',' chemistry', 2000]
Note: we will discuss the use of the remove () method in the following sections
Python list script operator
The operators of list pairs + and * are similar to strings. The + sign is used to combine the list, and the * sign is used to repeat the list.
As follows:
The result of Python expression describes the length of len ([1, 2, 3]) 3 length [1, 2, 3] + [4, 5, 6] [1, 2, 3, 4, 5, 6] combination ['hight'] * 4 ['hippie', 'hippie'] Repeat 3 in [1, 2, 3] whether the True element exists in the list for x in [1, 2, 3]: print xprit 1 2 3 iterative Python list interception
The list intercepted examples of Python are as follows: > L = ['Google',' Runoob', 'Taobao'] > L [2]' Taobao' > L [- 2] 'Runoob' > L [1:] [' Runoob', 'Taobao'] >
Description:
Python expression result describes L [2] 'Taobao' read list third element L [- 2]' Runoob' read list second to last element L [1:] ['Runoob',' Taobao'] intercepts the list Python list function & method from the second element
Python contains the following functions:
Ordinal function 1cmp (list1, list2)
Compare the element 2len (list) of the two lists
Number of list elements 3max (list)
Returns the maximum value of the list element 4min (list)
Returns the minimum value of the list element 5list (seq)
Convert tuples to lists
Python includes the following methods:
Serial number method 1list.append (obj)
Add a new object 2list.count (obj) at the end of the list
Count the number of times an element appears in the list 3list.extend (seq)
Append multiple values from another sequence at the end of the list at once (extend the original list with the new list) 4list.index (obj)
Find the index position 5list.insert (index, obj) of the first match of a value from the list
Insert objects into the list 6list.pop ([index=-1])
Removes an element from the list (the default last element) and returns the value of that element 7list.remove (obj)
Remove the first occurrence of a value in the list 8list.reverse ()
Elements 9list.sort (cmp=None, key=None, reverse=False) in the reverse list
Sort the original list
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.