unit_2_homework
随记2018/4/23
# 找元祖中的元素,移除每个元素的空格,并查找以a或A开头,c结尾的所有元素。
# 思路:将i取出来,求得li列表中有多少个元素for i in range(len(li));然后用li[i]的方式,让每个元素的空格去掉li[i] = li[i].replace(' ', '')
tu = ('alex', ' aric', 'Alex', 'Tony') # 设置元祖
tu = list(tu) # 将元祖列表化
tu_final = [] # 设置一个新的列表
for i in range(len(tu)): # 计算列表的长度,获得i——以便下一步中,取列表里面的每一个元素
tu[i] = tu[i].replace(' ', '') # 以此取列表的每一个元素,并且去掉空格
if tu[i][0] in ['a', 'A'] and tu[i][-1] == 'c': # 如果元素的首字母在a,A中,并且末尾为c
tu_final = tu_final.append(tu[i]) #### 那么将该元素添加到设置的空列表tu_final中——这里的语法是错误的,在列表中添加.append不应该进行赋值,因为.append()这个方法执行成功之后会返回None这个值,赋值的话会赋值None给变量。
print(tu_final) # 打印tu_final
今天在这个地方出现了好几个问题:
- 对各种括号代表什么数据类型弄不清楚;
- list语法后面应该使用小括号,而不是中括号;
- python如何合并两个列表的内容;
- Python startswith()方法;
逻辑 与 或 非的优先级(and or not 优先级)。
1. python中的括号——( ),[ ],{ }
python语言最常见的括号有三种,分别是:小括号( )、中括号[ ]和大括号也叫做花括号{ }。其作用也各不相同,分别用来代表不同的python基本内置数据类型。
- 1、python中的小括号( ):代表tuple元组数据类型,元组是一种不可变序列。创建方法很简单,大多时候都是用小括号括起来的。
>>> tup = (1,2,3)
>>> tup
(1, 2, 3)
>>>
>>> ()#空元组
()
>>>
>>> 55,#一个值的元组
(55,)
- 2、python中的中括号[ ]:代表list列表数据类型,列表是一种可变的序列。其创建方法即简单又特别,像下面一样:
>>> list('python')
['p', 'y', 't', 'h', 'o', 'n']
- 3、python大括号{ }花括号:代表dict字典数据类型,字典是由键对值组组成。冒号':'分开键和值,逗号','隔开组。用大括号创建的方法如下:
>>> dic={'jon':'boy','lili':'girl'}
>>> dic
{'lili': 'girl', 'jon': 'boy'}
>>>
2. python中列表如何合并?
正如下面的代码所示,将两个列表通过+号进行拼接即可。
>>> list1 = [1,2,3,4]
>>> list2 = [5,6,7,8]
>>> list1+list2
[1, 2, 3, 4, 5, 6, 7, 8]
3. Python中startswith()方法
描述
Python startswith() 方法用于检查字符串是否是以指定子字符串开头,如果是则返回 True,否则返回 False。如果参数 beg 和 end 指定值,则在指定范围内检查。
语法
startswith()方法语法:
str.startswith(str, beg=0,end=len(string));
参数
- str -- 检测的字符串。
- strbeg -- 可选参数用于设置字符串检测的起始位置。
strend -- 可选参数用于设置字符串检测的结束位置。
返回值
如果检测到字符串则返回True,否则返回False。
实例
以下实例展示了startswith()函数的使用方法:
#!/usr/bin/python
str = "this is string example....wow!!!";
print str.startswith( 'this' );
print str.startswith( 'is', 2, 4 );
print str.startswith( 'this', 2, 4 );
以上实例输出结果如下:
true
True
False
4. 逻辑 与 或 非的优先级(and or not 优先级)
- 在不加括号时候 and优先级大于or;
- x or y 的值只可能是x或y。x为真就是x,x为假就是y;
- x and y 的值只可能是x或y。x为真就是y,x为假就是x。
显然
对于,1 or 5 and 4:先算5 and 4,5为真, 值为4。 再算1 or 4,1 为真,值为1;
对于,(1 or 5) and 4:先算1 or 5,1为真, 值为1.。再算1 and 4,1为真,值为4。
unit_2_homework的更多相关文章
随机推荐
- 《DSP using MATLAB》示例Example6.6
代码: h = [1, 2, 3, 2, 1]/9; [C, B, A] = dir2fs(h) 运行结果:
- 在线编辑器KindEditor的使用
1.官网下载:点击进入 2.解压后目录说明 ├── asp asp示例 ├── asp.net asp.net示例 ├── attached 空文件夹,放置关联文件attached ├── examp ...
- C#封装的一个JSON操作类
using System; using System.Collections.Generic; using System.Collections; using System.Text; using S ...
- drone 学习四 几个有用的命令
1. 安装cli 工具 linux curl -L https://github.com/drone/drone-cli/releases/download/v0.8.5/drone_linux_am ...
- (QPS)TPS指标概述
性能测试关注指标-TPS概述 一.TPS(Transaction per Second)定义 TPS是Transactions Per Second 的缩写,也就是事务数/秒.它是软件测试结果的测量单 ...
- printf()_scanf()_取余运算符与取模运算符
基本的输入和输出函数的用法 printf();四种用法 1.printf("字符串\n"); 2.printf("输出控制符",输出参数); 3.printf( ...
- java代码----数据类型的转换-----int --->String
总结:int ----->String package com.a.b; //测试..char--->int // int--->String public class Yue2 { ...
- Ubuntu16下编译linux内核,报"mkimage" command not found错的解决
"mkimage" command not found - U-Boot images will not be built /work/system/linux-3.4.20/ar ...
- node中express的中间件之methodOverride
methodOverride中间件必须结合bodyParser中间件一起使用,为bodyParser中间件提供伪HTTP方法支持. index.html代码: <!DOCTYPE html> ...
- Centos下Apache+Tomcat集群--搭建记录
一.目的 利用apache的mod_jk模块,实现tomcat集群服务器的负载均衡以及会话复制,这里用到了<Cluster>. 二.环境 1.基础:3台主机,系统Centos6.5,4G内 ...