Python 检测列表元素是否为多字
这段代码可以正常工作,但我不能100%确定它是如何工作的,因为它是在我借的一本Python书中。我不明白程序是如何检查某个东西是否是多字的。还有,星号线是什么意思Python 检测列表元素是否为多字,python,Python,这段代码可以正常工作,但我不能100%确定它是如何工作的,因为它是在我借的一本Python书中。我不明白程序是如何检查某个东西是否是多字的。还有,星号线是什么意思 places= ["Home","In-n Out Burger", "John's house", "Santa Monica Pier", "Staples center", "LA Dodgers stadium", "Home"] def placesCount(places): multi_word = 0
places= ["Home","In-n Out Burger", "John's house", "Santa Monica Pier", "Staples center", "LA Dodgers stadium", "Home"]
def placesCount(places):
multi_word = 0
count = 0
**while True:
place = places[count]**
if place == 'LA Dodgers stadium':
break
**if ' ' in place:**
multi_word += 1
count += 1
return count + 1, multi_word + 1
placesCount(places)
该方法检查列表
places
中的字符串是否有空格,它认为一个多字
如果列表places
包含字符串LA Dodgers stadium
,则该方法将返回字符串的位置,以及在此之前找到的多个单词的计数
这里有一个提示:当您将['LA Dodgers stadium']
传递给函数时会发生什么?它返回正确的数字吗
def placesCount(places):
multi_word = 0 # a count of how many multiple words were found
count = 0 # an initializer (not needed in Python)
while True: # start a while loop
place = places[count] # get the object from the places list
# at position count
if place == 'LA Dodgers stadium':
# quit the loop if the current place equals 'LA Dodgers stadium'
break
if ' ' in place:
# If the current string from the places list
# (which is stored pointed to by the name place)
# contains a space, add one to the value of multi_word
multi_word += 1
# Add one to count, so the loop will pick the next object from the list
count += 1
# return a tuple, the first is how many words in the list
# and the second item is how many multiple words (words with spaces)
return count + 1, multi_word + 1
if''in place:#如果in place字符串包含空格,请在值中添加一个空格。我不明白它是如何检测是否有空格的。如果没有使用FIND方法,如何查找空格。我需要在不使用查找方法的情况下完成我的程序。你在说什么查找方法?