Python-list

Python列表详解
本文详细介绍了Python列表的基本概念、创建方式及各种操作方法,包括访问、组合、迭代等,并提供了丰富的示例代码。

简介

# list,即列表是Python里面基本的数据结构,它是有序且为可迭代对象,且元素类型可以不相同,每个value即元素都有一个index即索引

创建列表

list = list()
list = []
# 创建空列表
list = [1, 'a', 3.14]
# 创建列表并赋初值
list = list(iterable)
# 将一个可迭代对象转换为列表

访问列表中的值

# 列表的index即索引是以0开始的
l = [1, 2, 3, 4]
print(l[0])
# 输出第一个元素即为0
print(l[1])
# 输出第二个元素即为1
# 以此类推
print(l[-1])
# 输出倒数第一个数即为4
print(l[-2])
# 输出倒数第二个数即为3
# 以此类推
# 如果index即索引越界,如l[4]或则l[-5]则会出现IndexError

Python列表脚本操作符

组合

[1, 2, 3] + [4, 5, 6]
# 结果:[1, 2, 3, 4, 5, 6]

重复

[1, 2, 3] * 4
# 结果:[1, 2, 3, 1, 2 ,3, 1, 2, 3, 1 ,2, 3]

元素是否在列表中

1 in [1, 2, 3]
# 结果:True

迭代

for x in [ 1, 2, 3]:
    print(x, end="")
# 结果:123

列表截取

list = [1, 2, 3, 4, 5]
print(list[2:4])
# 结果:[3, 4]

函数

def len(*args, **kwargs):
	pass
l = [1, 2, 3, 4]
length = len(l)
print(length)
# 结果为4
# 返回列表中元素的个数即列表的长度
def max(*args, key=None):
	pass
l = [11, 23, 1, 33, 89, 19, 3]
maxNum = max(l)
print(maxNum)
# 结果为89
# 返回列表中元素的值最大的元素
def min(*args, key=None):
	pass
l = [11, 23, 1, 33, 89, 19, 3]
minNum = min(l)
print(minNum)
# 结果为1
# 返回列表中元素的值最小的元素

方法

增元素

def append(self, p_object): 
	pass
l = [1, 'a', 3.34, 4, 'z']
l.append('r')
print(l)
# 结果为[1, 'a', 3.34, 4, 'z', 'r']
# 往列表后面追加一个元素
def extend(self, iterable):
	pass
l = [1, 'a', 3.34]
add = [2, '1', 'z']
l.extend(add)
print(l)
# 结果为[1, 'a', 3.34, 2, '1', 'z']
# 延展列表,往列表后面追加一个可迭代的序列
def insert(self, index, p_object):
	pass
l = ['Mr', 'is', 'a', 'handsome', 'man']
l.insert(1, 'iDestro')
print(l)
# 结果为['Mr', 'iDestro', 'is', 'a', 'handsome', 'man']
# 在索引值为index的前面插入一个元素

减元素

def clear(self): 
	pass	
l = ['Mr', 'is', 'a', 'handsome', 'man']
l.clear()
print(l)
# 结果为[]
# 清空列表中的所有元素
def remove(self, value):
	pass
l = ['is', 'iDestro', 'is', 'a', 'handsome', 'man']
l.remove('is')
print(l)
# 结果为['iDestro', 'is', 'a', 'handsome', 'man']
# 清除值为value且出现的第一个元素(从左往右)

l = ['is', 'iDestro', 'is', 'a', 'handsome', 'man']
l.remove('hey')
print(l)
# 结果为Traceback (most recent call last):
#        File "C:/Users/iDest/Desktop/优快云/Demo1.py", line 2, in <module>
#          l.remove('hey')
#      ValueError: list.remove(x): x not in list
# 值不存在,触发ValueError
def pop(self, index=None):
	pass
l = ['iDestro', 'is', 'a', 'handsome', 'man', 'hey']
l.pop()
print(l)
# 结果为['iDestro', 'is', 'a', 'handsome', 'man']
# 默认删除列表最后一个元素
l.pop(2)
print(l)
# ['iDestro', 'is', 'handsome', 'man']
# 参数为需要删除元素的索引

换顺序

