# 使用列表推导式

代码：

```python
variable=[out_exp_res for out_exp_res in [1,2,3,4,5,6,7,8] if out_exp_res %2==0]
print(variable)
squares=[x**2 for x in range(10) if x %2==0]
print(squares)
```

结果：

```python
[2, 4, 6, 8]
[0, 4, 16, 36, 64]
```

视频：

```
wait
```

代码：

```python
values=['1','2','A','3','-','N/A']
def is_int(a):
    try:
        x=int(a)
        return True
    except ValueError:
        return False
print('函数','-'*50)
int_values=list(filter(is_int,values))
print(int_values)

print('列表推导','-'*50)
int_values2=[v for v in values if is_int(v)]
print(int_values2)
```

结果：

```python
函数 --------------------------------------------------
['1', '2', '3']
列表推导 --------------------------------------------------
['1', '2', '3']
```

视频：

```
wait
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://im-qianuxn.gitbook.io/pytorch/ji-suan-ji/python/2.1.5-shi-yong-lie-biao-tui-dao-shi.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
