新闻资讯

质量为本、客户为根、勇于拼搏、务实创新

< 返回新闻资讯列表

python怎么匹配数组里的值,python如何匹配某一字符串

发布时间:2023-11-02 23:28:36

python怎样匹配数组里的值

在Python中,可使用循环或列表推导式来匹配数组中的值。

  1. 使用循环:
array = [1, 2, 3, 4, 5]
value = 3

for item in array:
    if item == value:
        print("匹配到值", value)
        break
  1. 使用列表推导式:
array = [1, 2, 3, 4, 5]
value = 3

matches = [item for item in array if item == value]
if len(matches) > 0:
    print("匹配到值", value)

上述代码将匹配数组array中的值是否是等于给定的value。如果匹配到值,则会打印出匹配到的值。