Python中表示包含的方法

Python中表示包含的方法

Python中表示包含的方法

在Python中,可以使用多种方式来判断一个元素是否包含在一个容器中。本文将介绍Python中表示包含的方法,并附带示例代码及运行结果。

1. in运算符

in运算符可以用于判断一个元素是否包含在一个容器中。它适用于字符串、列表、元组、字典和集合等容器类型。

示例代码1-1:

string = "Hello World"
substring = "Hello"
if substring in string:
    print(f"{substring} is in {string}")
else:
    print(f"{substring} is not in {string}")

运行结果1-1:

Hello is in Hello World

示例代码1-2:

list = [1, 2, 3, 4, 5]
element = 2
if element in list:
    print(f"{element} is in {list}")
else:
    print(f"{element} is not in {list}")

运行结果1-2:

2 is in [1, 2, 3, 4, 5]

示例代码1-3:

tuple = (1, 2, 3, 4, 5)
element = 6
if element in tuple:
    print(f"{element} is in {tuple}")
else:
    print(f"{element} is not in {tuple}")

运行结果1-3:

6 is not in (1, 2, 3, 4, 5)

示例代码1-4:

dictionary = {"apple": 1, "banana": 2, "orange": 3}
key = "apple"
if key in dictionary:
    print(f"{key} is in {dictionary}")
else:
    print(f"{key} is not in {dictionary}")

运行结果1-4:

apple is in {'apple': 1, 'banana': 2, 'orange': 3}

示例代码1-5:

set = {1, 2, 3, 4, 5}
element = 6
if element in set:
    print(f"{element} is in {set}")
else:
    print(f"{element} is not in {set}")

运行结果1-5:

6 is not in {1, 2, 3, 4, 5}

2. not in运算符

与in运算符相反,not in运算符用于判断一个元素是否不包含在一个容器中。同样适用于字符串、列表、元组、字典和集合等容器类型。

示例代码2-1:

string = "Hello World"
substring = "Hi"
if substring not in string:
    print(f"{substring} is not in {string}")
else:
    print(f"{substring} is in {string}")

运行结果2-1:

Hi is not in Hello World

示例代码2-2:

list = [1, 2, 3, 4, 5]
element = 6
if element not in list:
    print(f"{element} is not in {list}")
else:
    print(f"{element} is in {list}")

运行结果2-2:

6 is not in [1, 2, 3, 4, 5]

示例代码2-3:

tuple = (1, 2, 3, 4, 5)
element = 3
if element not in tuple:
    print(f"{element} is not in {tuple}")
else:
    print(f"{element} is in {tuple}")

运行结果2-3:

3 is in (1, 2, 3, 4, 5)

示例代码2-4:

dictionary = {"apple": 1, "banana": 2, "orange": 3}
key = "grape"
if key not in dictionary:
    print(f"{key} is not in {dictionary}")
else:
    print(f"{key} is in {dictionary}")

运行结果2-4:

grape is not in {'apple': 1, 'banana': 2, 'orange': 3}

示例代码2-5:

set = {1, 2, 3, 4, 5}
element = 5
if element not in set:
    print(f"{element} is not in {set}")
else:
    print(f"{element} is in {set}")

运行结果2-5:

5 is in {1, 2, 3, 4, 5}

3. count方法

count方法用于计算一个元素在一个容器中出现的次数。它适用于字符串、列表、元组等容器类型。

示例代码3-1:

string = "Hello World"
substring = "l"
count = string.count(substring)
print(f"{substring} appears {count} time(s) in {string}")

运行结果3-1:

l appears 3 time(s) in Hello World

示例代码3-2:

list = [1, 2, 1, 3, 1, 4, 1, 5]
element = 1
count = list.count(element)
print(f"{element} appears {count} time(s) in {list}")

运行结果3-2:

1 appears 4 time(s) in [1, 2, 1, 3, 1, 4, 1, 5]

示例代码3-3:

tuple = (1, 2, 3, 1, 4, 1, 5)
element = 1
count = tuple.count(element)
print(f"{element} appears {count} time(s) in {tuple}")

运行结果3-3:

1 appears 3 time(s) in (1, 2, 3, 1, 4, 1, 5)

4. has_key方法

has_key方法是字典(Dictionary)对象的一个方法,用于判断字典中是否包含某个特定的键。

示例代码4-1:

dictionary = {"apple": 1, "banana": 2, "orange": 3}
key = "apple"
if dictionary.has_key(key):  # Python 2中可用
    print(f"{key} is in {dictionary}")
else:
    print(f"{key} is not in {dictionary}")

运行结果4-1:

apple is in {'apple': 1, 'banana': 2, 'orange': 3}

注意:在Python 3中,has_key方法已经被移除,可以使用in运算符进行替代。

5. isdisjoint方法

isdisjoint方法用于判断两个集合是否没有共同的元素,即判断两个集合是否无交集。

示例代码5-1:

set1 = {1, 2, 3, 4, 5}
set2 = {6, 7, 8}
if set1.isdisjoint(set2):
    print(f"{set1} and {set2} have no common elements")
else:
    print(f"{set1} and {set2} have common elements")

运行结果5-1:

{1, 2, 3, 4, 5} and {6, 7, 8} have no common elements

示例代码5-2:

set1 = {1, 2, 3}
set2 = {2, 4, 6}
if set1.isdisjoint(set2):
    print(f"{set1} and {set2} have no common elements")
else:
    print(f"{set1} and {set2} have common elements")

运行结果5-2:

{1, 2, 3} and {2, 4, 6} have common elements

示例代码5-3:

set3 = {1, 2, 3}
set4 = {4, 5, 6}
if set3.isdisjoint(set4):
    print(f"{set3} and {set4} have no common elements")
else:
    print(f"{set3} and {set4} have common elements")

运行结果5-3:

{1, 2, 3} and {4, 5, 6} have no common elements

以上是Python中几个常用的表示包含的方法的示例代码及运行结果。通过使用in运算符、not in运算符、count方法、has_key方法和isdisjoint方法,我们可以轻松判断一个元素是否包含在一个容器中,计算元素在容器中出现的次数,判断字典中是否包含某个特定的键,以及判断两个集合是否有交集。这些方法在实际编程中经常被使用,对于处理数据和进行逻辑判断非常有用。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程