共計(jì) 286 個(gè)字符,預(yù)計(jì)需要花費(fèi) 1 分鐘才能閱讀完成。
在 Python 中,可以使用循環(huán)或列表推導(dǎo)式來匹配數(shù)組中的值。
- 使用循環(huán):
array = [1, 2, 3, 4, 5]
value = 3
for item in array:
if item == value:
print(" 匹配到值 ", value)
break
- 使用列表推導(dǎo)式:
array = [1, 2, 3, 4, 5]
value = 3
matches = [item for item in array if item == value]
if len(matches) > 0:
print(" 匹配到值 ", value)
上述代碼將匹配數(shù)組 array
中的值是否等于給定的value
。如果匹配到值,則會(huì)打印出匹配到的值。
丸趣 TV 網(wǎng) – 提供最優(yōu)質(zhì)的資源集合!
正文完