【问题标题】:How to print a list without brackets or quotes or commas如何打印不带括号、引号或逗号的列表
【发布时间】:2021-11-07 01:42:41
【问题描述】:
user_input = input()
x = user_input.split()
print(x)

if len(x) % 2 == 0:
    y = 'even'

if len(x) % 2 != 0:
    y = 'odd'

list1 = []
list2 = []
if y == 'even':
    list1.append(x[::2])
    list2.append(x[1::2])
    print(*list1, sep=' ')
    print(*list2, sep=' ')
if y == 'odd':
    print('INVALID INPUT')

样本输入将是

"This is a test" 

这只是一个简单的列表制作工具,我正试图弄清楚。出于某种原因,(*list1, sep='') 仍在打印为完整列表。 当我将其保留为 print(list1) 时,它看起来是列表中的列表。

我正在尝试打印不带逗号、方括号或引号的列表。

【问题讨论】:

    标签: python python-3.x list


    【解决方案1】:
    #list1 and list2 are created with slicing
    #join is used to convert the list into a string
    
    user_input = input()
    x = user_input.split()
    print(x)
    
    if len(x) % 2 == 0:
        y = 'even'
    
    if len(x) % 2 != 0:
        y = 'odd'
    
    #print(x,y)
    if y == 'even':
        list1=x[::2]
        list2=x[1::2]
        print(list1,list2)
        print(' '.join(list1[::1]))
        print(' '.join(list2[::1]))
    if y == 'odd':
        print('INVALID INPUT')
    
    #output
    This is a test
    ['This', 'is', 'a', 'test']
    ['This', 'a'] ['is', 'test']
    This a
    is test
    

    【讨论】:

    • 没关系。我对它们做了 type() 并回答了我自己的愚蠢问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-27
    • 2022-11-12
    • 2022-01-10
    • 2022-10-13
    • 2023-01-17
    • 1970-01-01
    相关资源
    最近更新 更多