strip()方法为什么会从单词列表中返回一个列表?

huangapple go评论63阅读模式
英文:

Why does strip() method returns a list from a list of words?

问题

  1. 第一个问题是我不知道为什么返回空字符串。

  2. 第二个问题是我不知道为什么 strip() 的行为与 .split() 不同。我认为可能与每种方法的返回语句有关,但我想更好地理解为什么会发生这种情况。

英文:

I was trying to make a list with each char of a sentence.

Given the sentence "7h3 qu1ck brown fox jumps ov3r 7h3 lazy dog"

I used the .split() method to separate the words and afterwards another .split() method:

x = [i.split() for i in sentence]

and it returned:

[['7'], ['h'], ['3'], [], ['q'], ['u'], ['1'], ['c'], ['k'], [], ['b'], ['r'], ['o'], ['w'], ['n'], [], ['f'], ['o'], ['x'], [], ['j'], ['u'], ['m'], ['p'], ['s'], [], ['o'], ['v'], ['3'], ['r'], [], ['7'], ['h'], ['3'], [], ['l'], ['a'], ['z'], ['y'], [], ['d'], ['o'], ['g']]

So I was trying to figure out why this was happening and tried with:

[i.strip() for i in sentence if len(i) > 0]

and it returned:

['7', 'h', '3', '', 'q', 'u', '1', 'c', 'k', '', 'b', 'r', 'o', 'w', 'n', '', 'f', 'o', 'x', '', 'j', 'u', 'm', 'p', 's', '', 'o', 'v', '3', 'r', '', '7', 'h', '3', '', 'l', 'a', 'z', 'y', '', 'd', 'o', 'g']
  1. The first issue I'm facing is that I don't know why is returning empty strings.

  2. The second issue is that I don't know why strip() behaves differently than .split(). I think that maybe it can be related to the return statement of each method, but I would like to understand better why this happen.

答案1

得分: 1

.split(delim)接受一个字符串并返回以delim为分隔符的子字符串列表。.strip()将删除字符串中的任何前导或尾随空格,并返回结果字符串。

你遇到的第一个问题是错误地拆分字符串。在x = [i.split() for i in sentence]中,你遍历了sentence的每个字符,而不是每个单词。所以,i.split()试图将单个字符按照空格拆分,这没有太多意义,所以它只返回包含该字符的列表。请看下面的示例:

>>> sentence = '7h3 qu1ck brown fox jumps ov3r 7h3 lazy dog'
>>> [i for i in sentence]
['7', 'h', '3', ' ', 'q', 'u', '1', 'c', 'k', ' ', 'b', 'r', 'o', 'w', 'n', ' ', 'f', 'o', 'x', ' ', 'j', 'u', 'm', 'p', 's', ' ', 'o', 'v', '3', 'r', ' ', '7', 'h', '3', ' ', 'l', 'a', 'z', 'y', ' ', 'd', 'o', 'g']
>>> sentence.split(' ')
['7h3', 'qu1ck', 'brown', 'fox', 'jumps', 'ov3r', '7h3', 'lazy', 'dog']

相反,你想要按每个空格字符(' ')拆分,以获取每个单词的列表:

x = sentence.split(' ')

回应你尝试做什么的问题:

我试图创建一个包含句子每个字符的列表。

你只需要将字符串转换为列表,例如:

>>> list('7h3 qu1ck brown fox jumps ov3r 7h3 lazy dog')
['7', 'h', '3', ' ', 'q', 'u', '1', 'c', 'k', ' ', 'b', 'r', 'o', 'w', 'n', ' ', 'f', 'o', 'x', ' ', 'j', 'u', 'm', 'p', 's', ' ', 'o', 'v', '3', 'r', ' ', '7', 'h', '3', ' ', 'l', 'a', 'z', 'y', ' ', 'd', 'o', 'g']

如果你首先想按每个单词拆分,然后获取每个单词的每个字符,那么可以这样做:

>>> [list(word) for word in '7h3 qu1ck brown fox jumps ov3r 7h3 lazy dog'.split()]
[['7', 'h', '3'], ['q', 'u', '1', 'c', 'k'], ['b', 'r', 'o', 'w', 'n'], ['f', 'o', 'x'], ['j', 'u', 'm', 'p', 's'], ['o', 'v', '3', 'r'], ['7', 'h', '3'], ['l', 'a', 'z', 'y'], ['d', 'o', 'g']]
英文:

.split(delim) takes in a string and returns a list of substrings that are between delim. .strip() will remove any leading or trailing whitespace from the string and return the resulting string.

The first issue you encountered was that you were splitting the string incorrectly. In x = [i.split() for i in sentence], you iterate over every character of sentence, not every word. So, i.split() is attempting to split a single character by nothing. This doesn't make much sense, so it just returns a list with that character. See this below:

>>> sentence = '7h3 qu1ck brown fox jumps ov3r 7h3 lazy dog'
>>> [i for i in sentence]
['7', 'h', '3', ' ', 'q', 'u', '1', 'c', 'k', ' ', 'b', 'r', 'o', 'w', 'n', ' ', 'f', 'o', 'x', ' ', 'j', 'u', 'm', 'p', 's', ' ', 'o', 'v', '3', 'r', ' ', '7', 'h', '3', ' ', 'l', 'a', 'z', 'y', ' ', 'd', 'o', 'g']
>>> sentence.split(' ')
['7h3', 'qu1ck', 'brown', 'fox', 'jumps', 'ov3r', '7h3', 'lazy', 'dog']

Instead, you want to split by each space character (' ') to get a list of each word:

x = sentence.split(' ')

In response to what you were trying to do:
> I was trying to make a list with each char of a sentence.

All you need to do is convert the string to a list with list(). For example:

>>> list('7h3 qu1ck brown fox jumps ov3r 7h3 lazy dog')
['7', 'h', '3', ' ', 'q', 'u', '1', 'c', 'k', ' ', 'b', 'r', 'o', 'w', 'n', ' ', 'f', 'o', 'x', ' ', 'j', 'u', 'm', 'p', 's', ' ', 'o', 'v', '3', 'r', ' ', '7', 'h', '3', ' ', 'l', 'a', 'z', 'y', ' ', 'd', 'o', 'g']

If you first want to split by each word, then get each character of of each word, then do this:

>>> [list(word) for word in '7h3 qu1ck brown fox jumps ov3r 7h3 lazy dog'.split()]
[['7', 'h', '3'], ['q', 'u', '1', 'c', 'k'], ['b', 'r', 'o', 'w', 'n'], ['f', 'o', 'x'], ['j', 'u', 'm', 'p', 's'], ['o', 'v', '3', 'r'], ['7', 'h', '3'], ['l', 'a', 'z', 'y'], ['d', 'o', 'g']]

huangapple
  • 本文由 发表于 2023年5月15日 06:09:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76249885.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定