山东智趣岛论坛

 找回密码
 立即注册
查看: 241|回复: 0
打印 上一主题 下一主题

太乙真人

[复制链接]

12

主题

12

帖子

182万

积分

版主

Rank: 7Rank: 7Rank: 7

积分
1820008
跳转到指定楼层
楼主
发表于 7 天前 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

Python常用列表函数及功能详解
python常用列表函数,及其功能及示例
在Python中,列表(list)是一种非常常用的数据结构,提供了许多内置函数和方法来操作列表。以下是一些常用的列表函数及其功能和示例:

1. len()
功能: 返回列表的长度(元素个数)。

示例:

python
复制
my_list = [1, 2, 3, 4, 5]
print(len(my_list))  # 输出: 5
2. append()
功能: 在列表末尾添加一个元素。

示例:

python
复制
my_list = [1, 2, 3]
my_list.append(4)
print(my_list)  # 输出: [1, 2, 3, 4]
3. extend()
功能: 将一个列表中的所有元素添加到另一个列表的末尾。

示例:

python
复制
my_list = [1, 2, 3]
my_list.extend([4, 5, 6])
print(my_list)  # 输出: [1, 2, 3, 4, 5, 6]
4. insert()
功能: 在指定位置插入一个元素。

示例:

python
复制
my_list = [1, 2, 3]
my_list.insert(1, 1.5)
print(my_list)  # 输出: [1, 1.5, 2, 3]
5. remove()
功能: 移除列表中第一个匹配的元素。

示例:

python
复制
my_list = [1, 2, 3, 2]
my_list.remove(2)
print(my_list)  # 输出: [1, 3, 2]
6. pop()
功能: 移除并返回列表中指定位置的元素(默认是最后一个元素)。

示例:

python
复制
my_list = [1, 2, 3, 4]
popped_element = my_list.pop(1)
print(popped_element)  # 输出: 2
print(my_list)  # 输出: [1, 3, 4]
7. index()
功能: 返回列表中第一个匹配元素的索引。

示例:

python
复制
my_list = [1, 2, 3, 4, 3]
index = my_list.index(3)
print(index)  # 输出: 2
8. count()
功能: 返回列表中某个元素出现的次数。

示例:

python
复制
my_list = [1, 2, 3, 2, 2, 4]
count = my_list.count(2)
print(count)  # 输出: 3
9. sort()
功能: 对列表中的元素进行排序(默认是升序)。

示例:

python
复制
my_list = [3, 1, 4, 1, 5, 9, 2]
my_list.sort()
print(my_list)  # 输出: [1, 1, 2, 3, 4, 5, 9]
10. reverse()
功能: 反转列表中的元素顺序。

示例:

python
复制
my_list = [1, 2, 3, 4]
my_list.reverse()
print(my_list)  # 输出: [4, 3, 2, 1]
11. copy()
功能: 返回列表的浅拷贝。

示例:

python
复制
my_list = [1, 2, 3]
new_list = my_list.copy()
print(new_list)  # 输出: [1, 2, 3]
12. clear()
功能: 移除列表中的所有元素。

示例:

python
复制
my_list = [1, 2, 3]
my_list.clear()
print(my_list)  # 输出: []
13. sum()
功能: 返回列表中所有元素的和。

示例:

python
复制
my_list = [1, 2, 3, 4]
total = sum(my_list)
print(total)  # 输出: 10
14. min()
功能: 返回列表中的最小值。

示例:

python
复制
my_list = [1, 2, 3, 4]
min_value = min(my_list)
print(min_value)  # 输出: 1
15. max()
功能: 返回列表中的最大值。

示例:

python
复制
my_list = [1, 2, 3, 4]
max_value = max(my_list)
print(max_value)  # 输出: 4
16. sorted()
功能: 返回一个新的排序列表,不改变原列表。

示例:

python
复制
my_list = [3, 1, 4, 1, 5, 9, 2]
sorted_list = sorted(my_list)
print(sorted_list)  # 输出: [1, 1, 2, 3, 4, 5, 9]
print(my_list)  # 输出: [3, 1, 4, 1, 5, 9, 2]
17. any()
功能: 如果列表中至少有一个元素为True,则返回True,否则返回False。

示例:

python
复制
my_list = [False, False, True]
print(any(my_list))  # 输出: True
18. all()
功能: 如果列表中所有元素都为True,则返回True,否则返回False。

示例:

python
复制
my_list = [True, True, False]
print(all(my_list))  # 输出: False
这些是Python中常用的列表函数和方法,掌握它们可以帮助你更高效地处理列表数据。

python字符串常用函数及其功能
在Python中,字符串(str)是一种不可变的序列类型,提供了许多内置函数和方法来操作字符串。以下是一些常用的字符串函数及其功能和示例:

1. len()
功能: 返回字符串的长度(字符个数)。

示例:

python
复制
s = "Hello"
print(len(s))  # 输出: 5
2. str.upper()
功能: 将字符串中的所有字符转换为大写。

示例:

