How to group identical values from a list into their own lists?
up vote
4
down vote
favorite
Say I have a list [2, 3, 7, 2, 3, 8, 7, 3]
I would like to produce lists that contain identical values from the list above.
Expected Output something like:
[2, 2]
[3, 3, 3]
[7, 7]
[8]
The order that these lists are produced doesn't matter.
python list-comprehension
add a comment |
up vote
4
down vote
favorite
Say I have a list [2, 3, 7, 2, 3, 8, 7, 3]
I would like to produce lists that contain identical values from the list above.
Expected Output something like:
[2, 2]
[3, 3, 3]
[7, 7]
[8]
The order that these lists are produced doesn't matter.
python list-comprehension
Are you working with numbers, or strings? Also, what do you need this for... can you give us more details?
– Adriano
Nov 22 at 5:17
Numbers (Integers to be precise)
– Oamar Kanji
Nov 22 at 5:19
add a comment |
up vote
4
down vote
favorite
up vote
4
down vote
favorite
Say I have a list [2, 3, 7, 2, 3, 8, 7, 3]
I would like to produce lists that contain identical values from the list above.
Expected Output something like:
[2, 2]
[3, 3, 3]
[7, 7]
[8]
The order that these lists are produced doesn't matter.
python list-comprehension
Say I have a list [2, 3, 7, 2, 3, 8, 7, 3]
I would like to produce lists that contain identical values from the list above.
Expected Output something like:
[2, 2]
[3, 3, 3]
[7, 7]
[8]
The order that these lists are produced doesn't matter.
python list-comprehension
python list-comprehension
asked Nov 22 at 5:15
Oamar Kanji
183210
183210
Are you working with numbers, or strings? Also, what do you need this for... can you give us more details?
– Adriano
Nov 22 at 5:17
Numbers (Integers to be precise)
– Oamar Kanji
Nov 22 at 5:19
add a comment |
Are you working with numbers, or strings? Also, what do you need this for... can you give us more details?
– Adriano
Nov 22 at 5:17
Numbers (Integers to be precise)
– Oamar Kanji
Nov 22 at 5:19
Are you working with numbers, or strings? Also, what do you need this for... can you give us more details?
– Adriano
Nov 22 at 5:17
Are you working with numbers, or strings? Also, what do you need this for... can you give us more details?
– Adriano
Nov 22 at 5:17
Numbers (Integers to be precise)
– Oamar Kanji
Nov 22 at 5:19
Numbers (Integers to be precise)
– Oamar Kanji
Nov 22 at 5:19
add a comment |
7 Answers
7
active
oldest
votes
up vote
5
down vote
accepted
Try this
l = [2, 3, 7, 2, 3, 8, 7, 3]
for i in set(l):
print([i]*l.count(i))
Output:
[8]
[2, 2]
[3, 3, 3]
[7, 7]
1
[[i]*l.count(if) for i in set(l)]
– Netwave
Nov 22 at 5:29
3
There is a "problem" with this solution. It will iterate on l several times. It is not a problem for small lists. But will get slower for big ones. TheO(n)
solution with defaultdict will perform much faster.
– Netwave
Nov 22 at 5:50
add a comment |
up vote
4
down vote
You can use itertools.groupby
with sorted list:
>>> for _, l in itertools.groupby(sorted(l)):
... print(list(l))
...
[2, 2]
[3, 3, 3]
[7, 7]
[8]
Or an O(n)
solution with collections.defaultdict
:
>>> l = [2, 3, 7, 2, 3, 8, 7, 3]
>>> d = defaultdict(list)
>>> for e in l:
... d[e].append(e)
...
>>> d
defaultdict(<class 'list'>, {2: [2, 2], 3: [3, 3, 3], 7: [7, 7], 8: [8]})
>>> d.values()
dict_values([[2, 2], [3, 3, 3], [7, 7], [8]])
Or a list comprehension with collections.Counter
:
>>> from collections import Counter
>>> [[i]*n for i,n in Counter(l).items()]
[[2, 2], [3, 3, 3], [7, 7], [8]]
As I post, the defaultdict solution is O(n)
and faster than the other aproaches. Here are the tests:
from timeit import timeit
setup = (
"from collections import Counter, defaultdict;"
"from itertools import groupby;"
"l = [2, 3, 7, 2, 3, 8, 7, 3];"
)
defaultdict_call = (
"d = defaultdict(list); "
"nfor e in l: d[e].append(e);"
)
groupby_call = "[list(g) for _,g in groupby(sorted(l))]"
counter_call = "[[i]*n for i,n in Counter(l).items()]"
for call in (defaultdict_call, groupby_call, counter_call):
print(call)
print(timeit(call, setup))
Results:
d = defaultdict(list);
for e in l: d[e].append(e);
7.02662614302244
[list(g) for _,g in groupby(sorted(l))]
10.126392606005538
[[i]*n for i,n in Counter(l).items()]
19.55539561196929
Here is the live test
add a comment |
up vote
3
down vote
One way to do this is to use a simple dictionary:
l = [2, 3, 7, 2, 3, 8, 7, 3]
groups = {}
for n in l:
groups.setdefault(n, ).append(n)
print(list(groups.values()))
# [[2, 2], [3, 3, 3], [7, 7], [8]]
add a comment |
up vote
3
down vote
Here's a short way of doing it by using Counter
from collections import Counter
my_dict = Counter([2, 3, 7, 2, 3, 8, 7, 3]) # returns {3: 3, 2: 2, 7: 2, 8: 1}
new_list = [[k] * v for k,v in my_dict.items()]
Outputs:
[[2, 2], [3, 3, 3], [7, 7], [8]]
add a comment |
up vote
3
down vote
This answer is with list-comprehension:
l = [2, 3, 7, 2, 3, 8, 7, 3]
print(*[[i]*l.count(i) for i in set(l)], sep='n')
OUTPUT :
C:UsersDesktop>py x.py
[8]
[2, 2]
[3, 3, 3]
[7, 7]
Moreover, the output can be made exactly as yours with sorted()
method
l = [2, 3, 7, 2, 3, 8, 7, 3]
print(*sorted([[i]*l.count(i) for i in set(l)]), sep='n')
OUTPUT:
C:UsersDesktop>py x.py
[2, 2]
[3, 3, 3]
[7, 7]
[8]
EDIT : As the answer gets upvoted I want to explain the code in detail to be helpful as much as I can.
The code is this:
print(*[[i]*l.count(i) for i in set(l)], sep='n')
Using set(l)
eliminates duplicated values and remains only [2, 3, 7, 8]
in the list. Later, in [i]
we put each element of set(l)
in a new list. We count how many time i
element(i
is a element in set(l
)) occurs in native list l
(l = [2, 3, 7, 2, 3, 8, 7, 3]
). And in [i]*l.count(i)
i
become l.count(i)
times in the new list. List-comprehension method gets the all values after iterations are done and pack it in a list and returns list. *
sign at the beginning is for unpacking the values in the returned list. And finally *print()*
keyword sep='n'
put a 'n'
after each elements in the unpacked list. Without it this could have been done like:
for j in [[i]*l.count(i) for i in set(l)]:
print(j)
add a comment |
up vote
2
down vote
Doing this operation in Numpy array would be efficient
a= np.array([2, 3, 7, 2, 3, 8, 7, 3])
[a[a==i] for i in np.unique(a)]
Output:
[array([2, 2]), array([3, 3, 3]), array([7, 7]), array([8])]
add a comment |
up vote
2
down vote
I think you may try collections.Counter, and get different keys and its count in this list.
from collections import Counter
l = [2, 3, 7, 2, 3, 8, 7, 3]
c =Counter(l)
print(c) ## result: {3: 3, 2: 2, 7: 2, 8: 1}
add a comment |
7 Answers
7
active
oldest
votes
7 Answers
7
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
accepted
Try this
l = [2, 3, 7, 2, 3, 8, 7, 3]
for i in set(l):
print([i]*l.count(i))
Output:
[8]
[2, 2]
[3, 3, 3]
[7, 7]
1
[[i]*l.count(if) for i in set(l)]
– Netwave
Nov 22 at 5:29
3
There is a "problem" with this solution. It will iterate on l several times. It is not a problem for small lists. But will get slower for big ones. TheO(n)
solution with defaultdict will perform much faster.
– Netwave
Nov 22 at 5:50
add a comment |
up vote
5
down vote
accepted
Try this
l = [2, 3, 7, 2, 3, 8, 7, 3]
for i in set(l):
print([i]*l.count(i))
Output:
[8]
[2, 2]
[3, 3, 3]
[7, 7]
1
[[i]*l.count(if) for i in set(l)]
– Netwave
Nov 22 at 5:29
3
There is a "problem" with this solution. It will iterate on l several times. It is not a problem for small lists. But will get slower for big ones. TheO(n)
solution with defaultdict will perform much faster.
– Netwave
Nov 22 at 5:50
add a comment |
up vote
5
down vote
accepted
up vote
5
down vote
accepted
Try this
l = [2, 3, 7, 2, 3, 8, 7, 3]
for i in set(l):
print([i]*l.count(i))
Output:
[8]
[2, 2]
[3, 3, 3]
[7, 7]
Try this
l = [2, 3, 7, 2, 3, 8, 7, 3]
for i in set(l):
print([i]*l.count(i))
Output:
[8]
[2, 2]
[3, 3, 3]
[7, 7]
edited Nov 22 at 5:52
answered Nov 22 at 5:19
Anjaneyulu Batta
3,11011333
3,11011333
1
[[i]*l.count(if) for i in set(l)]
– Netwave
Nov 22 at 5:29
3
There is a "problem" with this solution. It will iterate on l several times. It is not a problem for small lists. But will get slower for big ones. TheO(n)
solution with defaultdict will perform much faster.
– Netwave
Nov 22 at 5:50
add a comment |
1
[[i]*l.count(if) for i in set(l)]
– Netwave
Nov 22 at 5:29
3
There is a "problem" with this solution. It will iterate on l several times. It is not a problem for small lists. But will get slower for big ones. TheO(n)
solution with defaultdict will perform much faster.
– Netwave
Nov 22 at 5:50
1
1
[[i]*l.count(if) for i in set(l)]
– Netwave
Nov 22 at 5:29
[[i]*l.count(if) for i in set(l)]
– Netwave
Nov 22 at 5:29
3
3
There is a "problem" with this solution. It will iterate on l several times. It is not a problem for small lists. But will get slower for big ones. The
O(n)
solution with defaultdict will perform much faster.– Netwave
Nov 22 at 5:50
There is a "problem" with this solution. It will iterate on l several times. It is not a problem for small lists. But will get slower for big ones. The
O(n)
solution with defaultdict will perform much faster.– Netwave
Nov 22 at 5:50
add a comment |
up vote
4
down vote
You can use itertools.groupby
with sorted list:
>>> for _, l in itertools.groupby(sorted(l)):
... print(list(l))
...
[2, 2]
[3, 3, 3]
[7, 7]
[8]
Or an O(n)
solution with collections.defaultdict
:
>>> l = [2, 3, 7, 2, 3, 8, 7, 3]
>>> d = defaultdict(list)
>>> for e in l:
... d[e].append(e)
...
>>> d
defaultdict(<class 'list'>, {2: [2, 2], 3: [3, 3, 3], 7: [7, 7], 8: [8]})
>>> d.values()
dict_values([[2, 2], [3, 3, 3], [7, 7], [8]])
Or a list comprehension with collections.Counter
:
>>> from collections import Counter
>>> [[i]*n for i,n in Counter(l).items()]
[[2, 2], [3, 3, 3], [7, 7], [8]]
As I post, the defaultdict solution is O(n)
and faster than the other aproaches. Here are the tests:
from timeit import timeit
setup = (
"from collections import Counter, defaultdict;"
"from itertools import groupby;"
"l = [2, 3, 7, 2, 3, 8, 7, 3];"
)
defaultdict_call = (
"d = defaultdict(list); "
"nfor e in l: d[e].append(e);"
)
groupby_call = "[list(g) for _,g in groupby(sorted(l))]"
counter_call = "[[i]*n for i,n in Counter(l).items()]"
for call in (defaultdict_call, groupby_call, counter_call):
print(call)
print(timeit(call, setup))
Results:
d = defaultdict(list);
for e in l: d[e].append(e);
7.02662614302244
[list(g) for _,g in groupby(sorted(l))]
10.126392606005538
[[i]*n for i,n in Counter(l).items()]
19.55539561196929
Here is the live test
add a comment |
up vote
4
down vote
You can use itertools.groupby
with sorted list:
>>> for _, l in itertools.groupby(sorted(l)):
... print(list(l))
...
[2, 2]
[3, 3, 3]
[7, 7]
[8]
Or an O(n)
solution with collections.defaultdict
:
>>> l = [2, 3, 7, 2, 3, 8, 7, 3]
>>> d = defaultdict(list)
>>> for e in l:
... d[e].append(e)
...
>>> d
defaultdict(<class 'list'>, {2: [2, 2], 3: [3, 3, 3], 7: [7, 7], 8: [8]})
>>> d.values()
dict_values([[2, 2], [3, 3, 3], [7, 7], [8]])
Or a list comprehension with collections.Counter
:
>>> from collections import Counter
>>> [[i]*n for i,n in Counter(l).items()]
[[2, 2], [3, 3, 3], [7, 7], [8]]
As I post, the defaultdict solution is O(n)
and faster than the other aproaches. Here are the tests:
from timeit import timeit
setup = (
"from collections import Counter, defaultdict;"
"from itertools import groupby;"
"l = [2, 3, 7, 2, 3, 8, 7, 3];"
)
defaultdict_call = (
"d = defaultdict(list); "
"nfor e in l: d[e].append(e);"
)
groupby_call = "[list(g) for _,g in groupby(sorted(l))]"
counter_call = "[[i]*n for i,n in Counter(l).items()]"
for call in (defaultdict_call, groupby_call, counter_call):
print(call)
print(timeit(call, setup))
Results:
d = defaultdict(list);
for e in l: d[e].append(e);
7.02662614302244
[list(g) for _,g in groupby(sorted(l))]
10.126392606005538
[[i]*n for i,n in Counter(l).items()]
19.55539561196929
Here is the live test
add a comment |
up vote
4
down vote
up vote
4
down vote
You can use itertools.groupby
with sorted list:
>>> for _, l in itertools.groupby(sorted(l)):
... print(list(l))
...
[2, 2]
[3, 3, 3]
[7, 7]
[8]
Or an O(n)
solution with collections.defaultdict
:
>>> l = [2, 3, 7, 2, 3, 8, 7, 3]
>>> d = defaultdict(list)
>>> for e in l:
... d[e].append(e)
...
>>> d
defaultdict(<class 'list'>, {2: [2, 2], 3: [3, 3, 3], 7: [7, 7], 8: [8]})
>>> d.values()
dict_values([[2, 2], [3, 3, 3], [7, 7], [8]])
Or a list comprehension with collections.Counter
:
>>> from collections import Counter
>>> [[i]*n for i,n in Counter(l).items()]
[[2, 2], [3, 3, 3], [7, 7], [8]]
As I post, the defaultdict solution is O(n)
and faster than the other aproaches. Here are the tests:
from timeit import timeit
setup = (
"from collections import Counter, defaultdict;"
"from itertools import groupby;"
"l = [2, 3, 7, 2, 3, 8, 7, 3];"
)
defaultdict_call = (
"d = defaultdict(list); "
"nfor e in l: d[e].append(e);"
)
groupby_call = "[list(g) for _,g in groupby(sorted(l))]"
counter_call = "[[i]*n for i,n in Counter(l).items()]"
for call in (defaultdict_call, groupby_call, counter_call):
print(call)
print(timeit(call, setup))
Results:
d = defaultdict(list);
for e in l: d[e].append(e);
7.02662614302244
[list(g) for _,g in groupby(sorted(l))]
10.126392606005538
[[i]*n for i,n in Counter(l).items()]
19.55539561196929
Here is the live test
You can use itertools.groupby
with sorted list:
>>> for _, l in itertools.groupby(sorted(l)):
... print(list(l))
...
[2, 2]
[3, 3, 3]
[7, 7]
[8]
Or an O(n)
solution with collections.defaultdict
:
>>> l = [2, 3, 7, 2, 3, 8, 7, 3]
>>> d = defaultdict(list)
>>> for e in l:
... d[e].append(e)
...
>>> d
defaultdict(<class 'list'>, {2: [2, 2], 3: [3, 3, 3], 7: [7, 7], 8: [8]})
>>> d.values()
dict_values([[2, 2], [3, 3, 3], [7, 7], [8]])
Or a list comprehension with collections.Counter
:
>>> from collections import Counter
>>> [[i]*n for i,n in Counter(l).items()]
[[2, 2], [3, 3, 3], [7, 7], [8]]
As I post, the defaultdict solution is O(n)
and faster than the other aproaches. Here are the tests:
from timeit import timeit
setup = (
"from collections import Counter, defaultdict;"
"from itertools import groupby;"
"l = [2, 3, 7, 2, 3, 8, 7, 3];"
)
defaultdict_call = (
"d = defaultdict(list); "
"nfor e in l: d[e].append(e);"
)
groupby_call = "[list(g) for _,g in groupby(sorted(l))]"
counter_call = "[[i]*n for i,n in Counter(l).items()]"
for call in (defaultdict_call, groupby_call, counter_call):
print(call)
print(timeit(call, setup))
Results:
d = defaultdict(list);
for e in l: d[e].append(e);
7.02662614302244
[list(g) for _,g in groupby(sorted(l))]
10.126392606005538
[[i]*n for i,n in Counter(l).items()]
19.55539561196929
Here is the live test
edited Nov 22 at 6:01
answered Nov 22 at 5:21
Netwave
12k22043
12k22043
add a comment |
add a comment |
up vote
3
down vote
One way to do this is to use a simple dictionary:
l = [2, 3, 7, 2, 3, 8, 7, 3]
groups = {}
for n in l:
groups.setdefault(n, ).append(n)
print(list(groups.values()))
# [[2, 2], [3, 3, 3], [7, 7], [8]]
add a comment |
up vote
3
down vote
One way to do this is to use a simple dictionary:
l = [2, 3, 7, 2, 3, 8, 7, 3]
groups = {}
for n in l:
groups.setdefault(n, ).append(n)
print(list(groups.values()))
# [[2, 2], [3, 3, 3], [7, 7], [8]]
add a comment |
up vote
3
down vote
up vote
3
down vote
One way to do this is to use a simple dictionary:
l = [2, 3, 7, 2, 3, 8, 7, 3]
groups = {}
for n in l:
groups.setdefault(n, ).append(n)
print(list(groups.values()))
# [[2, 2], [3, 3, 3], [7, 7], [8]]
One way to do this is to use a simple dictionary:
l = [2, 3, 7, 2, 3, 8, 7, 3]
groups = {}
for n in l:
groups.setdefault(n, ).append(n)
print(list(groups.values()))
# [[2, 2], [3, 3, 3], [7, 7], [8]]
answered Nov 22 at 5:35
slider
7,3851129
7,3851129
add a comment |
add a comment |
up vote
3
down vote
Here's a short way of doing it by using Counter
from collections import Counter
my_dict = Counter([2, 3, 7, 2, 3, 8, 7, 3]) # returns {3: 3, 2: 2, 7: 2, 8: 1}
new_list = [[k] * v for k,v in my_dict.items()]
Outputs:
[[2, 2], [3, 3, 3], [7, 7], [8]]
add a comment |
up vote
3
down vote
Here's a short way of doing it by using Counter
from collections import Counter
my_dict = Counter([2, 3, 7, 2, 3, 8, 7, 3]) # returns {3: 3, 2: 2, 7: 2, 8: 1}
new_list = [[k] * v for k,v in my_dict.items()]
Outputs:
[[2, 2], [3, 3, 3], [7, 7], [8]]
add a comment |
up vote
3
down vote
up vote
3
down vote
Here's a short way of doing it by using Counter
from collections import Counter
my_dict = Counter([2, 3, 7, 2, 3, 8, 7, 3]) # returns {3: 3, 2: 2, 7: 2, 8: 1}
new_list = [[k] * v for k,v in my_dict.items()]
Outputs:
[[2, 2], [3, 3, 3], [7, 7], [8]]
Here's a short way of doing it by using Counter
from collections import Counter
my_dict = Counter([2, 3, 7, 2, 3, 8, 7, 3]) # returns {3: 3, 2: 2, 7: 2, 8: 1}
new_list = [[k] * v for k,v in my_dict.items()]
Outputs:
[[2, 2], [3, 3, 3], [7, 7], [8]]
edited Nov 22 at 5:41
answered Nov 22 at 5:21
Vineeth Sai
2,24931023
2,24931023
add a comment |
add a comment |
up vote
3
down vote
This answer is with list-comprehension:
l = [2, 3, 7, 2, 3, 8, 7, 3]
print(*[[i]*l.count(i) for i in set(l)], sep='n')
OUTPUT :
C:UsersDesktop>py x.py
[8]
[2, 2]
[3, 3, 3]
[7, 7]
Moreover, the output can be made exactly as yours with sorted()
method
l = [2, 3, 7, 2, 3, 8, 7, 3]
print(*sorted([[i]*l.count(i) for i in set(l)]), sep='n')
OUTPUT:
C:UsersDesktop>py x.py
[2, 2]
[3, 3, 3]
[7, 7]
[8]
EDIT : As the answer gets upvoted I want to explain the code in detail to be helpful as much as I can.
The code is this:
print(*[[i]*l.count(i) for i in set(l)], sep='n')
Using set(l)
eliminates duplicated values and remains only [2, 3, 7, 8]
in the list. Later, in [i]
we put each element of set(l)
in a new list. We count how many time i
element(i
is a element in set(l
)) occurs in native list l
(l = [2, 3, 7, 2, 3, 8, 7, 3]
). And in [i]*l.count(i)
i
become l.count(i)
times in the new list. List-comprehension method gets the all values after iterations are done and pack it in a list and returns list. *
sign at the beginning is for unpacking the values in the returned list. And finally *print()*
keyword sep='n'
put a 'n'
after each elements in the unpacked list. Without it this could have been done like:
for j in [[i]*l.count(i) for i in set(l)]:
print(j)
add a comment |
up vote
3
down vote
This answer is with list-comprehension:
l = [2, 3, 7, 2, 3, 8, 7, 3]
print(*[[i]*l.count(i) for i in set(l)], sep='n')
OUTPUT :
C:UsersDesktop>py x.py
[8]
[2, 2]
[3, 3, 3]
[7, 7]
Moreover, the output can be made exactly as yours with sorted()
method
l = [2, 3, 7, 2, 3, 8, 7, 3]
print(*sorted([[i]*l.count(i) for i in set(l)]), sep='n')
OUTPUT:
C:UsersDesktop>py x.py
[2, 2]
[3, 3, 3]
[7, 7]
[8]
EDIT : As the answer gets upvoted I want to explain the code in detail to be helpful as much as I can.
The code is this:
print(*[[i]*l.count(i) for i in set(l)], sep='n')
Using set(l)
eliminates duplicated values and remains only [2, 3, 7, 8]
in the list. Later, in [i]
we put each element of set(l)
in a new list. We count how many time i
element(i
is a element in set(l
)) occurs in native list l
(l = [2, 3, 7, 2, 3, 8, 7, 3]
). And in [i]*l.count(i)
i
become l.count(i)
times in the new list. List-comprehension method gets the all values after iterations are done and pack it in a list and returns list. *
sign at the beginning is for unpacking the values in the returned list. And finally *print()*
keyword sep='n'
put a 'n'
after each elements in the unpacked list. Without it this could have been done like:
for j in [[i]*l.count(i) for i in set(l)]:
print(j)
add a comment |
up vote
3
down vote
up vote
3
down vote
This answer is with list-comprehension:
l = [2, 3, 7, 2, 3, 8, 7, 3]
print(*[[i]*l.count(i) for i in set(l)], sep='n')
OUTPUT :
C:UsersDesktop>py x.py
[8]
[2, 2]
[3, 3, 3]
[7, 7]
Moreover, the output can be made exactly as yours with sorted()
method
l = [2, 3, 7, 2, 3, 8, 7, 3]
print(*sorted([[i]*l.count(i) for i in set(l)]), sep='n')
OUTPUT:
C:UsersDesktop>py x.py
[2, 2]
[3, 3, 3]
[7, 7]
[8]
EDIT : As the answer gets upvoted I want to explain the code in detail to be helpful as much as I can.
The code is this:
print(*[[i]*l.count(i) for i in set(l)], sep='n')
Using set(l)
eliminates duplicated values and remains only [2, 3, 7, 8]
in the list. Later, in [i]
we put each element of set(l)
in a new list. We count how many time i
element(i
is a element in set(l
)) occurs in native list l
(l = [2, 3, 7, 2, 3, 8, 7, 3]
). And in [i]*l.count(i)
i
become l.count(i)
times in the new list. List-comprehension method gets the all values after iterations are done and pack it in a list and returns list. *
sign at the beginning is for unpacking the values in the returned list. And finally *print()*
keyword sep='n'
put a 'n'
after each elements in the unpacked list. Without it this could have been done like:
for j in [[i]*l.count(i) for i in set(l)]:
print(j)
This answer is with list-comprehension:
l = [2, 3, 7, 2, 3, 8, 7, 3]
print(*[[i]*l.count(i) for i in set(l)], sep='n')
OUTPUT :
C:UsersDesktop>py x.py
[8]
[2, 2]
[3, 3, 3]
[7, 7]
Moreover, the output can be made exactly as yours with sorted()
method
l = [2, 3, 7, 2, 3, 8, 7, 3]
print(*sorted([[i]*l.count(i) for i in set(l)]), sep='n')
OUTPUT:
C:UsersDesktop>py x.py
[2, 2]
[3, 3, 3]
[7, 7]
[8]
EDIT : As the answer gets upvoted I want to explain the code in detail to be helpful as much as I can.
The code is this:
print(*[[i]*l.count(i) for i in set(l)], sep='n')
Using set(l)
eliminates duplicated values and remains only [2, 3, 7, 8]
in the list. Later, in [i]
we put each element of set(l)
in a new list. We count how many time i
element(i
is a element in set(l
)) occurs in native list l
(l = [2, 3, 7, 2, 3, 8, 7, 3]
). And in [i]*l.count(i)
i
become l.count(i)
times in the new list. List-comprehension method gets the all values after iterations are done and pack it in a list and returns list. *
sign at the beginning is for unpacking the values in the returned list. And finally *print()*
keyword sep='n'
put a 'n'
after each elements in the unpacked list. Without it this could have been done like:
for j in [[i]*l.count(i) for i in set(l)]:
print(j)
edited Nov 22 at 6:29
answered Nov 22 at 5:35
Rarblack
2,1283823
2,1283823
add a comment |
add a comment |
up vote
2
down vote
Doing this operation in Numpy array would be efficient
a= np.array([2, 3, 7, 2, 3, 8, 7, 3])
[a[a==i] for i in np.unique(a)]
Output:
[array([2, 2]), array([3, 3, 3]), array([7, 7]), array([8])]
add a comment |
up vote
2
down vote
Doing this operation in Numpy array would be efficient
a= np.array([2, 3, 7, 2, 3, 8, 7, 3])
[a[a==i] for i in np.unique(a)]
Output:
[array([2, 2]), array([3, 3, 3]), array([7, 7]), array([8])]
add a comment |
up vote
2
down vote
up vote
2
down vote
Doing this operation in Numpy array would be efficient
a= np.array([2, 3, 7, 2, 3, 8, 7, 3])
[a[a==i] for i in np.unique(a)]
Output:
[array([2, 2]), array([3, 3, 3]), array([7, 7]), array([8])]
Doing this operation in Numpy array would be efficient
a= np.array([2, 3, 7, 2, 3, 8, 7, 3])
[a[a==i] for i in np.unique(a)]
Output:
[array([2, 2]), array([3, 3, 3]), array([7, 7]), array([8])]
answered Nov 22 at 5:20
AILearning
525420
525420
add a comment |
add a comment |
up vote
2
down vote
I think you may try collections.Counter, and get different keys and its count in this list.
from collections import Counter
l = [2, 3, 7, 2, 3, 8, 7, 3]
c =Counter(l)
print(c) ## result: {3: 3, 2: 2, 7: 2, 8: 1}
add a comment |
up vote
2
down vote
I think you may try collections.Counter, and get different keys and its count in this list.
from collections import Counter
l = [2, 3, 7, 2, 3, 8, 7, 3]
c =Counter(l)
print(c) ## result: {3: 3, 2: 2, 7: 2, 8: 1}
add a comment |
up vote
2
down vote
up vote
2
down vote
I think you may try collections.Counter, and get different keys and its count in this list.
from collections import Counter
l = [2, 3, 7, 2, 3, 8, 7, 3]
c =Counter(l)
print(c) ## result: {3: 3, 2: 2, 7: 2, 8: 1}
I think you may try collections.Counter, and get different keys and its count in this list.
from collections import Counter
l = [2, 3, 7, 2, 3, 8, 7, 3]
c =Counter(l)
print(c) ## result: {3: 3, 2: 2, 7: 2, 8: 1}
answered Nov 22 at 5:23
async
7616
7616
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424276%2fhow-to-group-identical-values-from-a-list-into-their-own-lists%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Are you working with numbers, or strings? Also, what do you need this for... can you give us more details?
– Adriano
Nov 22 at 5:17
Numbers (Integers to be precise)
– Oamar Kanji
Nov 22 at 5:19