用python中最简单的方法查找列表和元组中以a或A开头 并且以c结尾的所有元素。 li=["alec","Aric","Alex","Tony","rain"] tu =("alec", " Aric", "Alex", "Tony", "rain")
时间: 2023-10-11 19:10:33 浏览: 85
python 找出列表中最大或最小的元素 示例
5星 · 资源好评率100%
可以使用列表推导式来筛选符合条件的元素。以下是实现的代码:
```python
li = ["alec", "Aric", "Alex", "Tony", "rain"]
tu = ("alec", "Aric", "Alex", "Tony", "rain")
result = [element for element in li + tu if (element.startswith("a") or element.startswith("A")) and element.endswith("c")]
print(result)
```
输出结果为:
```
['alec', 'Aric']
```
阅读全文