Python tuples manipulation
up vote
2
down vote
favorite
I have a tuple in Python like this -
a = ((-,-,x), (-,-,x), (-,-,y), (-,-,z), (-,-,z), (-,-,z))
Now, I want to group the tuples with the same third element. I have to convert this tuple,a into
b = (((-,-,x), (-,-,x)), ((-,-,y)), ((-,-,z), (-,-,z), (-,-,z)))
How do I write Python code for this? To convert a into b? Since tuples are immutable I'm not able to write the code successfully.
python django tuples
|
show 3 more comments
up vote
2
down vote
favorite
I have a tuple in Python like this -
a = ((-,-,x), (-,-,x), (-,-,y), (-,-,z), (-,-,z), (-,-,z))
Now, I want to group the tuples with the same third element. I have to convert this tuple,a into
b = (((-,-,x), (-,-,x)), ((-,-,y)), ((-,-,z), (-,-,z), (-,-,z)))
How do I write Python code for this? To convert a into b? Since tuples are immutable I'm not able to write the code successfully.
python django tuples
- is string or?
– Dejan Marić
Nov 8 at 10:17
1
I don't get why tuples immutability is a problem. You here construct a new tuple.
– Willem Van Onsem
Nov 8 at 10:20
1
I had to add so many':( - next time please post data structures that we can copy-paste into the Python interpreter.
– timgeb
Nov 8 at 10:23
@DejanMarić '-' can be any datatype. I just wanted the logic irrespective of datatype.
– Keerthan Bhat
Nov 8 at 10:24
1
@timgeb Alright noted, my bad. Thanks.
– Keerthan Bhat
Nov 8 at 10:26
|
show 3 more comments
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I have a tuple in Python like this -
a = ((-,-,x), (-,-,x), (-,-,y), (-,-,z), (-,-,z), (-,-,z))
Now, I want to group the tuples with the same third element. I have to convert this tuple,a into
b = (((-,-,x), (-,-,x)), ((-,-,y)), ((-,-,z), (-,-,z), (-,-,z)))
How do I write Python code for this? To convert a into b? Since tuples are immutable I'm not able to write the code successfully.
python django tuples
I have a tuple in Python like this -
a = ((-,-,x), (-,-,x), (-,-,y), (-,-,z), (-,-,z), (-,-,z))
Now, I want to group the tuples with the same third element. I have to convert this tuple,a into
b = (((-,-,x), (-,-,x)), ((-,-,y)), ((-,-,z), (-,-,z), (-,-,z)))
How do I write Python code for this? To convert a into b? Since tuples are immutable I'm not able to write the code successfully.
python django tuples
python django tuples
asked Nov 8 at 10:13
Keerthan Bhat
879
879
- is string or?
– Dejan Marić
Nov 8 at 10:17
1
I don't get why tuples immutability is a problem. You here construct a new tuple.
– Willem Van Onsem
Nov 8 at 10:20
1
I had to add so many':( - next time please post data structures that we can copy-paste into the Python interpreter.
– timgeb
Nov 8 at 10:23
@DejanMarić '-' can be any datatype. I just wanted the logic irrespective of datatype.
– Keerthan Bhat
Nov 8 at 10:24
1
@timgeb Alright noted, my bad. Thanks.
– Keerthan Bhat
Nov 8 at 10:26
|
show 3 more comments
- is string or?
– Dejan Marić
Nov 8 at 10:17
1
I don't get why tuples immutability is a problem. You here construct a new tuple.
– Willem Van Onsem
Nov 8 at 10:20
1
I had to add so many':( - next time please post data structures that we can copy-paste into the Python interpreter.
– timgeb
Nov 8 at 10:23
@DejanMarić '-' can be any datatype. I just wanted the logic irrespective of datatype.
– Keerthan Bhat
Nov 8 at 10:24
1
@timgeb Alright noted, my bad. Thanks.
– Keerthan Bhat
Nov 8 at 10:26
- is string or?
– Dejan Marić
Nov 8 at 10:17
- is string or?
– Dejan Marić
Nov 8 at 10:17
1
1
I don't get why tuples immutability is a problem. You here construct a new tuple.
– Willem Van Onsem
Nov 8 at 10:20
I don't get why tuples immutability is a problem. You here construct a new tuple.
– Willem Van Onsem
Nov 8 at 10:20
1
1
I had to add so many
' :( - next time please post data structures that we can copy-paste into the Python interpreter.– timgeb
Nov 8 at 10:23
I had to add so many
' :( - next time please post data structures that we can copy-paste into the Python interpreter.– timgeb
Nov 8 at 10:23
@DejanMarić '-' can be any datatype. I just wanted the logic irrespective of datatype.
– Keerthan Bhat
Nov 8 at 10:24
@DejanMarić '-' can be any datatype. I just wanted the logic irrespective of datatype.
– Keerthan Bhat
Nov 8 at 10:24
1
1
@timgeb Alright noted, my bad. Thanks.
– Keerthan Bhat
Nov 8 at 10:26
@timgeb Alright noted, my bad. Thanks.
– Keerthan Bhat
Nov 8 at 10:26
|
show 3 more comments
3 Answers
3
active
oldest
votes
up vote
3
down vote
accepted
This can be done with itertools.groupby.
>>> from operator import itemgetter
>>> from itertools import groupby
>>>
>>> a = (('-','-','x'), ('-','-','x'), ('-','-','y'), ('-','-','z'), ('-','-','z'), ('-','-','z'))
>>> tuple(tuple(grp) for _, grp in groupby(a, key=itemgetter(-1)))
>>>
((('-', '-', 'x'), ('-', '-', 'x')),
(('-', '-', 'y'),),
(('-', '-', 'z'), ('-', '-', 'z'), ('-', '-', 'z')))
It's assumed that groups of equal last elements cannot be interrupted (like in your sample data). If they can, you need to sort a with key=itemgetter(-1) first.
...can you explain "interrupted"?
Sure. What I mean is that groupby lumps together groups of consecutive elements where key returns the same value. So if we had something like
b = (('-','-','z'), ('-', '-', 'x'), ('-','-','z'))
then the groupby from above would create three groups, not two.
>>> b = (('-','-','z'), ('-', '-', 'x'), ('-','-','z'))
>>> tuple(tuple(grp) for _, grp in groupby(b, key=itemgetter(-1)))
>>> ((('-', '-', 'z'),), (('-', '-', 'x'),), (('-', '-', 'z'),))
If you don't want that to happen, sort b first.
>>> last = itemgetter(-1)
>>> tuple(tuple(grp) for _, grp in groupby(sorted(b, key=last), key=last))
>>> ((('-', '-', 'x'),), (('-', '-', 'z'), ('-', '-', 'z')))
...can you explain "interrupted"?
– mikuszefski
Nov 8 at 10:24
Yep, OK. Thanks.
– mikuszefski
Nov 8 at 10:29
add a comment |
up vote
0
down vote
Use itertools.groupby:
from itertools import groupby
a = (('-','-','x'), ('-','-','x'), ('-','-','y'), ('-','-','z'), ('-','-','z'), ('-','-','z'))
print(tuple(tuple(x) for _, x in groupby(a)))
# ((('-', '-', 'x'), ('-', '-', 'x')), (('-', '-', 'y'),), (('-', '-', 'z'), ('-', '-', 'z'), ('-', '-', 'z')))
add a comment |
up vote
0
down vote
Given the elements are already ordered by the last element (well the tuples with x as last element, etc. are positioned in a sequence), we can use itertools.groupby:
from itertools import groupby
from operator import itemgetter
b = tuple(tuple(vs) for _, vs in groupby(a, itemgetter(-1)))
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
This can be done with itertools.groupby.
>>> from operator import itemgetter
>>> from itertools import groupby
>>>
>>> a = (('-','-','x'), ('-','-','x'), ('-','-','y'), ('-','-','z'), ('-','-','z'), ('-','-','z'))
>>> tuple(tuple(grp) for _, grp in groupby(a, key=itemgetter(-1)))
>>>
((('-', '-', 'x'), ('-', '-', 'x')),
(('-', '-', 'y'),),
(('-', '-', 'z'), ('-', '-', 'z'), ('-', '-', 'z')))
It's assumed that groups of equal last elements cannot be interrupted (like in your sample data). If they can, you need to sort a with key=itemgetter(-1) first.
...can you explain "interrupted"?
Sure. What I mean is that groupby lumps together groups of consecutive elements where key returns the same value. So if we had something like
b = (('-','-','z'), ('-', '-', 'x'), ('-','-','z'))
then the groupby from above would create three groups, not two.
>>> b = (('-','-','z'), ('-', '-', 'x'), ('-','-','z'))
>>> tuple(tuple(grp) for _, grp in groupby(b, key=itemgetter(-1)))
>>> ((('-', '-', 'z'),), (('-', '-', 'x'),), (('-', '-', 'z'),))
If you don't want that to happen, sort b first.
>>> last = itemgetter(-1)
>>> tuple(tuple(grp) for _, grp in groupby(sorted(b, key=last), key=last))
>>> ((('-', '-', 'x'),), (('-', '-', 'z'), ('-', '-', 'z')))
...can you explain "interrupted"?
– mikuszefski
Nov 8 at 10:24
Yep, OK. Thanks.
– mikuszefski
Nov 8 at 10:29
add a comment |
up vote
3
down vote
accepted
This can be done with itertools.groupby.
>>> from operator import itemgetter
>>> from itertools import groupby
>>>
>>> a = (('-','-','x'), ('-','-','x'), ('-','-','y'), ('-','-','z'), ('-','-','z'), ('-','-','z'))
>>> tuple(tuple(grp) for _, grp in groupby(a, key=itemgetter(-1)))
>>>
((('-', '-', 'x'), ('-', '-', 'x')),
(('-', '-', 'y'),),
(('-', '-', 'z'), ('-', '-', 'z'), ('-', '-', 'z')))
It's assumed that groups of equal last elements cannot be interrupted (like in your sample data). If they can, you need to sort a with key=itemgetter(-1) first.
...can you explain "interrupted"?
Sure. What I mean is that groupby lumps together groups of consecutive elements where key returns the same value. So if we had something like
b = (('-','-','z'), ('-', '-', 'x'), ('-','-','z'))
then the groupby from above would create three groups, not two.
>>> b = (('-','-','z'), ('-', '-', 'x'), ('-','-','z'))
>>> tuple(tuple(grp) for _, grp in groupby(b, key=itemgetter(-1)))
>>> ((('-', '-', 'z'),), (('-', '-', 'x'),), (('-', '-', 'z'),))
If you don't want that to happen, sort b first.
>>> last = itemgetter(-1)
>>> tuple(tuple(grp) for _, grp in groupby(sorted(b, key=last), key=last))
>>> ((('-', '-', 'x'),), (('-', '-', 'z'), ('-', '-', 'z')))
...can you explain "interrupted"?
– mikuszefski
Nov 8 at 10:24
Yep, OK. Thanks.
– mikuszefski
Nov 8 at 10:29
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
This can be done with itertools.groupby.
>>> from operator import itemgetter
>>> from itertools import groupby
>>>
>>> a = (('-','-','x'), ('-','-','x'), ('-','-','y'), ('-','-','z'), ('-','-','z'), ('-','-','z'))
>>> tuple(tuple(grp) for _, grp in groupby(a, key=itemgetter(-1)))
>>>
((('-', '-', 'x'), ('-', '-', 'x')),
(('-', '-', 'y'),),
(('-', '-', 'z'), ('-', '-', 'z'), ('-', '-', 'z')))
It's assumed that groups of equal last elements cannot be interrupted (like in your sample data). If they can, you need to sort a with key=itemgetter(-1) first.
...can you explain "interrupted"?
Sure. What I mean is that groupby lumps together groups of consecutive elements where key returns the same value. So if we had something like
b = (('-','-','z'), ('-', '-', 'x'), ('-','-','z'))
then the groupby from above would create three groups, not two.
>>> b = (('-','-','z'), ('-', '-', 'x'), ('-','-','z'))
>>> tuple(tuple(grp) for _, grp in groupby(b, key=itemgetter(-1)))
>>> ((('-', '-', 'z'),), (('-', '-', 'x'),), (('-', '-', 'z'),))
If you don't want that to happen, sort b first.
>>> last = itemgetter(-1)
>>> tuple(tuple(grp) for _, grp in groupby(sorted(b, key=last), key=last))
>>> ((('-', '-', 'x'),), (('-', '-', 'z'), ('-', '-', 'z')))
This can be done with itertools.groupby.
>>> from operator import itemgetter
>>> from itertools import groupby
>>>
>>> a = (('-','-','x'), ('-','-','x'), ('-','-','y'), ('-','-','z'), ('-','-','z'), ('-','-','z'))
>>> tuple(tuple(grp) for _, grp in groupby(a, key=itemgetter(-1)))
>>>
((('-', '-', 'x'), ('-', '-', 'x')),
(('-', '-', 'y'),),
(('-', '-', 'z'), ('-', '-', 'z'), ('-', '-', 'z')))
It's assumed that groups of equal last elements cannot be interrupted (like in your sample data). If they can, you need to sort a with key=itemgetter(-1) first.
...can you explain "interrupted"?
Sure. What I mean is that groupby lumps together groups of consecutive elements where key returns the same value. So if we had something like
b = (('-','-','z'), ('-', '-', 'x'), ('-','-','z'))
then the groupby from above would create three groups, not two.
>>> b = (('-','-','z'), ('-', '-', 'x'), ('-','-','z'))
>>> tuple(tuple(grp) for _, grp in groupby(b, key=itemgetter(-1)))
>>> ((('-', '-', 'z'),), (('-', '-', 'x'),), (('-', '-', 'z'),))
If you don't want that to happen, sort b first.
>>> last = itemgetter(-1)
>>> tuple(tuple(grp) for _, grp in groupby(sorted(b, key=last), key=last))
>>> ((('-', '-', 'x'),), (('-', '-', 'z'), ('-', '-', 'z')))
edited Nov 8 at 10:29
answered Nov 8 at 10:20
timgeb
43.3k106085
43.3k106085
...can you explain "interrupted"?
– mikuszefski
Nov 8 at 10:24
Yep, OK. Thanks.
– mikuszefski
Nov 8 at 10:29
add a comment |
...can you explain "interrupted"?
– mikuszefski
Nov 8 at 10:24
Yep, OK. Thanks.
– mikuszefski
Nov 8 at 10:29
...can you explain "interrupted"?
– mikuszefski
Nov 8 at 10:24
...can you explain "interrupted"?
– mikuszefski
Nov 8 at 10:24
Yep, OK. Thanks.
– mikuszefski
Nov 8 at 10:29
Yep, OK. Thanks.
– mikuszefski
Nov 8 at 10:29
add a comment |
up vote
0
down vote
Use itertools.groupby:
from itertools import groupby
a = (('-','-','x'), ('-','-','x'), ('-','-','y'), ('-','-','z'), ('-','-','z'), ('-','-','z'))
print(tuple(tuple(x) for _, x in groupby(a)))
# ((('-', '-', 'x'), ('-', '-', 'x')), (('-', '-', 'y'),), (('-', '-', 'z'), ('-', '-', 'z'), ('-', '-', 'z')))
add a comment |
up vote
0
down vote
Use itertools.groupby:
from itertools import groupby
a = (('-','-','x'), ('-','-','x'), ('-','-','y'), ('-','-','z'), ('-','-','z'), ('-','-','z'))
print(tuple(tuple(x) for _, x in groupby(a)))
# ((('-', '-', 'x'), ('-', '-', 'x')), (('-', '-', 'y'),), (('-', '-', 'z'), ('-', '-', 'z'), ('-', '-', 'z')))
add a comment |
up vote
0
down vote
up vote
0
down vote
Use itertools.groupby:
from itertools import groupby
a = (('-','-','x'), ('-','-','x'), ('-','-','y'), ('-','-','z'), ('-','-','z'), ('-','-','z'))
print(tuple(tuple(x) for _, x in groupby(a)))
# ((('-', '-', 'x'), ('-', '-', 'x')), (('-', '-', 'y'),), (('-', '-', 'z'), ('-', '-', 'z'), ('-', '-', 'z')))
Use itertools.groupby:
from itertools import groupby
a = (('-','-','x'), ('-','-','x'), ('-','-','y'), ('-','-','z'), ('-','-','z'), ('-','-','z'))
print(tuple(tuple(x) for _, x in groupby(a)))
# ((('-', '-', 'x'), ('-', '-', 'x')), (('-', '-', 'y'),), (('-', '-', 'z'), ('-', '-', 'z'), ('-', '-', 'z')))
answered Nov 8 at 10:20
Austin
7,8843827
7,8843827
add a comment |
add a comment |
up vote
0
down vote
Given the elements are already ordered by the last element (well the tuples with x as last element, etc. are positioned in a sequence), we can use itertools.groupby:
from itertools import groupby
from operator import itemgetter
b = tuple(tuple(vs) for _, vs in groupby(a, itemgetter(-1)))
add a comment |
up vote
0
down vote
Given the elements are already ordered by the last element (well the tuples with x as last element, etc. are positioned in a sequence), we can use itertools.groupby:
from itertools import groupby
from operator import itemgetter
b = tuple(tuple(vs) for _, vs in groupby(a, itemgetter(-1)))
add a comment |
up vote
0
down vote
up vote
0
down vote
Given the elements are already ordered by the last element (well the tuples with x as last element, etc. are positioned in a sequence), we can use itertools.groupby:
from itertools import groupby
from operator import itemgetter
b = tuple(tuple(vs) for _, vs in groupby(a, itemgetter(-1)))
Given the elements are already ordered by the last element (well the tuples with x as last element, etc. are positioned in a sequence), we can use itertools.groupby:
from itertools import groupby
from operator import itemgetter
b = tuple(tuple(vs) for _, vs in groupby(a, itemgetter(-1)))
answered Nov 8 at 10:20
Willem Van Onsem
139k16131221
139k16131221
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53205572%2fpython-tuples-manipulation%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
- is string or?
– Dejan Marić
Nov 8 at 10:17
1
I don't get why tuples immutability is a problem. You here construct a new tuple.
– Willem Van Onsem
Nov 8 at 10:20
1
I had to add so many
':( - next time please post data structures that we can copy-paste into the Python interpreter.– timgeb
Nov 8 at 10:23
@DejanMarić '-' can be any datatype. I just wanted the logic irrespective of datatype.
– Keerthan Bhat
Nov 8 at 10:24
1
@timgeb Alright noted, my bad. Thanks.
– Keerthan Bhat
Nov 8 at 10:26