如何从我的空列表中删除逗号和方括号?

问题描述 投票:0回答:1

好吧,我想要的是

[1] [2] [3] [4]

而我最终的结果是:

[[1], [2], [3], [4]]

我做了[i],看看它是否会工作,但我真的不知道,请帮助,我已经发送了完整的代码,让你们看看这是怎么回事,并帮助我确切地在它需要的地方,因为我是一个完整的新手在python,我的朋友帮我做的代码,但他不知道太。

numeroIncorreto = True  
continuaProcesso = True

lst_A = []
lst_B = []

    while continuaProcesso:
        while numeroIncorreto:
            try:
                n1 = int(input('Digite o primeiro número: '))
                n2 = int(input('Digite o segundo número: '))
                numeroIncorreto = False  
            except:
                print('Favor digitar um número correto')
        numeros = '' 
        if n2 >= 10 and n2 <= 50 and n1 >= 10 and n1 <= 50: 
            if n1>n2:
                for i in range(n2, n1+1): 
                    if i == n1:   
                        numeros += str(i) 
                    else:    
                        numeros += str(i) + ' '

                    if i%2 == 0: 
                        lst_A.append([i]) 

                    if str(i)[1] == '3' or str(i)[1] == '4': #
                        lst_B.append([i])  

            if n1<n2:
                numeroD = n2  
                for i in range(n1, n2+1):
                    if numeroD == n1: 
                        numeros += str(numeroD) 
                    else:
                        numeros += str(numeroD) + ' ' 

                    if numeroD%2 == 0: 
                        lst_A.append([numeroD])

                    if str(numeroD)[1] == '3' or str(numeroD)[1] == '4': 
                        lst_B.append([numeroD]) 

                    numeroD -= 1 

            #Visualização na tela com devido espaço         
            print('\n'+numeros+'')
            print('\nlst_A:')
            print(lst_A)
            print('\nlst_B:')
            print(lst_B)
            continuaProcesso = False
        else:
            print('Números fora do intervalo desejado.')
            print('Numero 1: ' + str(n1))
            print('Numero 2: ' + str(n2))
            numeroIncorreto = True 
python comma square-bracket empty-list
1个回答
1
投票

试试 print(*lst_A)print(*lst_B).


0
投票

首先将列表中的元素转换为字符串,然后将元素连接起来

 data = [[1], [2], [3], [4]]

 data_str = [str(x) for x in data]
 text = ' '.join(data_str)

 print(text)

结果

[1] [2] [3] [4]
© www.soinside.com 2019 - 2024. All rights reserved.