# 给定一个数组[1,2,3,[[4,5,6],7,8],9],转化为[1,2,3,4,5,6,7,8,9]
x =[1,2,3,[[4,5,6],7,8],9]
y =[]deffun1(listname):for i in listname:ifisinstance(i,list):
fun1(i)else:
y.append(i)return y
z = fun1(x)print(z)
# 给定一个数组['1a','2b','3c','4d','5a'],输出出现次数最多的字符前面的数字之和import re
x =['1a','2b','3c','4d','5a']
list1 =[]
list2 =[]# 将数组每一个分割成为数字和字符,单独隔开,如‘1a’,分割成‘1’,‘a’,最后list1组成数字组,list2组成字符组deffun2(listname):
result =0for i in listname:
pattern = re.compile(r'(\d+)(\D+)')
result1 = pattern.match(i)
list1.append(result1.group(1))
list2.append(result1.group(2))
k = fun3(list2)for j inrange(len(list2)):if list2[j]== k:
result +=int(list1[j])print(result)deffun3(listname):
s =set(listname)
count ={}for i in s:
count[i]= listname.count(i)
maxvalue =max(count.values())for k,j in count.items():if j == maxvalue:return k
fun2(x)
#给定一个二维数组[['A','B'],['1','2'],['a','b']],将所有的排列顺序表示出来
x =[['A','B'],['1','2'],['a','b']]#将二维数组进行翻转,然后将后面两个弹出来,进行fun2()排序合并操作,将返还的值加入数组中,直至数组的长度为1deffun1(listname):
listname.reverse()while(len(listname)!=1):
y = listname.pop()
z = listname.pop()# print(y,z)
h = fun2(y,z)
listname.append(h)print(listname)deffun2(y,z):
h =[]for i in y:for j in z:
temp = i+j
h.append(temp)return h
fun1(x)