简体   繁体   English

Python function 用于判断字符串中的两个单词是否以相同的字母开头

[英]Python function for determining whether two words in a string start with the same letter

this is my first post here and I am a complete beginner(only been coding for a week or so) so please pardon me if Ive made some very dumb mistakes这是我在这里的第一篇文章,我是一个完整的初学者(只编码了一周左右)所以如果我犯了一些非常愚蠢的错误,请原谅我

Write a function takes a two-word string and returns True if both words begin with the same letter(capitalisation does not matter) and false otherwise写一个 function 接受一个双字字符串,如果两个单词以相同的字母开头(大写无关紧要)则返回 True,否则返回 false

SOLUTION:解决方案:

def prob2(b):
for p,q in b.split():
    if p[0] == q[0]:
        return(True)
    else:
        return(False).

If I try it with a string like 'hello world', I get the error message:如果我尝试使用像“hello world”这样的字符串,我会收到错误消息:

too many values to unpack, expected 2太多值无法解压,预计 2

But if I try it with a string like 'dc' i get the desired output, which makes it obvious that its splitting the string after each letter, not on the whitespace, which is weird, because i did not make any changes and by default it should be splitting on the whitespace.但是如果我用像'dc'这样的字符串尝试它,我会得到所需的output,这很明显它在每个字母之后分割字符串,而不是在空格上,这很奇怪,因为我没有做任何更改,默认情况下它应该在空白处分裂。

Please tell me how I can fix this, thanks!请告诉我如何解决这个问题,谢谢!

b.split() returns a list of space-separated words. b.split()返回以空格分隔的单词列表。 Break this into component steps:将其分解为组件步骤:

seq = b.split()
p, q = seq[0]
if p[0] == q[0]:
    return True
else:
   return False

This will work only when the first element of seq is some iterable of length 2. In short, a 2-letter string will work, comparing those two letters... d and c in your example.seq的第一个元素是长度为 2 的可迭代元素时才有效。简而言之,一个 2 个字母的字符串将起作用,在您的示例中比较这两个字母... dc

Following the above break-down in your two-word case, we get the attempted assignment在您的两个单词的情况下进行上述分解之后,我们得到了尝试的分配

p, q = "hello"

This tries to unpack 5 characters to two variables, causing your error.这会尝试将 5 个字符解压缩为两个变量,从而导致您的错误。

Simply splitting a two-word string requires no loop, as others have shown:正如其他人所展示的那样,简单地拆分两个单词的字符串不需要循环:

p, q = b.split()

Or, safer yet, simply slice off the first two words:或者,更安全的是,只需去掉前两个词:

p, q = seq[:2]
seq = seq[2:]     # remove the first two words from the `split` sequence

Also, note that your return can be much simpler: you already evaluated the Boolean result you want in the if expression.另外,请注意,您的返回可能要简单得多:您已经在if表达式中评估了您想要的 Boolean 结果。

p, q = seq[:2]
return p[0] == q[0]

it may have more items in the list, when you split the string by white space.当您用空格分割字符串时,列表中可能有更多项目。 you could print it out to see你可以打印出来看看

def prob2(b):
    p,q = b.split()
    return p[0].lower() == q[0].lower()

for p,q in b.split() is not doing what you think. for p,q in b.split()没有按照您的想法进行。

Use p,q = b.split() instead.请改用p,q = b.split()

When doing for p,q in b.split() this need that every element returned from the split() can be unpack in 2, this is the case of dc->d,c but each word of Hello Word canno't be unpacked in 2 for p,q in b.split()需要从split()返回的每个元素都可以在 2 中解包,这是dc->d,c的情况,但Hello Word的每个单词不能拆包 2

Also you don't need to loop, as you need to look only at the first char, just do您也不需要循环,因为您只需要查看第一个字符,只需执行

def prob2(content):
    words = content.split()
    return len(words) == 2 and words[0].lower() == words[1].lower()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM