📜  Python中的列出方法 |设置 1 (in, not in, len(), min(), max()…)

📅  最后修改于: 2022-05-13 01:55:32.805000             🧑  作者: Mango

Python中的列出方法 |设置 1 (in, not in, len(), min(), max()…)

列表基础知识已在下面的集合Python进行了介绍
数据类型——列表、元组和迭代

本文讨论了列表方法。
1. “in”运算符:- 该运算符用于检查列表中是否存在元素。如果元素存在于列表中,则返回 true,否则返回 false。
2. “not in”运算符:- 此运算符用于检查列表中是否不存在元素。如果元素不在列表中,则返回 true,否则返回 false。

Python3
# Python code to demonstrate the working of
# "in" and "not in"
# initializing list
lis = [1, 4, 3, 2, 5]
 
# checking if 4 is in list using "in"
if 4 in lis:
        print ("List is having element with value 4")
else :  print ("List is not having element with value 4")
 
# checking if 4 is not list using "not in"
if 4 not in lis:
        print ("List is not having element with value 4")
else :  print ("List is having element with value 4")


Python3
# Python code to demonstrate the working of
# len(), min() and max()
# initializing list 1
lis = [2, 1, 3, 5, 4]
 
# using len() to print length of list
print ("The length of list is : ", end="")
print (len(lis))
 
# using min() to print minimum element of list
print ("The minimum element of list is : ", end="")
print (min(lis))
 
# using max() to print maximum element of list
print ("The maximum element of list is : ", end="")
print (max(lis))


Python3
# Python code to demonstrate the working of
# "+" and "*"
# initializing list 1
lis = [1, 2, 3]
 
# initializing list 2
lis1 = [4, 5, 6]
 
# using "+" to concatenate lists
lis2= lis + lis1
 
# printing concatenated lists
print ("list after concatenation is : ", end="")
for i in range(0,len(lis2)):
         print (lis2[i], end=" ")
          
print ("\r")
 
#using '*' to combine lists
lis3 = lis * 3
 
# printing combined lists
print ("list after combining is : ", end="")
for i in range(0,len(lis3)):
         print (lis3[i], end=" ")


Python3
# Python code to demonstrate the working of
# index() and count()
# initializing list 1
lis = [2, 1, 3, 5, 4, 3]
 
# using index() to print first occurrence of 3
# prints 5
print ("The first occurrence of 3 after 3rd position is : ", end="")
print (lis.index(3, 3, 6))
 
# using count() to count number of occurrence of 3
print ("The number of occurrences of 3 is : ", end="")
print (lis.count(3))


输出:

List is having element with value 4
List is having element with value 4

3. len() :- 该函数返回列表的长度
4. min() :- 该函数返回列表的最小元素。
5. max() :- 该函数返回列表的最大元素。

Python3

# Python code to demonstrate the working of
# len(), min() and max()
# initializing list 1
lis = [2, 1, 3, 5, 4]
 
# using len() to print length of list
print ("The length of list is : ", end="")
print (len(lis))
 
# using min() to print minimum element of list
print ("The minimum element of list is : ", end="")
print (min(lis))
 
# using max() to print maximum element of list
print ("The maximum element of list is : ", end="")
print (max(lis))

输出:

The length of list is : 5
The minimum element of list is : 1
The maximum element of list is : 5

6.“+”运算符:- 该运算符用于将两个列表连接成一个列表。
7. “*”运算符:- 该运算符用于将列表“n”次相乘并返回单个列表。

Python3

# Python code to demonstrate the working of
# "+" and "*"
# initializing list 1
lis = [1, 2, 3]
 
# initializing list 2
lis1 = [4, 5, 6]
 
# using "+" to concatenate lists
lis2= lis + lis1
 
# printing concatenated lists
print ("list after concatenation is : ", end="")
for i in range(0,len(lis2)):
         print (lis2[i], end=" ")
          
print ("\r")
 
#using '*' to combine lists
lis3 = lis * 3
 
# printing combined lists
print ("list after combining is : ", end="")
for i in range(0,len(lis3)):
         print (lis3[i], end=" ")

输出:

list after concatenation is : 1 2 3 4 5 6 
list after combining is : 1 2 3 1 2 3 1 2 3 

8. index(ele, beg, end) :- 该函数返回 beg 之后 end 之前第一次出现的元素的索引
9. count() :- 该函数计算列表中元素出现的次数

Python3

# Python code to demonstrate the working of
# index() and count()
# initializing list 1
lis = [2, 1, 3, 5, 4, 3]
 
# using index() to print first occurrence of 3
# prints 5
print ("The first occurrence of 3 after 3rd position is : ", end="")
print (lis.index(3, 3, 6))
 
# using count() to count number of occurrence of 3
print ("The number of occurrences of 3 is : ", end="")
print (lis.count(3))

输出:

The first occurrence of 3 after 3rd position is : 5
The number of occurrences of 3 is : 2

相关文章:
列出Python中的方法
Python中的列出方法 |设置 2 (del、remove()、sort()、insert()、pop()、extend()…)