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

2024-10-01 11:36:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我想要的是:

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

最后我得到了:

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

我确实想看看它是否能工作,但我真的不知道,请帮忙,我已经为你发送了完整的代码 你们来看看发生了什么,帮我找到它的确切位置,因为我是一个完全的新手 关于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 

Tags: andtrueifelseprintappendlststr
2条回答

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

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

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

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

 print(text)

结果

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

相关问题 更多 >