-
Notifications
You must be signed in to change notification settings - Fork 0
/
name_duplicated.py
34 lines (27 loc) · 1.02 KB
/
name_duplicated.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
def name_duplicated(x):
"""
Handle Duplicated Names by Appending Sequence Number
This function checks for duplicated elements in a list. If duplicated elements are found,
it appends an underscore and a sequence number to each occurrence of the duplicated element.
Parameters:
- x (list of str): A list of strings.
Returns:
- list of str: A list where duplicated names are made unique by appending a sequence number.
Example:
>>> vec = ["apple", "orange", "apple", "banana", "orange"]
>>> name_duplicated(vec)
['apple_1', 'orange_1', 'apple_2', 'banana', 'orange_2']
"""
counts = {}
result = []
for item in x:
if item in counts:
counts[item] += 1
result.append(f"{item}_{counts[item]}")
else:
counts[item] = 1
result.append(f"{item}_{counts[item]}" if x.count(item) > 1 else item)
return result
# # Example usage
# vec = ["apple", "orange", "apple", "banana", "orange"]
# print(name_duplicated(vec))