python - 如何打印不带括号、引号或逗号的列表
问题描述
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)
它看起来是一个列表中的一个列表。
我正在尝试打印不带逗号、方括号或引号的列表。
解决方案
#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
推荐阅读
- magento - 管理员菜单不会滑出,配置菜单也不会打开。按钮也不起作用
- c# - 碰撞的统一问题-特定碰撞后碰撞不起作用
- android - 在 Android 中覆盖 MPChart 值
- java - ConcurrentHashMap 中的值抛出 NullPointerException
- javascript - for循环传递参数中的多个onchange效果
- javascript - 将类添加到
- 如果有超过 2 个
- 嵌套的
- python - 如何修复 VSCode 和 Python 中的“无法导入 gurobipy”、未定义变量“模型”和未定义变量“GRB”?
- spring-boot - LoadBalanced RestTemplate 总是得到 java.net.SocketTimeoutException: Read timed out
- android - 语言在 API 级别 24 以上不变
- java - 我有这个错误 java.lang.IndexOutOfBoundsException: Index: 0, Size: 0