更改特定位置列表中的某些字符

时间:2018-01-24 04:48:31

标签: python string list

我的目标是接受一个列表然后向后输出。 现在我的输入/输出是:

['1', '[2,[4,5]]', '3'] 

['3', '[]5,4[,2]', '1']

从输出中可以看出,括号面向错误的方向。我正在尝试使用替换功能,但它用括号替换每个位置。

正确的输出应如下所示。

['3', '[[5,4],2]', '1']

这是我的代码。

organizeList = ['1','[2,[4,5]]','3']
print("Format should be similar to '1, 2, [3, 4], 5'")
#organizeList.append(input("Enter a list: ")
tempList = []
otherList = []
testString = ""
lastString = ""
print(organizeList)
for p in range(len(organizeList)-1, -1, -1):
    if (organizeList[p].startswith('[') == True):
        for showList in organizeList[p]:
            testString = testString + showList
        for x in range(len(testString)-1, -1, -1):
            if (testString[x] == ']'):
                testString = testString.replace(testString[x], '[')
            elif (testString[x] == '['):
                testString = testString.replace(testString[x], ']')
            lastString = lastString + testString[x]
        tempList.append(lastString)
    else:
        tempList.append(organizeList[p])
print(tempList)

我还在学习python,所以如果你有关于如何清理我的代码的任何提示和技巧,请告诉我。

3 个答案:

答案 0 :(得分:2)

我不知道您的代码有什么问题,但您应该将所有左括号重新映射到右括号,反之亦然,如下所示:

>>> remap = {ord('['): ']', ord(']'): '['}
>>> L = ['1', '[2,[4,5]]', '3']
>>> [s[::-1].translate(remap) for s in reversed(L)]
['3', '[[5,4],2]', '1']

答案 1 :(得分:0)

我能够做一个相当简单的修复。不确定我的整个方法是否正确,但这是有效的。

organizeList = ['1','[2,[4,5]]','3']
print("Format should be similar to '1, 2, [3, 4], 5'")
#organizeList.append(input("Enter a list: ")
tempList = []
otherList = []
testString = ""
lastString = ""
remap = {ord('['): ']', ord(']'): '['}
print(organizeList)
for p in range(len(organizeList)-1, -1, -1):
    if (organizeList[p].startswith('[') == True):
        for showList in organizeList[p]:
            testString = testString + showList
        for x in range(len(testString)-1, -1, -1):
            if (testString[x] == ']'):
                lastString = lastString + '['
            elif (testString[x] == '['):
                lastString = lastString + ']'
            else:
                lastString = lastString + testString[x]
        tempList.append(lastString)
    else:
        tempList.append(organizeList[p])
print(tempList)

就像我说的那样我还在学习python,所以如果有人对我有任何提示,请告诉我。

答案 2 :(得分:0)

您也可以尝试这种方式

my_list_of_string = ['1', '[2,[4,5]]', '3']

print("Input : ",my_list_of_string)
# This function basically replace '[' to ']' and ']' to '['
# That's why when we will reverse the string it will maintain the
# parenthesis
def reverse_list_string(ls):
    c = []
    for ch in ls:
        if ch == "[":
            c.append("]")
        elif ch == "]":
            c.append("[")
        else:
            c.append(ch)
    c.reverse()
    new_reverse_string = "".join(c)
    return new_reverse_string

reverse_ls=[]
for ls in my_list_of_string:
    new_string = reverse_list_string(ls)
    reverse_ls.append(new_string)

reverse_ls.reverse()
print("Output : ", reverse_ls)

示例输入/输出

Input :  ['1', '[2,[4,5]]', '3']
Output :  ['3', '[[5,4],2]', '1']