input def inisial(daftar):
daftar = (
"Michael","Viny","Aurelio","Michael",
"Felix","Kevin","Vincen","Vincen","Michael")
inisial(daftar)
output:
Michael
Viny
Aurelio
Michael2
Felix
Kevin
Vincen2
Vincen3
Michael3
input def inisial(daftar):
daftar = (
"Michael","Viny","Aurelio","Michael",
"Felix","Kevin","Vincen","Vincen","Michael")
inisial(daftar)
output:
Michael
Viny
Aurelio
Michael2
Felix
Kevin
Vincen2
Vincen3
Michael3
I assume you want to count the number of occurences for each name. If so, you can do it like this. If you want the output to be different you can, of course, change the return format of the dictionary.
def inisial(daftar):
d = {}
for daft in daftar:
if daft not in d:
d[daft] = 0
else:
d[daft] += 1
return d
daftar = (
"Michael","Viny","Aurelio","Michael",
"Felix","Kevin","Vincen","Vincen","Michael")
inisial(daftar)
Output:
{'Michael': 2, 'Viny': 0, 'Aurelio': 0, 'Felix': 0, 'Kevin': 0, 'Vincen': 1}
You need to count frequencies of entries in your list and use this frequencies in returend list.
def inisial(daftar):
freqs = {}
for name in daftar:
freqs[name] = freqs.get('name', 0) + 1
ret = []
for name in daftar:
freq = freqs[name]
if freq == 1:
ret.append(name)
elif freq > 1:
ret.append(name + str(freq))
return ret
you can solve the problem with the following code.
daftar = ("Michael", "Viny", "Aurelio", "Michael",
"Felix", "Kevin", "Vincen", "Vincen", "Michael")
temp_dict = {}
for name in daftar:
if temp_dict.get(name):
temp_dict[name] += 1
else:
temp_dict[name] = 1
if temp_dict[name] > 1:
print('%s%d' % (name, temp_dict[name]))
else:
print(name)