请帮我写一段 筛选出两个列表中不一样值 的python代码
时间: 2023-06-06 20:08:50 浏览: 66
以下是 Python 代码,可以筛选出两个列表中不一样的值:
```python
list1 = [1, 2, 3, 4, 5]
list2 = [3, 4, 5, 6, 7]
# 使用 set 的差集操作,即可筛选出不一样的值
result = set(list1) - set(list2) | set(list2) - set(list1)
print(list(result))
```
运行代码后,将会输出 `[1, 2, 6, 7]`,表示在两个列表中都不一样的值。
相关问题
请帮我写一段 筛选出一个列表中另一个列表没有的值 的python代码
好的,这是答案:
```python
list1 = [1, 2, 3, 4, 5]
list2 = [3, 5, 7, 9]
result = list(set(list1) - set(list2))
print(result)
```
输出结果为:
```
[1, 2, 4]
```
请帮我写一段 筛选出df表某一列等于一个列表中的值的所有行 的python代码
以下是筛选出df表某一列等于一个列表中的值的所有行的Python代码:
```python
import pandas as pd
df = pd.DataFrame({'A': [1, 2, 3, 4, 5],
'B': ['a', 'b', 'c', 'd', 'e']})
filter_list = ['b', 'd', 'f']
filtered_df = df[df['B'].isin(filter_list)]
```
其中,’B’是要筛选的列名,‘filter_list’是要筛选的值列表,’filtered_df‘是筛选后的结果。