新风花雪月 发表于 2018-8-6 13:20:16

Python字符串,列表

  字符串:
  字符串的创建:
  单引号, 双引号 ,三引号 <注意: 转义字符的使用>
  字符串的特殊性:
  索引切片 连接 重复成员操作符(in, not in)
  字符串的常用方法:
  1). 判断字符串的类型(isdigit, isspace, isupper,islower ......)
  2). 字符串开头结尾的判断(endwith,startwith)
  endwith -- 多用于查找指定的文件格式(.log, .png......)
  startwith -- 所用于判断使用的协议(http://, https://, ftp://)
  3). 去除空格(left,middle,right)
  lstrip#去除左边空格
  replace (&quot; &quot;, &quot;&quot;) # 使用replace函数间接替换中间的空格
  rstrip#去除右边空格
  strip   #去除左右两边空格
  4).居中 , 居左, 居右
  center
  ljust
  rjust
  

    5). 切片  split()
  6). 连接
  join()
  

  内置函数:
  cmp max min enumerate zip sum len abs
  数值,字符串是不可变数据类型: 列表是可变数据类型
  测试练习:
  2017-好未来-笔试编程题


[*]  题目描述:
  输入两个字符串,从第一字符串中删除第二个字符串中所有的字符。例如,输入”They are students.”和”aeiou”,则删除之后的第一个字符串变成”Thy r stdnts.”

[*]  输入描述:
  每个测试输入包含2个字符串

[*]  输出描述:
  输出删除后的字符串

[*]示例1:
  

输入  They are students.
  aeiou
  
输出
  
Thy r stdnts.
  

#!/usr/bin/env python  
#coding:utf-8
  
s1 = raw_input ("please input your first string:")
  
s2 = raw_input ("please input your second string:")
  
for i in s2:
  s1=s1.replace(i,"")
  
else:
  print s1
  

  

  测试结果:
  2017-小米-句子反转


[*]  题目描述:

  给定一个句子(只包含字母和空格), 将句子中的单词位置反转,单词用空格分割, 单词之间只有一个空格,前后没有空格。 比如: (1) “hello xiao mi”-> “mi xiao hello”


[*]  输入描述:

  输入数据有多组,每组占一行,包含一个句子(句子长度小于1000个字符)


[*]  输出描述:

  对于每个测试示例,要求输出句子中单词反转后形成的句子


[*]示例1:
  

- 输入  hello xiao mi
  
- 输出
  mi xiao hello
  

  #!/usr/bin/env python
  #coding:utf-8
  print &quot; &quot;.join(raw_input(&quot;please in put your first string:&quot;).split()[::-1])
  

