首页 > 解决方案 > 如何打印不带括号、引号或逗号的列表

问题描述

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)它看起来是一个列表中的一个列表。

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

标签: pythonpython-3.xlist

解决方案


#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

推荐阅读