Home > Backend Development > Python Tutorial > Python Zero Basics Introduction 4: Sharding of Lists

Python Zero Basics Introduction 4: Sharding of Lists

黄舟
Release: 2017-01-16 13:53:13
Original
1450 people have browsed it

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.

Python Zero Basics Introduction 4: Sharding of Lists

Python Zero Basics Introduction 4: Sharding of Lists

2. List repetition operator (*)

Python Zero Basics Introduction 4: Sharding of Lists

Python Zero Basics Introduction 4: Sharding of Lists

3. Membership operator (in not in)
Function: Determine whether an element exists in a list

Python Zero Basics Introduction 4: Sharding of Lists

Python Zero Basics Introduction 4: Sharding of Lists

4. List built-in functions
(1)count(): Count the number of times an element appears in a list

Python Zero Basics Introduction 4: Sharding of Lists

Python Zero Basics Introduction 4: Sharding of Lists

(2)index(): Returns the position where the specified element first appears in the list

Python Zero Basics Introduction 4: Sharding of Lists

Python Zero Basics Introduction 4: Sharding of Lists

This function can also be used Add parameters to search within the specified interval.

(3)reverse() function: realize the reversal of list elements

Python Zero Basics Introduction 4: Sharding of Lists

Python Zero Basics Introduction 4: Sharding of Lists

Of course, it can also be realized from the largest Sort by smallest (note that reverse=True is capitalized)

Python Zero Basics Introduction 4: Sharding of Lists

Python Zero Basics Introduction 4: Sharding of Lists

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)
Copy after login

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)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template