对于元组,.split()和.rstrip()的等价物是什么?

Max*_*wer 3 python

我有一个看起来像这样的元组:

('1 2130 0 279 90 92 193 1\n', '1 186 0 299 14 36 44 1\n')
Run Code Online (Sandbox Code Playgroud)

我想拆分它,以便每列都是单独的,所以我可以更容易地访问它.

举个例子:

tuple[0][2]
Run Code Online (Sandbox Code Playgroud)

会回来的 0

tuple[1][3]
Run Code Online (Sandbox Code Playgroud)

会回来的 299

我的问题的第二部分是什么相当于.rstrip() 所以我可以摆脱\n

Ble*_*der 8

你可以使用列表理解:

rows = [row.split() for row in your_tuple]
Run Code Online (Sandbox Code Playgroud)

至于.rstrip(),你不需要它..split()(没有争论!)为你照顾:

>>> '    a    b  c \t\n\n   '.split()
['a', 'b', 'c']
Run Code Online (Sandbox Code Playgroud)