2019-04-27

Python第二天

  • 字符串操作
    1.find
index5 = my_str.rfind('neuedu')
print(index5)

2.replace 字符串替换,若没有第三个参数,则会全部替换

name = 'hello world haha'
new_name = name.replace('ha','tong',1)
print(new_name)
price = "¥500"
price = price.replace('¥',599)
print(price)

3.split

name = 'hello world haha'
name_list = name.split(" ")
print(name_list)
name_list1 = name,split(" ",2)
name.split(" ",2)
print(name_list1)

4.join

str4 = " "
list1 = ['my','name','is','suntongtong']
my_name = str4.join(list1)
print(my_name)

5.strip
lstrip去空格,清除左边空格
rstrip 去空格,清除右侧空格
strip()删除两端的空白字符

#lstrip去空格,清除左边空格
print(new_my_str_space)
print(len(new_my_str_space))
new_str2 = new_my_str_space.lstrip()
print(new_str2)
print(len(new_str2))
#rstrip 去空格,清除右侧空格
new_str3 = new_str2.rstrip()
print(new_str3)
print(len(new_str3))
#strip()删除两端的空白字符
#\t \n
str1 = '\n\tdddddd\t'
print(new_my_str_space2)
print(len(new_my_str_space2))
new_str4 = new_my_str_space2.strip()
print(new_str4)
str2 = str1.strip()
print(len(str2))

6.split

name = 'hello world haha'
name_list = name.split(" ")
print(name_list)
name_list1 = name,split(" ",2)
name.split(" ",2)
print(name_list1)
  1. 以str为分隔符,分割name,如果maxsplit指定值,那么仅分割maxsplit个自字符串
my_str = 'hello world haha'
my_str2 = my_str.capitalize()
print(my_str2)

8.title
title()把字符串每个单词首字母大写

my_str3 = my_str.title()
print(my_str3)

9.startwith检测字符串是否以str为开始

my_str4= my_str.startswith('hello')
print(my_str4)
my_str5= my_str.startswith('Hello')
print(my_str5)

10.endswith()检测字符串是否以str为结尾

my_str.endswith('cpp')
my_str6 = my_str.endswith('cpp')
print(my_str6)

11.upper把字符串的所有字母变为大写

my_str7 = my_str.upper()
print(my_str7)

12.lower把字符串的所有字母变为小写

my_str8 = my_str7.lower()
print(my_str8)

ps:字符串不可变,修改之后创建了新的字符串
13.ljust/rjust 返回一个原字符串左对齐/右对齐,并用空格填充width长度的新字符串

my_str_space = 'hello'
new_my_str_space = my_str_space.rjust(10)
print(len(new_my_str_space))
print(new_my_str_space)
new_my_str_space1 = my_str_space.ljust(100)
print(new_my_str_space1)
print(len(new_my_str_space1))

14.partition 把mystr以str分割成3部分,str前,str,str后

print(my_str)
t_mystr = my_str.partition('neuedu')
print(t_mystr)#元组

15.splitlines 按照行分割,返回一个包含各行作为元素的列表

line = 'hello\nworld'
print(line)
list_line = line.splitlines()
print(list_line)

16.isalpha判断字符串是否都是字母 True

alpha = my_str.isalpha()
print(alpha)
alpha2 = 'ddddddd'
alpha3 = alpha2.isalpha()
print(alpha3)
  • 列表操作
    1.列表和c中的数组很像,但是,list可以存储不同类型的数据
# name_list = ['鲁班','xiaoming',1024]
# print(name_list)
# print(type(name_list))

2.访问及遍历

#访问
 print(name_list[0])
#遍历
 for x in name_list:
     print(x)
i = 0
while i < len(name_list):
     print(name_list[i])

3.添加

name_list = ['鲁班','xiaoming',1024]
print('添加前',name_list)
str = input('请输入您要添加的内容')
name_list.append(str)
print('添加后',name_list)

insert 在指定位置index前插入

 a = [0,1,2]
 a.insert(1,10)
 print(a)

extend可将另外一个集合主意添加到列表中

a = [1, 2]
 b = [3, 4]
a.append(b)
 print(a)
a.extend(b)
print(a)

4.修改

name_list = ['鲁班','xiaoming',1024]
name_list[1] = '小明'
print(name_list)

5.删除
del:根据下标进行删除
pop 删除最后一个元素
remove 根据元素的值进行删除

name_list = ['鲁班','xiaoming',1024]
print('添加前',name_list)
del name_list[0]
print('删除后',name_list)
#pop 删除最后一个元素
name_list.pop()
print('删除后',name_list)
#remove 根据元素的值进行删除
name_list.remove()
print('删除后',name_list)

6.列表的排序
from modenam import name1, name2

num = randint(-10,10)#[-10,10]
print(num)
num_list = []
for i in range(10):
     num_list.append(randint(1,20))
print(num_list)
num_list.sort()
print('正序排序',num_list)
num_list.sort(reverse=Ture)
print('逆序排序',num_list)

new_list = sorted(num_list)
sorted(num_list)
print(num_list)

区别
(1)sort对原来的列表进行修改排序 sorted(num_list)返回新的,原来的没有改变
(2).sort属于列表的成员方法,sorted对所有可迭代对象进行操作

7.列表里的嵌套,列表里还有列表

school_name = [['qinghua','beida'],['nankai','tianda'],['dongqin','nankai']]
print(school_name)
print('qqqqqqqqqqqqqqq')
print(school_name[0,1])

8.列表推导式 :所谓的列表推导式就是指轻量级循环创建列表

list1 = []
for _ in range(10)
     list1.append('不给我鲁班我就送')
    print(list1)
list2 = [i for i in range(10)]
print(list2)
list3 = ["不给我鲁班我就送"for _ in range(10)]
print(list3)
from random import randint
#生成十个元素范围在【-10,10】区间的列表
l = [randint(-10,10)for _ in range(10)]
print(l)
#选出大于等于0的数据
res = []
for x in l:
    if x >= 0:
        res.append(x)
        print('使用for循环筛选:',res)
#循环过程中使用if
res2 = [x for x in l if x >=0]
print('使用列表解析筛选:',res2)

9.列表转化成字符串

my_list = ['welcome','to','ML','World']
my_list_to_str = str(my_list)
print(my_list_to_str)
print(' '.join(my_list))

10.列表和字符串的*

str1 = 'hehe'*3
print(str1)
list4 =['6',9,0,3,14]*5
print(list4)
  • 分配工位
#一个学校,三个办公室,8位老师等待工位分配
import string.randint as randint
#定义3个办公室
offices = [[],[],[]]
#定义八位老师
names = string.ascii_uppercase[:8]
#names = list(names)
print(names)
for name in names:
    #产生一个随机数【0,2】
    index = randint(0,2)
    offices[index]. append(name)
    print(offices)
for tempNames in offices:
    print('办公室{}的人数为{}'。format(i,len(tempNames)))
    i += 1
    for name in tempNames:
        print('成员为{}'.format(name),end='')
    print('-'*30)    

你可能感兴趣的:(2019-04-27)