[英]Python Whitespace Removal from Strings in a List
Here is my program: 这是我的计划:
import sys
d = sys.stdin.readlines()
print(*d)
d = [x.strip(' ') for x in d]
print(*d)
Here is what happens when I run it: 这是我运行时发生的事情:
>>> import program12
Austin Houston 400
SanFrancisco Fresno 700
Miami Ames 500
# EOF
Austin Houston 400
SanFrancisco Fresno 700
Miami Ames 500
Austin Houston 400
SanFrancisco Fresno 700
Miami Ames 500
My program needs to accept per line, 2 Strings separated by white-space, followed (optionally) by a number. 我的程序需要接受每行,2个由空格分隔的字符串,后跟(可选)数字。 I want to separate these with no white-space so it would be: 我想将这些没有空格分开,所以它会是:
['Austin', 'Houston', 400]
I then want to put these in a 'graph' so I would use something like: 然后我想把它们放在'图表'中,所以我会使用类似的东西:
flights = collections.defaultdict(dict)
Any help is appreciated! 任何帮助表示赞赏!
EDIT: First answer is fixed! 编辑:第一个答案是固定的! In reference to my previous question, I have added this code, and this generates an error: Now I have this: 在参考我之前的问题时,我添加了这段代码,这会产生错误:现在我有了这个:
import sys
d = sys.stdin.readlines()
print(*d)
d = [x.split() for x in d]
print(*d)
flights = {}
for each in d:
flights[each.split()[0]][each.split()[1]] = each.split()[2]
And when I run: 当我跑:
>>> import program12
Austin Houston 400
SanFrancisco Fresno 700
Miami Ames 500
Austin Houston 400
SanFrancisco Fresno 700
Miami Ames 500
['Austin', 'Houston', '400'] ['SanFrancisco', 'Fresno', '700'] ['Miami', 'Ames', '500']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/program12.py", line 8, in <module>
flights[each.split()[0]][each.split()[1]] = each.split()[2]
AttributeError: 'list' object has no attribute 'split'
EDIT 2: My program: 编辑2:我的计划:
import sys
import collections
d = sys.stdin.readlines()
d = filter(None,d.split('\n'))
flights = {each.split()[0]:{each.split()[1]:''} for each in d}
for each in d:
sp = each.split();flights[sp[0]][sp[1]] = '' if len(sp) <= 2 else sp[2]
New Error: 新错误:
>>> import program12
Austin Houston 400
SanFrancisco Fresno 700
Miami Ames 500
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/program12.py", line 4, in <module>
d = filter(None,d.split('\n'))
AttributeError: 'list' object has no attribute 'split'
this is in correspondence with reference to your previous question too. 这也与您之前的问题相对应。 str.split(' ') is different from str.split() str.split('')与str.split()不同
>>> d1 = [i.split(' ') for i in filter(None,d.split('\n'))]
>>> d1
[['Houston', 'Washington', '', '', '', '', '', '', '', '1000'], ['Vancouver', 'Houston', '300'], ['Dallas', 'Sacramento', '', '', '', '', '', '', '', '', '', '800'], ['Miami', '', '', '', '', '', '', '', '', '', '', 'Ames', '2000'], ['SanFrancisco', 'LosAngeles'], ['ORD', 'PVD', '1000']]
>>> d2 = [i.split() for i in filter(None,d.split('\n'))]
>>> d2
[['Houston', 'Washington', '1000'], ['Vancouver', 'Houston', '300'], ['Dallas', 'Sacramento', '800'], ['Miami', 'Ames', '2000'], ['SanFrancisco', 'LosAngeles'], ['ORD', 'PVD', '1000']]
Basically, you need to form your dict before accessing it! 基本上,你需要在访问它之前形成你的dict!
>>> d
'\nHouston Washington 1000\nVancouver Houston 300\nDallas Sacramento 800\nMiami Ames 2000\nSanFrancisco LosAngeles\nORD PVD 1000\n'
>>> d1=filter(None,d.split('\n'))
>>> flights = {each.split()[0]:{each.split()[1]:''} for each in d1}
>>>
>>> flights
{'Houston': {'Washington': ''}, 'SanFrancisco': {'LosAngeles': ''}, 'Dallas': {'Sacramento': ''}, 'Miami': {'Ames': ''}, 'Vancouver': {'Houston': ''}, 'ORD': {'PVD': ''}}
>>> for each in d1:sp = each.split();flights[sp[0]][sp[1]] = '' if len(sp) <= 2 else sp[2]
...
>>> flights
{'Houston': {'Washington': '1000'}, 'SanFrancisco': {'LosAngeles': ''}, 'Dallas': {'Sacramento': '800'}, 'Miami': {'Ames': '2000'}, 'Vancouver': {'Houston': '300'}, 'ORD': {'PVD': '1000'}}
Try this 尝试这个
import sys
d = sys.stdin.readlines()
d = [i.strip() for x in d for i in x.split()]
print(*d)
if you want line by line 如果你想逐行
import sys
for d in sys.stdin.readlines():
d = [i.strip() for i in d.split()]
print(*d)
Use replace()
: 使用replace()
:
import sys
d = sys.stdin.readlines()
print(*d)
d = [x.replace(' ','') for x in d]
print(*d)
If there happen to be tabs instead of just spaces: 如果碰巧有标签而不是空格:
import sys
d = sys.stdin.readlines()
print(*d)
d = [x.replace(' ','').replace('\t', '') for x in d]
print(*d)
This matches the exact result you are trying to get 这与您尝试获得的确切结果相符
For every line in d, strip newline characters using strip('\\n')
and then split it using split()
对于d中的每一行,使用strip('\\n')
删除换行符,然后使用split()
拆分它
d = [x.strip().split() for x in d] d = [x.strip()。split()for x in d]
For your second question, for each in d:
here d
is a list of lists and so each
is a list and you cannot use split()
on it because it is already split. 对于你的第二个问题, for each in d:
这里d
是一个列表列表,因此each
都是一个列表,你不能使用split()
因为它已经被拆分了。 You can directly use each[0]
. 您可以直接使用each[0]
。
Alternatively you could use a regex to split the lines: 或者,您可以使用正则表达式来分割线条:
import sys
import re
raw_lines = sys.stdin.readlines()
data = [re.split("\s+", line) for line in d]
"\\s+" means "one or more whitespace characters" This does not address the problem of converting your (optional) number to a numeric format, but this seems like another question “\\ s +”表示“一个或多个空白字符”这不能解决将(可选)数字转换为数字格式的问题,但这似乎是另一个问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.