search
HomeBackend DevelopmentPython TutorialPython Zero Basics Introduction 4: Sharding of Lists

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)

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


Statement
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
How are arrays used in scientific computing with Python?How are arrays used in scientific computing with Python?Apr 25, 2025 am 12:28 AM

ArraysinPython,especiallyviaNumPy,arecrucialinscientificcomputingfortheirefficiencyandversatility.1)Theyareusedfornumericaloperations,dataanalysis,andmachinelearning.2)NumPy'simplementationinCensuresfasteroperationsthanPythonlists.3)Arraysenablequick

How do you handle different Python versions on the same system?How do you handle different Python versions on the same system?Apr 25, 2025 am 12:24 AM

You can manage different Python versions by using pyenv, venv and Anaconda. 1) Use pyenv to manage multiple Python versions: install pyenv, set global and local versions. 2) Use venv to create a virtual environment to isolate project dependencies. 3) Use Anaconda to manage Python versions in your data science project. 4) Keep the system Python for system-level tasks. Through these tools and strategies, you can effectively manage different versions of Python to ensure the smooth running of the project.

What are some advantages of using NumPy arrays over standard Python arrays?What are some advantages of using NumPy arrays over standard Python arrays?Apr 25, 2025 am 12:21 AM

NumPyarrayshaveseveraladvantagesoverstandardPythonarrays:1)TheyaremuchfasterduetoC-basedimplementation,2)Theyaremorememory-efficient,especiallywithlargedatasets,and3)Theyofferoptimized,vectorizedfunctionsformathematicalandstatisticaloperations,making

How does the homogenous nature of arrays affect performance?How does the homogenous nature of arrays affect performance?Apr 25, 2025 am 12:13 AM

The impact of homogeneity of arrays on performance is dual: 1) Homogeneity allows the compiler to optimize memory access and improve performance; 2) but limits type diversity, which may lead to inefficiency. In short, choosing the right data structure is crucial.

What are some best practices for writing executable Python scripts?What are some best practices for writing executable Python scripts?Apr 25, 2025 am 12:11 AM

TocraftexecutablePythonscripts,followthesebestpractices:1)Addashebangline(#!/usr/bin/envpython3)tomakethescriptexecutable.2)Setpermissionswithchmod xyour_script.py.3)Organizewithacleardocstringanduseifname=="__main__":formainfunctionality.4

How do NumPy arrays differ from the arrays created using the array module?How do NumPy arrays differ from the arrays created using the array module?Apr 24, 2025 pm 03:53 PM

NumPyarraysarebetterfornumericaloperationsandmulti-dimensionaldata,whilethearraymoduleissuitableforbasic,memory-efficientarrays.1)NumPyexcelsinperformanceandfunctionalityforlargedatasetsandcomplexoperations.2)Thearraymoduleismorememory-efficientandfa

How does the use of NumPy arrays compare to using the array module arrays in Python?How does the use of NumPy arrays compare to using the array module arrays in Python?Apr 24, 2025 pm 03:49 PM

NumPyarraysarebetterforheavynumericalcomputing,whilethearraymoduleismoresuitableformemory-constrainedprojectswithsimpledatatypes.1)NumPyarraysofferversatilityandperformanceforlargedatasetsandcomplexoperations.2)Thearraymoduleislightweightandmemory-ef

How does the ctypes module relate to arrays in Python?How does the ctypes module relate to arrays in Python?Apr 24, 2025 pm 03:45 PM

ctypesallowscreatingandmanipulatingC-stylearraysinPython.1)UsectypestointerfacewithClibrariesforperformance.2)CreateC-stylearraysfornumericalcomputations.3)PassarraystoCfunctionsforefficientoperations.However,becautiousofmemorymanagement,performanceo

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor