2019-02-07
#字符串输出
str="hello world!!!"
print (str)
print(str[1:])
print(str[:2])
print(str[2:4])
print("end")
str="hello world!!!"
str1=‘17317753127‘
print("我手机号码是:{0},你好世界的英文是:{1}".format(str1,str))
list=["list",‘list‘,‘list‘]
list2=[1,2,3,3,3]
print(list,list2) 输出结果:[‘list‘, ‘list‘, ‘list‘] [1, 2, 3, 3, 3] 删除列表元素:del (list[1])
tup = (‘gogo‘,‘bibi‘,1,2,2)
tup2 = (1,2)
tup3=tup+tup2;print (tup3)
print ("tup3:{0}".format(tup3))
del (tup3)
dict={‘name‘:‘xu‘,‘phone‘:‘17317753127‘,‘age‘:26}
print (dict[‘name‘],dict[‘age‘])
print(‘全部输出:‘,dict)
#修改字典
dict[‘age‘]=28;print(dict[‘age‘])
#删除字典
del dict[‘name‘]
print(dict[‘name‘]) #删除字典name
dict.clear() #清空字典
del dict
print(dict)
a = set() #创建一个空集合
a = {‘a‘,‘b‘,‘c‘,‘a‘}
b = set((1,2,3,4))
print(a)#不会输出两个a,可以去重复元素。
#集合基本操作
a.add(‘x‘) # 添加元素
a.update(‘aa‘) #添加元素
a.remove(‘x‘) #移除元素,不存在会发生错误
b.discard(1) #移除元素,不存在会发生错误
a.pop() #随机删除元素
value=‘123‘
if value==‘123‘:
print(value)
else:print(‘456‘)
n=5
while n<=10:
n=n+1
print(n)
else: print(‘循环结束‘)
print(n)
for a in range(10):
print(a,end=‘,‘)
if a>=9:
print (‘结束循环‘)
c=set((1,2,3,4,5))
it=iter(c) #创建一个迭代器对象
for x in it:
print(x,end=‘,‘)
def trans(list): #矩阵转换函数(只适用于规则矩阵)
tran_end=[]
n=list
lenrow=n.__len__()
m=n.__iter__().__next__()
lencol=m.__len__()
print("此矩阵是{0}*{1}矩阵".format(lenrow,lencol))
print("转换前矩阵:",n)
print("····矩阵转换开始·····")
#tran_end=[[row[i] for row in n]for i in range(lencol)]
for i in range(lencol):
tran_end.append([row[i] for row in n])
print(‘····矩阵转换结束·····‘)
print("转换后矩阵:",tran_end)
trans(list1)
#不规则矩阵转换
list1=[[0,2],[1],[4,5,6],[]] #4*3矩阵
def change(list):
n = list
m=[]
maxrow=n.__len__()
for row in n:
a=row.__len__()
m.append(a)
maxcol=max(m)
print(maxrow,maxcol)
# 检测每一个row ,若一个row的len<maxcol 则填充元素直至达到maxcol
new_list=[];
for row_list in n:
for i in range(maxcol):
if row_list.__len__() < maxcol:
row_list.append(0)
#print(row_list)
print(row_list)
new_list.append(row_list)
print(new_list)
new_list=[[row[i] for row in new_list] for i in range(maxcol)]
print(new_list)
change(list1)
原文:https://www.cnblogs.com/hero-xu/p/10355246.html