[Python3 填坑] 007 多才多艺的 len()

目录


1. print( 坑的信息 )

  • 挖坑时间:2019/01/10
  • 明细
坑的编码 内容
Py006-1 len() 的使用场合



2. 开始填坑

(1) 总的来说

  • len() 返回容器中的项目数
  • 在某些对象中会包含对其它对象的引用,这样的对象被称作容器(containers)
  • 简单地说,Python 中常见的数据结构,如 string、list、tuple、set、dict 等均为容器
  • frozenset 也是容器,还有一些我目前还没学到,如 deque、defaultdict、namedtuple 等

(2) 举例说明

# 这更像是一种总结吧

str1 = "I am YorkFish."
list1 = ['I', 'am', 'YorkFish']
tuple1 = ('I', 'am', 'YorkFish')
set1 = {'I', 'am', 'YorkFish'}
dict1 = {1:'I', 2:'am', 3:'YorkFish'}

print("len(str1) =", len(str1))
print("len(list1) =", len(list1))
print("len(tuple1) =", len(tuple1))
print("len(set1) =", len(set1))
print("len(dict1) =", len(dict1))
  • 运行结果

len(str1) = 14
len(list1) = 3
len(tuple1) = 3
len(set1) = 3
len(dict1) = 3


(3) 后记

不止是 len(),如 count()、index() 等都是“多才多艺”的。


我的学识有限,如果有同学、老师或者前辈看到我写的东西,发现错误之处,还请不吝赐教!谢谢!

猜你喜欢

转载自www.cnblogs.com/yorkyu/p/10316052.html