博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【Python基础】lpthw - Exercise 38 列表的操作
阅读量:7153 次
发布时间:2019-06-29

本文共 1865 字,大约阅读时间需要 6 分钟。

  1.列表和字符串操作的混合练习

1 ten_things = "apples  oranges crows telephone light sugar" 2  3 print("Wait there are not 10 things in that list. Let's fix it.") 4  5 stuff = ten_things.split() 6 more_stuff = ["day","night","song","frisbee","corn","banana","girl","boy"] 7  8 while len(stuff) != 10: 9     next_one = more_stuff.pop()10     print("Adding:",next_one)11     stuff.append(next_one)12     print(f"There are {len(stuff)} items now.")13 14 print("There we go:",stuff)15 16 print("Let's do some things with stuff")17 18 print(stuff[1])19 print(stuff[-1])20 print(stuff.pop())21 print(' '.join(stuff))22 print('#'.join(stuff[3:5]))

  输出

Wait there are not 10 things in that list. Let's fix it.Adding: boyThere are 7 items now.Adding: girlThere are 8 items now.Adding: bananaThere are 9 items now.Adding: cornThere are 10 items now.There we go: ['apples', 'oranges', 'crows', 'telephone', 'light', 'sugar', 'boy', 'girl', 'banana', 'corn']Let's do some things with stufforangescorncornapples oranges crows telephone light sugar boy girl bananatelephone#light
View Code

  2. 列表的索引和操作

  参考:

  python在设计索引下标的时候都是采用左闭右开区间的形式,是个值得注意的小细节。

1 list = ['a','b','c','d']2 x1 = list[0]      # x1 = a3 x2 = list[-1]     # x2 = d 此时 list = ['a','b','c','d']4 x3 = list.pop()  # x3 = d 此时 list = ['a','b','c']5 x4 = list[1:2]    # x4 = b 注意python中索引时[]为左闭右开区间

  list.pop()表示取出列表最后一个元素返回给当前被赋值的变量。

  3. str.join(sequence)

  返回被str连接的sequence序列,如

1 str = "-"2 sequence = ["a","b","c"]3 print(str.join(sequence))

  输出为

  a-b-c

  一般sequence是一个列表,如果它是一个字符串,则将字符串中每个字符视为一个元素并用str去连接它们,如

1 str = "-"2 sequence = "test"3 print(str.join(sequence))

  输出为

  t-e-s-t

  4.进一步理解python的列表(作为一种常见的数据结构)

  有序;

  可随机访问,也可线性访问(使用索引);

  查找时需要依次检索

  5.巩固练习-概念理解

  面向对象编程,object oriented programming(OOP)

  python中的类(class)

  函数式编程,functional programming

 

转载于:https://www.cnblogs.com/cry-star/p/10649124.html

你可能感兴趣的文章
Hash表 hash table 又名散列表
查看>>
.Net工程详解及项目版本管理
查看>>
win7安装apache或者php 5.7缺少vcruntime140.dll的问题
查看>>
Guava 的学习
查看>>
ie-css3.htc 可以让IE低版本浏览器支持CSS3 的一个小工具
查看>>
java 中的equal和"=="
查看>>
Linux 查找指定名称的进程并显示进程详细信息
查看>>
git push上传代码到gitlab上,报错401/403(或需要输入用户名和密码)
查看>>
Delphi图像处理 -- 文章索引
查看>>
【转】WCF入门教程三[WCF的宿主]
查看>>
JSTL与EL与OGNL
查看>>
启动Tomcat服务时,出现org.apache.catalina.startup.VersionLoggerListener报错
查看>>
利用scons构建project
查看>>
Flash-制作空心文字
查看>>
Android高效率编码-细节,控件,架包,功能,工具,开源汇总,你想要的这里都有...
查看>>
防DNS劫持教程,手动修复本地DNS教程
查看>>
java.net.ServerSocket 解析
查看>>
机器学习就业学习计划,从零开始,全面涵盖机器学习重要知识点学习计划
查看>>
浅谈配置文件:spring-servlet.xml(spring-mvc.xml) 与 applicationContext.xml
查看>>
Django使用Pillow制作验证码
查看>>