import re
a = "k:1|k1:2|k2:3|k3:4"
key_list = []
value_list = []
ret = re.split(r"\||:",a)
for i in ret[1::2]:
value_list.append(i)
for j in ret[::2]:
key_list.append(j)
a_dict = dict(zip(key_list,value_list))
print(a_dict)
7、定义一个列表的操作类:Listinfo 包括的方法:
列表元素添加: add_key(keyname) [keyname:字符串或者整数类型]
列表元素取值:get_key(num) [num:整数类型]
列表合并:update_list(list) [list:列表类型]
删除并且返回最后一个元素:del_key()
classListInfo:def__init__(self):
self.list1 = []
defadd_key(self,keyname):
self.list1.append(keyname)
# print(new_list1)defget_key(self,num):return self.list1[num]
defupdate_list(self,list):
self.list1.extend(list)
def__iter__(self):'''表示对象可迭代'''
myiter = MyIter(self.list1)
return myiter
classMyIter(object):'''实现迭代器'''def__init__(self,mylist):
self.mylist = mylist
self.current_index = 0def__next__(self):if self.current_index < len(self.mylist):
self.current_index += 1return self.mylist[self.current_index-1]
else:
raise StopIteration
def__iter__(self):return self
mylist = ListInfo()
for i in range(10):
# print(i)
mylist.add_key(i)
for i in mylist:
print(i)
import re
a = input('字符串:')
ret = re.split(r" ",a)
for i in range(len(ret)):
ifnot ret[i].isalpha():
ret[i] = ret[i][-1] + ret[i][:-1]
new_str = ' '.join(ret)
b = new_str.split()
b.reverse()
c = ' '.join(b)
print(c)