今天总结了这几天所学的基础知识,做了一些练习题,都是一些常用操作:
#1:请用代码实现:利用下划线将列表的每一个元素拼接成字符串 # li = ["alex","eric","rain"] # '_'.join(li) #2:查找列表中元素,移除每个元素的空格,并查找以A或a开头并且以c结尾的所有元素 # li = ['alec','aric','Alex',' Tony','rain'] # tu = ('alec','aric','Alex',' Tony','rain') # dic = {'k1':'alex','k2':' aric','k3':'Alex','k4':'Tony'} # for index,i in enumerate(li): # i = i.strip() # li[index] = i # if i.endswith('c') and (i.startswith('a') or i.startswith('A')): # print(i) # print(li) # print("------------------------------------") # # for i in tu: # i = i.strip() # if i.endswith('c') and (i.startswith('a') or i.startswith('A')): # print(i) # print(tu) # print("------------------------------------") # # for index,i in enumerate(dic): # dic[i] = dic[i].strip() # if dic[i].endswith('c') and (dic[i].startswith('a') or dic[i].startswith('A')): # print(i) # print(dic) # print("------------------------------------") #3:有如下列表,按照要求实现每一个功能 # li = ['alex','eric','rain'] # #计算列表长度并输出 # print(len(li)) # #列表中追加一个元素,并输出添加后的列表 # li.append('brues') # print(li) # #在指定位置插入元素 # li.insert(1,"blues") # #修改指定元素 # li[2] = 'Kelly' # #删除指定元素 # del li[1] # del li[2:4] # print(li) # #将列表的所有元素反转 # li.reverse() # print(li) # #输出索引及元素 # for index,i in enumerate(li): # print(index,i) #4:有如下列表,按照需求实现功能 # li = ['hello','secen',['non',['h','kelly'],'all'],123,446] # #请根据索引输出指定的元素 # print(li[2][1][1]) # #找到指定元素并且修改 # li[2][2] = 'ALL' #5;有如下元组,请按照要求实现指定功能 # tu = ('alex','eric','rain') # #计算长度 # print(len(tu)) # #获取指定元素 # print(tu[1:2]) # #输出 # for index,i in enumerate(tu): # print(index,i) #6:字典 # dic = {'k1':'v1','k2':'v2','k3':'v3','k4':'v4','k5':[123,4234,24]} # #输出所有的key和value # for i in dic.keys(): # print(i,end=' ') # print(dic[i]) # #追加 # dic['k6'] = 'v6' # print(dic) # dic['k5'].append(44) # dic['k5'].insert(1,18) # print(dic) #7:转换 # #将字符串转换为列表 # s = 'alex' # s = list(s) # print(s) # print(tuple(s)) # #将列表转换为元组 # li = ['alex','seven'] # print(tuple(li)) # #将列表转换为字典 # new_dict = {} # for index,i in enumerate(li): # new_dict[index] = i # print(new_dict) #8:按要求创建字典 # li = [11,22,33,44,55,66,77,88,99] # new_dict = {'k1':[],'k2':[]} # for index,i in enumerate(li): # if i >=66: # new_dict['k1'].append(i) # else: # new_dict['k2'].append(i) # print(new_dict) #9:乘法表 # for i in range(1,10): # for j in range(1,i+1): # print("%s * %s"%(i,j),end=' ') # print('\n')