Reputation: 129
I have a list with duplicate elements. I want to count the occurrence of each element
example->
list1= ["a", "b", "b", "c", "d", "d", "d", "a", "a"]
output->
result = ["a1", "b2", "c1", "d3", "a2"]
i.e. output format should be <value><count>
I have tried this solution, but its not giving expected output
def find_repetitive_elem(a):
dict_obj = {}
arr_obj = []
for i in range(0, len(a)-1):
count = 1
if a[i] == a[i+1]
set_val = "%s%s" % (a[i], count+1)
else:
set_val = "%s%s" % (a[i], count)
arr_obj.append(set_val)
I'm trying to implement this using algorithm and not by python package's!
Upvotes: 2
Views: 95
Reputation: 3421
This might look very basic, but this is how I understood the question:
Code:
list1 = ["a", "b", "b", "c", "d", "d", "d", "a", "a"]
def find_repetitive_elem_continuously(arr):
result = list()
prev_ele = arr[0]
count = 0
for curr_ele in arr:
if curr_ele != prev_ele:
new_arr_ele = str(prev_ele)+""+str(count)
result.append(new_arr_ele)
prev_ele = curr_ele
count = 1
else:
count += 1
new_arr_ele = str(prev_ele)+""+str(count)
result.append(new_arr_ele)
print result
find_repetitive_elem_continuously(list1)
Output:
['a1', 'b2', 'c1', 'd3', 'a2']
Upvotes: 1
Reputation: 21766
You can use groupby
from the itertools
module to achieve this:
import itertools
list1= ["a", "b", "b", "c", "d", "d", "d", "a", "a"]
["{}{}".format(key,sum( 1 for _ in group )) for key, group in itertools.groupby( list1 ) ]
As you have updated your answer stating that you do not want to use a Python package, this is the most elegant way I could think off that doesn't use external modules:
list1= ["a", "b", "b", "c", "d", "d", "d", "a", "a"]
#output-> result = ["a1", "b2", "c1", "d3", "a2"]
def find_repetitive_elem(array):
output=[]
for letter in array:
existing_entry = {}
new_entry = {}
if len(output)>0:
existing_entry= output.pop();
if letter in existing_entry:
existing_entry[letter] +=1
output.append(existing_entry)
else:
if existing_entry:
output.append(existing_entry)
new_entry[letter]=1
output.append(new_entry)
return output
output = find_repetitive_elem(list1)
print ["{}{}".format(key,value) for key,value in (entry.popitem() for entry in output)]
Upvotes: 2
Reputation: 132028
Not sure what you mean by
I'm trying to implement this using algorithm and not by python package's!
This is without using itertools (though that is a far better way to do this)
def compact(inval):
last_char = None
outval = []
count = 0
for l in inval + ['dummyvalue']:
if last_char and last_char != l:
outval.append("{}{}".format(last_char, count))
count = 0
last_char = l
count += 1
return outval
letters = ["a", "b", "b", "c", "d", "d", "d", "a", "a"]
print(compact(letters))
OUTPUT
letters = ["a", "b", "b", "c", "d", "d", "d", "a", "a"] # yields
['a1', 'b2', 'c1', 'd3', 'a2']
letters = ["a", "b", "b", "c", "c", "d", "e", "d", "d", "d", "a", "a"] # yields
['a1', 'b2', 'c2', 'd1', 'e1', 'd3', 'a2']
Upvotes: 2
Reputation: 22011
Since you do not want to use any of Python's modules, here is a solution that does not use any (except for builtins
implicitly). The code should be able to work on more than just strings.
def main():
array = 'a', 'b', 'b', 'c', 'd', 'd', 'd', 'a', 'a'
result = find_repetitive_elements(array)
print(result)
def find_repetitive_elements(array):
result, first, last, count = [], True, None, None
for item in array:
if first:
first, last, count = False, item, 1
elif item == last:
count += 1
else:
result.append('{}{}'.format(last, count))
last, count = item, 1
if not first:
result.append('{}{}'.format(last, count))
return result
if __name__ == '__main__':
main()
Upvotes: 2
Reputation: 25339
Here is my attempt without using itertools.groupby
. The code relies on itertools.dropwhile
iterator, but it is quite easy to replace it and I leave this as an exercise for you.
import itertools
list1= ["a", "b", "b", "c", "d", "d", "d", "a", "a"]
result = []
lst = list1.copy()
while len(lst)>0:
current_char = lst[0]
length = len(lst)
lst = list(itertools.dropwhile(lambda x: x==current_char, lst))
result.append("{}{}".format(current_char, length-len(lst)))
print(result)
See code output at http://ideone.com/Cd1Y8B
Upvotes: 0
Reputation: 11961
You could use itertools.groupby
:
from itertools import groupby
list1= ["a", "b", "b", "c", "d", "d", "d", "a", "a"]
answer = [group[0] + str(len(list(group[1]))) for group in groupby(list1)]
print(answer)
Output
['a1', 'b2', 'c1', 'd3', 'a2']
Upvotes: 4