list_m = None
#
# 1.[2,0,2,0]--->[2,2,0,0]
# 1.[2,0,0,2]--->[2,2,0,0]
def zero_to_end():
print(list_m)
for r in range(-1,-len(list_m)-1,-1):
if list_m[r]==0:
del list_m[r]
list_m.append(0)
zero_to_end()
print(list_m)
def merge():
'''
先将中间的元素移到末尾
再将相邻元素合并
:return:
'''
zero_to_end()
for i in range(0,len(list_m)-1):
if list_m[i]==list_m[i+1]:
list_m[i]+=list_m[i+1]
del list_m[i+1]
list_m.append(0)
merge()
print(list_m)
list_m = None
map=[
[2,0,0,2],
[4,4,2,2],
[2,4,0,4],
[0,0,2,2],
]
def move_left():
for line in map:
global list_m
list_m = line
# merge() #merge方法开始操作
最后的这函数for line in map: global list_m list_m = line 这句话赋值给全局变量list_m 但就是赋值不到啊 list_m一直就是None 这是为什么啊??
来源:oschina
链接:https://my.oschina.net/u/4406496/blog/4260720