【发布时间】:2021-12-10 05:19:34
【问题描述】:
我正在寻找在输入一串数字时删除尾随空格的解决方案。
代码
vals_inp=input()
print('vals_inp',vals_inp)
vals_strip = vals_inp.rstrip()
print('vals_strip', vals_strip, type(vals_inp))
in_set=set(vals_strip) # problem happens here
输出
in_set {'1', '2', '3', ' '} #PROBLEM ' '
尝试过的事情
我试过 rstrip lstrip rstrip 和 strip(' ') 我看到很多使用 rstrip 等的答案,但它对我不起作用
为什么重要
我的集合中的 ' ' 搞乱了后续代码。感谢所有帮助
【问题讨论】:
-
如果您输入的是
1 2 3,那么' '就是您的数字的分隔符。 minimal reproducible example 显示实际输入,或至少显示对应于脚本的输出,会有所帮助。 -
这能回答你的问题吗? Get a list of numbers as input from the user
标签: python-3.x set whitespace removing-whitespace