编程
qq_37007819
这个作者很懒,什么都没留下…
展开
-
采用递归的方式查询字符索引
查询字符串部分字符索引,并返回索引成一个元组形式递归在字符串中的应用list1 = []def findall(str, s): # find()方法没找到返回-1 a = str.find(s, 0, len(str)) if a == 0: list1.append(a) elif a > 0: list1.append(list1[-1] + a + len(s)) else: print('没找到')原创 2020-06-20 20:40:55 · 132 阅读 · 0 评论 -
python-list的分配问题
list#8个学生随机分配到3个教室的问题import randomoffices = [[], [], []]names = ["a", "b", "c", "d", "e", "f", "g", "h"]flag = 1for i in names: index = random.randint(0, 2) offices[index].append(i)for temp in offices: print("教室%d有%d人:"%(flag,len(temp))原创 2020-06-14 20:07:55 · 696 阅读 · 0 评论 -
python中字符串的常见操作
python中字符串的常见操作字符串切片str = "123456789"# 取字符串的"123"str[0:3]# 取字符串"789"str[6:-1]# 取字符串"1357"str[0:-1:2]# 反转字符串str[::-1]字符串查找字符操作str = "hello world!"# find()返回值为存在为索引值,反之为-1str.find("hello",0,12)# 用index()方法查找,如果没有的话,程序会报异常str.index("hello",0,原创 2020-06-06 22:41:33 · 263 阅读 · 0 评论