python
复制
s = "hello"
print(s.upper())  # 输出: HELLO
3. str.lower()
功能: 将字符串中的所有字符转换为小写。

示例:

python
复制
s = "HELLO"
print(s.lower())  # 输出: hello
4. str.capitalize()
功能: 将字符串的第一个字符大写,其余字符小写。

示例:

python
复制
s = "hello world"
print(s.capitalize())  # 输出: Hello world
5. str.title()
功能: 将字符串中每个单词的首字母大写。

示例:

python
复制
s = "hello world"
print(s.title())  # 输出: Hello World
6. str.strip()
功能: 去除字符串开头和结尾的空白字符(包括空格、换行符等)。

示例:

python
复制
s = "  hello  "
print(s.strip())  # 输出: hello
7. str.lstrip() 和 str.rstrip()
功能:

lstrip(): 去除字符串开头的空白字符。

rstrip(): 去除字符串结尾的空白字符。

示例:

python
复制
s = "  hello  "
print(s.lstrip())  # 输出: hello  
print(s.rstrip())  # 输出:   hello
8. str.replace()
功能: 替换字符串中的子字符串。

示例:

python
复制
s = "hello world"
print(s.replace("world", "Python"))  # 输出: hello Python
9. str.split()
功能: 将字符串按指定分隔符拆分为列表。

示例:

python
复制
s = "hello,world"
print(s.split(","))  # 输出: ['hello', 'world']
10. str.join()
功能: 将列表中的字符串用指定字符连接成一个字符串。

示例:

python
复制
words = ["hello", "world"]
print("-".join(words))  # 输出: hello-world
11. str.find()
功能: 返回子字符串在字符串中第一次出现的索引,如果未找到则返回 -1。

示例:

python
复制
s = "hello world"
print(s.find("world"))  # 输出: 6
print(s.find("Python"))  # 输出: -1
12. str.index()
功能: 返回子字符串在字符串中第一次出现的索引,如果未找到则抛出 ValueError。

示例:

python
复制
s = "hello world"
print(s.index("world"))  # 输出: 6
# print(s.index("Python"))  # 抛出 ValueError
13. str.count()
功能: 返回子字符串在字符串中出现的次数。

示例:

python
复制
s = "hello world"
print(s.count("l"))  # 输出: 3
14. str.startswith()
功能: 检查字符串是否以指定子字符串开头。

示例:

python
复制
s = "hello world"
print(s.startswith("hello"))  # 输出: True
15. str.endswith()
功能: 检查字符串是否以指定子字符串结尾。

示例:

python
复制
s = "hello world"
print(s.endswith("world"))  # 输出: True
16. str.isalpha()
功能: 检查字符串是否只包含字母字符。

示例:

python
复制
s = "hello"
print(s.isalpha())  # 输出: True
s = "hello123"
print(s.isalpha())  # 输出: False
17. str.isdigit()
功能: 检查字符串是否只包含数字字符。

示例:

python
复制
s = "123"
print(s.isdigit())  # 输出: True
s = "123abc"
print(s.isdigit())  # 输出: False
18. str.isalnum()
功能: 检查字符串是否只包含字母和数字字符。

示例:

python
复制
s = "hello123"
print(s.isalnum())  # 输出: True
s = "hello 123"
print(s.isalnum())  # 输出: False
19. str.islower()
功能: 检查字符串中的所有字母字符是否都是小写。

示例:

python
复制
s = "hello"
print(s.islower())  # 输出: True
s = "Hello"
print(s.islower())  # 输出: False
20. str.isupper()
功能: 检查字符串中的所有字母字符是否都是大写。

示例:

python
复制
s = "HELLO"
print(s.isupper())  # 输出: True
s = "Hello"
print(s.isupper())  # 输出: False
21. str.format()
功能: 格式化字符串,将占位符替换为指定的值。

示例:

python
复制
name = "Alice"
age = 25
print("My name is {} and I am {} years old.".format(name, age))
# 输出: My name is Alice and I am 25 years old.
22. f-string(Python 3.6+)
功能: 使用 f 前缀的字符串格式化方法。

示例:

python
复制
name = "Alice"
age = 25
print(f"My name is {name} and I am {age} years old.")
# 输出: My name is Alice and I am 25 years old.
23. str.encode()
功能: 将字符串编码为字节对象。

示例:

python
复制
s = "hello"
print(s.encode("utf-8"))  # 输出: b'hello'
24. str.zfill()
功能: 在字符串左侧填充 0,直到达到指定长度。

示例:

python
复制
s = "42"
print(s.zfill(5))  # 输出: 00042
这些是Python中常用的字符串函数和方法,掌握它们可以帮助你更高效地处理字符串数据。

开启新对话
给 DeepSeek 发送消息
内容由 AI 生成,请仔细甄别
回复

使用道具 举报

QQ|山东智趣岛论坛 ( 鲁ICP备2023024048号 )

GMT+8, 2025-3-1 03:03 , Processed in 0.184257 second(s), 35 queries .

Powered by Discuz! X3.3 Licensed

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表