测试结果:  
![](http://i2.51cto.com/images/blog/201803/18/d07ff4e4a83878f51f96ccfb1c4ada48.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
  

  
华为测试:
  
题目描述:输入一行字符串,分别统计出包含英文字母,空格,数字和其他字符的个数
  

  #!/usr/bin/env python
  #coding:utf-8
  s = raw_input(&quot;请输入字符:&quot;)
  al_count, space_count, num_count, other_count = 0, 0, 0, 0
  for i in s:
  if i.isalpha():
  al_count += 1
  elif i.isspace():
  space_count += 1
  elif i.isdigit():
  num_count += 1
  else:
  other_count += 1
  print &quot;英文字母个数:%d\n空格个数:%d\n数字个数:%d\n其他字符个数:%d&quot; % (
  al_count, space_count, num_count, other_count)
  

测试结果:  
![](http://i2.51cto.com/images/blog/201803/18/a7fe6d2648487416412ad7562770f7d4.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
  

  
枚举,zip
  

  #!/usr/bin/env python
  #coding:utf-8
  s = &quot;hello&quot;
  for i in range(len(s)):
  print i,s
  

测试结果:  
![](http://i2.51cto.com/images/blog/201803/18/6265339f9db37d71ce9d78bd435b4ce9.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
  

  
enumerate内置函数枚举;
  
![](http://i2.51cto.com/images/blog/201803/18/1ac49d6feccfad85077d3bf1a037189f.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
  

  
zip内置函数;
  
![](http://i2.51cto.com/images/blog/201803/18/2b39e4c9cbaa96c517666afb227ec486.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
  

  
List 列表
  
列表特性:
  

  #!/usr/bin/env python
  #coding:utf-8
  array =
  li =
  

#索引:正向索引和反向索引片  

  print li[::-1]
  

  
#重复
  

  print li * 3
  

  
#连接
  

  print array + li
  

  
#成员操作符
  

  print 1 in li
  print 1 not in li
  

  
结果输出:
  
![](http://i2.51cto.com/images/blog/201803/18/ba67f09d4cf8ddb63e4637d06d1f1db0.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
  

  
列表增删查改
  

  #!/usr/bin/env python
  #coding:utf-8
  allow_ip = ['172.25.254.91', '172.25.254.2', '172.25.254.14', '172.25.254.32']
  

# 增  
#append追加元素到列表的最后;
  

  #allow_ip.append('172.25.254.6')
  #print allow_ip
  

  
#insert将元素添加到指定索引的前面;
  

  #allow_ip.insert(1,'172.25.254.34')
  #print allow_ip
  

  
#extend是追加多个元素到列表中;
  

  #allow1_ip = ['172.25.254.56', '172.25.254.66']
  #allow_ip.extend(allow1_ip)
  #print allow_ip
  

  
#改
  
#给列表的指定索引重新赋值;
  

  #allow_ip = '172.25.254.11'
  #print allow_ip
  

  
#查
  
#显示指定元素出现的次数;
  

  #print allow_ip.count('172.25.254.1')
  

  
#显示指定元素的索引值;如果出现多个,显示小的那个索引;如果元素不存在,报错,ValueError;
  

  #print allow_ip.index('172.25.254.1')
  

  
#删
  
#pop是删除指定索引的值;如果列表为空或者索引不在范围内,则报错; 如果不指定索引值,默认删除最后一个元素;
  
#allow_ip.
  
#删除列表中最先出现的值,不存在,则报错;
  

  #allow_ip.remove('172.25.254.1')
  #print allow_ip
  

  
#反转列表
  

  #allow_ip.reverse()
  

  
#列表排序
  

  allow_ip.sort()
  print allow_ip
  

  
测试练习:
  
多用户登陆系统:
  1). 已知多个用户名和密码;
  2). 判断用户名是否存在,
  如果登陆的用户不存在,则报错, 清注册用户;
  如果用户存在, 则判断密码是否正确:
  如果正确, 输出用户登陆成功;
  如果不正确, 输出登陆失败;
  

  

  #!/usr/bin/env python
  #coding:utf-8
  import getpass
  users = ['user1', 'user2', 'user3']
  passwds = ['123', '456', '123']
  inuser = raw_input(&quot;用户名:&quot;)
  inpass = getpass.getpass(&quot;密码:&quot;)
  #if 用户是否存在(成员操作符):
  if inuser in users:
  index = users.index(inuser)
  passwd = passwds
  if passwd == inpass:
  print &quot;%s登陆成功.....&quot; %(inuser)
  

##1).找出用户名的索引值  
##2).取出对应用户名的密码
  
##3).判断是否一致
  
#if 密码正确:
  #pass
  
else:
  print "密码不正确..."
  

  else:
  print &quot;%s 不存在,清注册.....&quot; %(inuser)
  

  
测试结果:
  
因为导入getpass。在测试时必须用终端进行。
  
![](http://i2.51cto.com/images/blog/201803/18/2783a8fae766bf49e8261718bfe51e25.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
  

  
测试练习:
  
批量生成学号:
  
学号前4位为1705
  依次有三个学院:电子工程学院(01), 软件学院(02), 商学院(03)
  每个学院有200人;
  
#!/usr/bin/env python
  
#coding:utf-8
  
for i in['01', '02', '03']:
  for j in range(1,201):
  print "1705%s%.3d" %(i,j)
页: [1]
查看完整版本: Python字符串,列表