\u7b2c3.0 \u8bb2\u6570\u636e\u7ed3\u6784-\u5217\u8868\u57fa\u7840(2).pptx - Python \u6570\u636e\u7ed3\u6784-list \u4e1b\u57f9\u76db \u5217\u8868 List \u5217\u8868\u751f\u6210\u65b9\u6848 1 \uff0c\u76f4\u63a5\u7528 \u5217\u8868 list \u662f\u4e00\u4e2a

第3.0 讲数据结构-列表基础(2).pptx - Python...

This preview shows page 1 - 10 out of 28 pages.

Python 数据结构 --list 丛培盛
Image of page 1
2 Tongji University 列表 List 列表生成方案 1 ,直接用 [ ] 列表 list 是一个 [] 中的一组数据,这组数据可以是同类 型,也可是不同类型 x=[1, 2,3,” 小王” ,”anna”]
Image of page 2
3 Tongji University 列表 List 列表生成方案 2 list 函数配合 range 函数 range 函数产生列表 , 格式为: range(start, end, step) 其中 start step 可以省略, step 省略,则其为 1 start 省略,则认为其为 0 例如 list(range(1,10)) 产生 [1,2,…, 9] 的列表 range(1,10,3) 产生 [1,4,7] Range 确定的范围为 [ ) 左端闭区间,右端开区间
Image of page 3
4 Tongji University 列表推导式快速求列表 列表生成方案 3 :列表推导式 例子 求 0—9 每个数字平方的列表 li= [x*x for x in range(10)] [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] 0—9 中可以被 3 整除的数字的平方列表 li= [x*x for x in range(10) if x%3==0] [0, 9, 36, 81]
Image of page 4
5 Tongji University 列表元素的调用 列表通过 表名 [ 下标 ] 下标从左到右 0~n-1 ;从右向左 -1~-n >>> x=[1, 2,3,' 小王 ','anna'] >>> x[0] 1 >>> x[-1] 'anna' >>> [ 索引 ] 方式引用
Image of page 5
6 Tongji University 列表运算 ---- 分片 与字符串一样 采用 [m:n:step] 的形式,左闭右开 m 省略,取 0 n 省略,取最大值 step 省略,取 1 全部省略,取整个表,既拷贝
Image of page 6
7 Tongji University 列表的拷贝与引用 li= [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] a=li # 引用,两者是一样的 a=li[:] # 拷贝 比较两者差异 测试: a[0]=-100
Image of page 7
8 Tongji University 列表的加法和乘法—与字符串一样 + >>> x [1, 3, 23, 22, 45, 90] >>> y ['bob', 'alice', 'storm'] >>> x+y [1, 3, 23, 22, 45, 90, 'bob', 'alice', 'storm'] * >>> y*3 ['bob', 'alice', 'storm', 'bob', 'alice', 'storm', 'bob', 'alice', 'storm']
Image of page 8
9 Tongji University 空列表 无元素的空表 >>> x=[ ] 每个元素都是空的列表 Python 提供
Image of page 9
Image of page 10

You've reached the end of your free preview.

Want to read all 28 pages?

  • Spring '09
  • FAREWELL

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes