MyList = [tuple(i for i in j if type(i) != str ) for j in MyList]
result是list中的元组,例如:
[(X,Y), (X2,Y2)]
这里重要的是理解列表的推导,而不是你想要的。
这听起来像是某种形式的课程作业,以确保你理解这一行发生了什么;-)
MyList = [(1,2,3,(1,2), "hello world"),("hello"),(3,4,1),"world"]
comp = [tuple(i for i in j if type(i) != str ) for j in MyList]
print(comp)
newList = []
for j in MyList:
newTup = []
for i in j:
if type(i) is not str:
newTup.append(i)
newList.append(tuple(newTup))
print(newList)
fun = list(map(lambda j: tuple(filter(lambda i: type(i) is not str,j)) , MyList))
print(fun)