一个含有字符串的数组,例如:["a","b","a","c","b","d","a",1,1],需要得到一个新数组 ["a1","b1","a2","c","b2","d","a3","11","12"]
时间: 2023-09-04 20:13:11 浏览: 86
以下是 Python 代码实现:
```python
arr = ["a","b","a","c","b","d","a",1,1]
new_arr = []
count_dict = {}
for item in arr:
if isinstance(item, str):
if item not in count_dict:
count_dict[item] = 1
new_arr.append(item + str(count_dict[item]))
else:
count_dict[item] += 1
new_arr.append(item + str(count_dict[item]))
else:
if item not in count_dict:
count_dict[item] = 1
new_arr.append(str(item) + str(count_dict[item]))
else:
count_dict[item] += 1
new_arr.append(str(item) + str(count_dict[item]))
print(new_arr)
```
输出结果为:`['a1', 'b1', 'a2', 'c1', 'b2', 'd1', 'a3', '11', '12']`
阅读全文