Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
5c4d3d0e
Commit
5c4d3d0e
authored
Jun 09, 2008
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Let set.intersection() and set.intersection_update() take multiple input arguments.
parent
d6234141
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
54 additions
and
8 deletions
+54
-8
Doc/library/stdtypes.rst
Doc/library/stdtypes.rst
+10
-4
Lib/test/test_set.py
Lib/test/test_set.py
+6
-0
Misc/NEWS
Misc/NEWS
+2
-1
Objects/setobject.c
Objects/setobject.c
+36
-3
No files found.
Doc/library/stdtypes.rst
View file @
5c4d3d0e
...
@@ -1575,11 +1575,14 @@ The constructors for both classes work the same:
...
@@ -1575,11 +1575,14 @@ The constructors for both classes work the same:
.. versionchanged:: 2.6
.. versionchanged:: 2.6
Accepts multiple input iterables.
Accepts multiple input iterables.
.. method:: intersection(other)
.. method:: intersection(other
, ...
)
set & other
set & other
& ...
Return a new set with elements common to both sets.
Return a new set with elements common to both sets.
.. versionchanged:: 2.6
Accepts multiple input iterables.
.. method:: difference(other)
.. method:: difference(other)
set - other
set - other
...
@@ -1639,11 +1642,14 @@ The constructors for both classes work the same:
...
@@ -1639,11 +1642,14 @@ The constructors for both classes work the same:
.. versionchanged:: 2.6
.. versionchanged:: 2.6
Accepts multiple input iterables.
Accepts multiple input iterables.
.. method:: intersection_update(other)
.. method:: intersection_update(other
, ...
)
set &= other
set &= other
& ...
Update the set, keeping only elements found in it and *other*.
Update the set, keeping only elements found in it and *other*.
.. versionchanged:: 2.6
Accepts multiple input iterables.
.. method:: difference_update(other)
.. method:: difference_update(other)
set -= other
set -= other
...
...
Lib/test/test_set.py
View file @
5c4d3d0e
...
@@ -103,6 +103,7 @@ class TestJointOps(unittest.TestCase):
...
@@ -103,6 +103,7 @@ class TestJointOps(unittest.TestCase):
self
.
assertEqual
(
self
.
thetype
(
'abcba'
).
intersection
(
C
(
'efgfe'
)),
set
(
''
))
self
.
assertEqual
(
self
.
thetype
(
'abcba'
).
intersection
(
C
(
'efgfe'
)),
set
(
''
))
self
.
assertEqual
(
self
.
thetype
(
'abcba'
).
intersection
(
C
(
'ccb'
)),
set
(
'bc'
))
self
.
assertEqual
(
self
.
thetype
(
'abcba'
).
intersection
(
C
(
'ccb'
)),
set
(
'bc'
))
self
.
assertEqual
(
self
.
thetype
(
'abcba'
).
intersection
(
C
(
'ef'
)),
set
(
''
))
self
.
assertEqual
(
self
.
thetype
(
'abcba'
).
intersection
(
C
(
'ef'
)),
set
(
''
))
self
.
assertEqual
(
self
.
thetype
(
'abcba'
).
intersection
(
C
(
'cbcf'
),
C
(
'bag'
)),
set
(
'b'
))
def
test_isdisjoint
(
self
):
def
test_isdisjoint
(
self
):
def
f
(
s1
,
s2
):
def
f
(
s1
,
s2
):
...
@@ -429,6 +430,11 @@ class TestSet(TestJointOps):
...
@@ -429,6 +430,11 @@ class TestSet(TestJointOps):
s
=
self
.
thetype
(
'abcba'
)
s
=
self
.
thetype
(
'abcba'
)
self
.
assertEqual
(
s
.
intersection_update
(
C
(
p
)),
None
)
self
.
assertEqual
(
s
.
intersection_update
(
C
(
p
)),
None
)
self
.
assertEqual
(
s
,
set
(
q
))
self
.
assertEqual
(
s
,
set
(
q
))
ss
=
'abcba'
s
=
self
.
thetype
(
ss
)
t
=
'cbc'
self
.
assertEqual
(
s
.
intersection_update
(
C
(
p
),
C
(
t
)),
None
)
self
.
assertEqual
(
s
,
set
(
'abcba'
)
&
set
(
p
)
&
set
(
t
))
def
test_iand
(
self
):
def
test_iand
(
self
):
self
.
s
&=
set
(
self
.
otherword
)
self
.
s
&=
set
(
self
.
otherword
)
...
...
Misc/NEWS
View file @
5c4d3d0e
...
@@ -12,7 +12,8 @@ What's New in Python 2.6 beta 1?
...
@@ -12,7 +12,8 @@ What's New in Python 2.6 beta 1?
Core and Builtins
Core and Builtins
-----------------
-----------------
- The set methods, update() and union() now accept multiple arguments.
- Several set methods now accept multiple arguments: update(), union(),
intersection() and intersection_update().
- Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
- Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
...
...
Objects/setobject.c
View file @
5c4d3d0e
...
@@ -1306,6 +1306,26 @@ set_intersection(PySetObject *so, PyObject *other)
...
@@ -1306,6 +1306,26 @@ set_intersection(PySetObject *so, PyObject *other)
return
(
PyObject
*
)
result
;
return
(
PyObject
*
)
result
;
}
}
static
PyObject
*
set_intersection_multi
(
PySetObject
*
so
,
PyObject
*
args
)
{
Py_ssize_t
i
;
PyObject
*
result
=
(
PyObject
*
)
so
;
Py_INCREF
(
so
);
for
(
i
=
0
;
i
<
PyTuple_GET_SIZE
(
args
)
;
i
++
)
{
PyObject
*
other
=
PyTuple_GET_ITEM
(
args
,
i
);
PyObject
*
newresult
=
set_intersection
((
PySetObject
*
)
result
,
other
);
if
(
newresult
==
NULL
)
{
Py_DECREF
(
result
);
return
NULL
;
}
Py_DECREF
(
result
);
result
=
newresult
;
}
return
result
;
}
PyDoc_STRVAR
(
intersection_doc
,
PyDoc_STRVAR
(
intersection_doc
,
"Return the intersection of two sets as a new set.
\n
\
"Return the intersection of two sets as a new set.
\n
\
\n
\
\n
\
...
@@ -1324,6 +1344,19 @@ set_intersection_update(PySetObject *so, PyObject *other)
...
@@ -1324,6 +1344,19 @@ set_intersection_update(PySetObject *so, PyObject *other)
Py_RETURN_NONE
;
Py_RETURN_NONE
;
}
}
static
PyObject
*
set_intersection_update_multi
(
PySetObject
*
so
,
PyObject
*
args
)
{
PyObject
*
tmp
;
tmp
=
set_intersection_multi
(
so
,
args
);
if
(
tmp
==
NULL
)
return
NULL
;
set_swap_bodies
(
so
,
(
PySetObject
*
)
tmp
);
Py_DECREF
(
tmp
);
Py_RETURN_NONE
;
}
PyDoc_STRVAR
(
intersection_update_doc
,
PyDoc_STRVAR
(
intersection_update_doc
,
"Update a set with the intersection of itself and another."
);
"Update a set with the intersection of itself and another."
);
...
@@ -1946,9 +1979,9 @@ static PyMethodDef set_methods[] = {
...
@@ -1946,9 +1979,9 @@ static PyMethodDef set_methods[] = {
difference_doc
},
difference_doc
},
{
"difference_update"
,
(
PyCFunction
)
set_difference_update
,
METH_O
,
{
"difference_update"
,
(
PyCFunction
)
set_difference_update
,
METH_O
,
difference_update_doc
},
difference_update_doc
},
{
"intersection"
,(
PyCFunction
)
set_intersection
,
METH_O
,
{
"intersection"
,(
PyCFunction
)
set_intersection
_multi
,
METH_VARARGS
,
intersection_doc
},
intersection_doc
},
{
"intersection_update"
,(
PyCFunction
)
set_intersection_update
,
METH_O
,
{
"intersection_update"
,(
PyCFunction
)
set_intersection_update
_multi
,
METH_VARARGS
,
intersection_update_doc
},
intersection_update_doc
},
{
"isdisjoint"
,
(
PyCFunction
)
set_isdisjoint
,
METH_O
,
{
"isdisjoint"
,
(
PyCFunction
)
set_isdisjoint
,
METH_O
,
isdisjoint_doc
},
isdisjoint_doc
},
...
@@ -2073,7 +2106,7 @@ static PyMethodDef frozenset_methods[] = {
...
@@ -2073,7 +2106,7 @@ static PyMethodDef frozenset_methods[] = {
copy_doc
},
copy_doc
},
{
"difference"
,
(
PyCFunction
)
set_difference
,
METH_O
,
{
"difference"
,
(
PyCFunction
)
set_difference
,
METH_O
,
difference_doc
},
difference_doc
},
{
"intersection"
,(
PyCFunction
)
set_intersection
,
METH_O
,
{
"intersection"
,(
PyCFunction
)
set_intersection
_multi
,
METH_VARARGS
,
intersection_doc
},
intersection_doc
},
{
"isdisjoint"
,
(
PyCFunction
)
set_isdisjoint
,
METH_O
,
{
"isdisjoint"
,
(
PyCFunction
)
set_isdisjoint
,
METH_O
,
isdisjoint_doc
},
isdisjoint_doc
},
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment