首先生成一个随机的集合
In [100]: data Out[100]: [-4, -2, 6, 1, 5, 10, 3, -4, 1, 7] In [101]: set(data) Out[101]: {-4, -2, 1, 3, 5, 6, 7, 10}
1
2
3
4
5
|
In
[
100
]
:
data
Out
[
100
]
:
[
-
4
,
-
2
,
6
,
1
,
5
,
10
,
3
,
-
4
,
1
,
7
]
In
[
101
]
:
set
(
data
)
Out
[
101
]
:
{
-
4
,
-
2
,
1
,
3
,
5
,
6
,
7
,
10
}
|
In [106]: data Out[106]: {-4, -2, 1, 3, 5, 6, 7, 10} In [107]: {i for i in data if i%2 == 0} Out[107]: {-4, -2, 6, 10}
1
2
3
4
5
|
In
[
106
]
:
data
Out
[
106
]
:
{
-
4
,
-
2
,
1
,
3
,
5
,
6
,
7
,
10
}
In
[
107
]
:
{
i
for
i
in
data
if
i
%
2
==
0
}
Out
[
107
]
:
{
-
4
,
-
2
,
6
,
10
}
|
[]