python培训之有几种循环遍历的方法?

发布时间:2023-03-31 09:31:42 人气:66 作者:多测师

  python有几种循环遍历的方法?

  1、for-in可以用来遍历数组与字典。

  words = ['cat', 'window', 'defenestrate']

  for w in words:

  print(w, len(w))

python培训之有几种循环遍历的方法?

  # 使用数组访问操作符,能够迅速地生成数组的副本

  for w in words[:]:

  if len(w) > 6:

  words.insert(0, w)

  # words -> ['defenestrate', 'cat', 'window', 'defenestrate']

  2、如果希望使用数字序列进行遍历,可以使用Python内置的range函数。

  a = ['Mary', 'had', 'a', 'little', 'lamb']

  for i in range(len(a)):

  print(i, a[i])

  以上就是python2种循环遍历的方法,希望对大家有所帮助。更多Python学习指路:请关注多测师。https://www.e70w.com/xwzx/


返回列表
在线客服
联系方式

热线电话

17727591462

上班时间

周一到周五

二维码
线