题目是:有一组“+”和“-”符号,要求将“+”排到左边,“-”排到右边,写出具体的实现方法。
方法一:
l=['-', '+', '-', '+', '+', '-'] a = [] b = [] for i in range(len(l)): if l[i] == '+': a.append(l[i]) else: b.append(l[i]) print a+b
方法二:
def StringSort(data): startindex = 0 endindex = 0 count = len(data) while startindex + endindex < count: if data[startindex] == '-': data[startindex], data[count - endindex - 1] = data[count - endindex - 1], data[startindex] endindex += 1 else: startindex += 1 return data data = ['-', '-', '+', '+', '+','-', '+','-', '+','-','-'] print(StringSort(data))