def reverse(self):
l = ['iDestro', 'is', 'a', 'handsome', 'man', 'hey']
a = l.reverse()
print(l)
print(a)
# 结果为['hey', 'man', 'handsome', 'a', 'is', 'iDestro']
#           None
# 就地逆序,改变当前列表,返回None

def sort(self, key=None, reverse=False):
就地排序,改变当前列表

其它

def copy(self):
	return []
浅拷贝列表
def count(self, value):
	return 0
l = ['iDestro', 'is', 'a', 'is', 'is', 'hey']
times = l.count('is')
print(times)
# 结果为3
# 返回出现元素的值为value的次数

def index(self, value, start=None, stop=None):
	return 0
l = ['iDestro', 'is', 'a', 'is', 'is', 'hey']
index = l.index('is')
print(index)
# 结果为1
# 返回第一次出现元素的值为value的索引
index = l.index('is', 2, 5)
print(index)
# 结果为1
# 返回第一次出现在特定范围且元素的值为value的索引,其中stop可不要,表示到最后一个元素
index = l.index('am')
print(index)
# 结果为File "C:/Users/iDest/Desktop/优快云/Demo1.py", line 10, in <module>
#        index = l.index('am')
#    ValueError: 'am' is not in list
# 值不存在,触发ValueError
<think>我们需要从多行文本中提取特定的邮件地址"python-list@python.org",但要求不包含前缀(如From:,To:等)。我们可以使用正则表达式来匹配该邮件地址,同时确保不会匹配到其他相似但不完全相同的邮件地址。思路:1.直接匹配"python-list@python.org"字符串,但这样可能会匹配到包含该字符串的其他邮箱(如admin@python-list@python.org)或者上下文不同的情况。2.更可靠的方法是匹配一个完整的邮箱地址,并且只匹配我们指定的那个。由于邮箱地址通常是由字母、数字、点、下划线、横线等组成的,我们可以用字符类来定义。但是,题目要求只提取"python-list@python.org",不包含前缀。所以我们可以使用正则表达式来匹配这个特定的字符串,并且确保它前后没有其他字符使得它成为另一个邮箱的一部分。一种方法是使用单词边界`\b`来确保我们匹配的是完整的邮箱地址。正则表达式:`\bpython-list@python\.org\b`注意:点号需要转义,因为点号在正则中表示任意字符。但是,如果这个邮箱地址出现在一行中间,并且前后都是非单词字符(例如在尖括号< >中,或者引号内),那么单词边界可能就不太适用了。另外,我们还需要考虑这个邮箱地址可能出现在行首或行尾。另一种方法是使用负向断言(lookaround)来确保邮箱地址的前后不是其他可能导致它成为更大字符串一部分的字符。但是,由于题目中指定了具体的邮箱地址,且不包含前缀,我们可以考虑这个邮箱地址是独立的,或者被非字母数字字符包围(比如空格、逗号、分号、尖括号等)。我们可以这样写:`(?:^|\s)(python-list@python\.org)(?:\s|$)`但是这样会匹配到前后的空格,而捕获组只捕获邮箱地址。但这样会要求在邮箱前后必须有空格或行边界,但在实际邮件头中,邮箱地址可能出现在逗号分隔的列表中,或者被尖括号包围。更通用的方法是匹配邮箱地址,并且使用非捕获组来匹配可能的前后缀,但只捕获邮箱部分。实际上,由于我们明确知道要提取的字符串,并且这个字符串是固定的,我们可以直接使用:正则表达式:`python-list@python\.org`然后检查匹配的上下文是否是一个独立的邮箱地址。但为了精确,我们可以这样:考虑常见的邮箱地址格式:-可能被尖括号包围:<python-list@python.org>-可能被双引号包围(较少)-可能作为逗号分隔列表的一部分所以,我们可以写一个正则表达式,匹配可能出现的边界情况,并使用捕获组来提取邮箱部分。但是,题目要求只提取"python-list@python.org"而不包含前缀,我们可以在匹配时使用非捕获组来忽略前缀,但在这里我们不需要前缀,所以实际上我们只关心这个字符串本身。然而,为了避免匹配到类似“test-python-list@python.org”这样的字符串,我们可以使用负向断言:正则表达式:`(?<!\w)python-list@python\.org(?!\w)`这个表达式的意思是:邮箱地址前面不能是单词字符(比如字母、数字、下划线),后面也不能是单词字符。这样就能确保匹配的是完整的邮箱地址。但是,在邮件头中,邮箱地址可能被放在尖括号中,例如:<python-list@python.org>。在这种情况下,邮箱地址前面是尖括号,它不是单词字符,所以可以匹配。同样,后面是尖括号,也可以匹配。所以,我们可以使用这个正则表达式。我们来写代码演示:假设文本是多行的,我们使用re.MULTILINE标志,但实际上这个正则表达式不涉及行首行尾,所以不需要特别处理多行模式。代码示例:</think> # 多行文本中提取指定邮箱地址的方法 要提取"python-list@python.org"邮箱地址而不包含前缀(如To:、Cc:等),可以使用以下方法: ## 方法1:直接匹配邮箱地址 ```python import re text = """From: john.doe@example.com To: python-list@python.org From: jane.doe@example.com To: admin@python.org, python-list@python.org Cc: python-list@python.org""" # 查找所有匹配的邮箱地址 matches = re.findall(r'\bpython-list@python\.org\b', text) print("匹配结果:", matches) ``` **输出:** ``` 匹配结果: ['python-list@python.org', 'python-list@python.org', 'python-list@python.org'] ``` **解释:** - `\b` 表示单词边界,确保匹配完整的邮箱地址 - 使用反斜杠对点号 `.` 进行转义,因为点号在正则中有特殊含义 - 直接匹配完整邮箱字符串 ## 方法2:使用前瞻后顾断言提取不含前缀的地址(更精确) ```python import re text = """From: john.doe@example.com To: python-list@python.org From: jane.doe@example.com To: admin@python.org, python-list@python.org Cc: python-list@python.org""" # 使用零宽断言确保邮箱前面没有字母数字字符 pattern = r'(?<!\w)python-list@python\.org(?!\w)' matches = re.findall(pattern, text) print("精确匹配结果:", matches) ``` **解释:** - `(?<!\w)` 表示"前面不能是单词字符"(零宽负向后行断言) - `(?!\w)` 表示"后面不能是单词字符"(零宽负向先行断言) - 这样可以确保只匹配完整的"python-list@python.org"而不匹配任何前缀 ## 方法3:从邮件头字段中提取邮箱地址 ```python import re text = """From: john.doe@example.com To: python-list@python.org From: jane.doe@example.com To: admin@python.org, python-list@python.org Cc: python-list@python.org""" # 匹配邮件头字段中的指定邮箱 matches = [] for header in ['From:', 'To:', 'Cc:']: pattern = fr"{header}.*?\b(python-list@python\.org)\b" for match in re.finditer(pattern, text, re.IGNORECASE): matches.append(match.group(1)) print("提取结果:", matches) ``` **输出:** ``` 提取结果: ['python-list@python.org', 'python-list@python.org', 'python-list@python.org'] ``` **解释:** - `fr"{header}.*?\b(python-list@python\.org)\b"` 使用格式化字符串创建正则 - `re.IGNORECASE` 使匹配不区分大小写 - 使用捕获组 `( )` 只获取邮箱部分 - 遍历多个可能的邮件头字段进行匹配 ## 方法4:处理多个邮箱的复杂情况 ```python import re text = """To: admin@python.org, python-list@python.org CC: support@python.org; python-list@python.org From: python-list@python.org <no-reply@lists.python.org>""" # 匹配邮件头字段中的多个邮箱地址 pattern = r"(?:From|To|Cc):\s*([\w\s\-,;<>@\.]*python-list@python\.org[\w\s\-,;<>@\.]*)" all_headers = re.findall(pattern, text, re.IGNORECASE) matches = [] for header in all_headers: # 从单个头部提取所有邮箱 emails = re.findall(r'\bpython-list@python\.org\b', header) matches.extend(emails) print("复杂情况提取结果:", matches) ``` **输出:** ``` 复杂情况提取结果: ['python-list@python.org', 'python-list@python.org', 'python-list@python.org'] ``` **解释:** 1. 首先匹配可能包含多个邮箱的整个头部内容 2. 然后从中提取具体的"python-list@python.org"地址 3. 这种方式可以处理邮箱列表(逗号/分号分隔)和带名称的邮箱格式
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值