Hack Forums
[Python] Count the number of replicates in a list - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Python] Count the number of replicates in a list (/showthread.php?tid=5694386)



Count the number of replicates in a list - Mucius Scaevola - Aug 14, 2017

I have a list like this, this is an example list:

(1, 1, 2, 2, 2, 3, 4)

I need to create a similar list but then with the number of replicates for each list item. This list will look like this:

(2, 2, 3, 3, 3, 1, 1)

All count modules I found do not support such output. Does anyone have an idea what modules to use that do support this?

Thanks!


* - Retribute - Aug 14, 2017

PHP Code

<?php 
tup 
= (1, 1, 2, 2, 2, 3, 4)
tup_instances = tuple((tup.count(i) for i in tup))
print(
tup_instances) 



* - Mucius Scaevola - Aug 14, 2017

(Aug 14, 2017 10:11 AM)Retribute Wrote:  
PHP Code

<?php 
tup 
= (1, 1, 2, 2, 2, 3, 4)
tup_instances = tuple((tup.count(i) for i in tup))
print(
tup_instances) 

Legend!