Continue to learn the related operations of the list from the previous article, mainly learning list sharding and several functions that operate on list elements, which is a supplement to the previous article.
1. List fragmentation
Format: str1=str[start position: end position]
Function: Copy the elements in the specified range in str to str1, that is to say fragmentation What you get is a copy of the original string, not just a label pointing to str. What you get through "=" assignment is just a label of str. That is, the operation on str is the operation on str1, which is similar to the value transfer in Java. and citations.
2. List repetition operator (*)
3. Membership operator (in not in)
Function: Determine whether an element exists in a list
4. List built-in functions
(1)count(): Count the number of times an element appears in a list
(2)index(): Returns the position where the specified element first appears in the list
This function can also be used Add parameters to search within the specified interval.
(3)reverse() function: realize the reversal of list elements
Of course, it can also be realized from the largest Sort by smallest (note that reverse=True is capitalized)
The relevant knowledge about the list is basically over here, and will be sorted out tomorrow A data structure similar to a list - a tuple. Finally, attach today’s code:
print("------列表的分片------") str1="abcdefg"str2=str1[0:2]print(str2)print("------重复操作符------") list=["小甲鱼","小布丁","大布丁"] list=list*3print(list)print("------成员关系操作符------")print("小甲鱼" in list)print("\n") list1=['123',"华为",["三星","小米"]]print("三星" in list1)print("\n")print("三星" in list1[2]) list2=['1','1','2','3'] num=list2.count('1')print("列表中元素1出现的次数为:%d"%num) list3=list2[:]print(list3.index('1'))print(list3) list3.reverse()print(list3) list4=[2,1,4,3,7,5]print(list4)print("\n") list4.sort()print(list4) list4.sort(reverse=True)print(list4)
The above is the content of the shards in the fourth list of zero-based introduction to Python. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!