data = '------++---' # 将-放在右边,+放在左边 # 方法一 新数组修改 def sortString1(data): len_data = len(data) strat_index = 0 new_data = [] for i in data: if i == '-': new_data.append(i) else: new_data.insert(0, i) print(new_data) # sortString(data) # 方法二 原数组的修改 def sortString2(data): len_data = len(data) start = 0 for i in range(len_data): if data[start] == '-': data[start], data[len_data -1] = data[len_data -1], data[start] len_data -= 1 else: start += 1 print(data) if __name__ == '__main__': sortString2(list(data))