题解 | #坐标移动# 空值处理

坐标移动

https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29

'''从(0,0)点开始移动:
A表示向左移动
D表示向右移动
W表示向上移动
S表示向下移动
'''

while 1:
    try:
        s=input().split(';')
        #须先删掉空字符串 s.remove('') 只能删掉第一个'',后面的''导致函数停止
        s = list(filter(None, s)) 
        #filter()可以过滤掉空值,将结果重新组成列表即可
        #print(s)
        x,y=0,0
        for i in s:
            if i[0] in 'ADWS' and i[1:].isdigit():
                dir,step=i[0],int(i[1:])
                #print(dir,step)
                if i[0]=='A':
                    x -= step
                elif i[0]=='D':
                    x += step
                elif i[0]=='W':
                    y += step
                elif i[0]=='S':
                    y -= step
                #print(x,',',y)
        print(str(x)+','+str(y))
    except:  # 遇到空白/空格/空行自动停止
        break
'''
d={1:'fsdf',2:'sdfds'}
print(len(d),d.items())
a='4234njn'
b='41434'
print(b.isdigit())
l=['','gf','dgdf','hher','','grg','']
try:
    while True:
        l.remove("")
except ValueError:
    pass
for i in l:
    print(i)
'''





删掉空字符串:

s.remove('') 只能删掉第一个'',后面的''导致函数停止

s = list(filter(None, s)) #filter()可以过滤掉空值,将结果重新组成列表即可

全部评论

相关推荐

不愿透露姓名的神秘牛友
06-29 17:30
找实习找着找着就要进入7月了,马上秋招也要开始了,找实习还有意义吗?
绝迹的星:有面就面, 没面上就当日薪4位数大佬免费培训, 面上了再考虑要不要实习
点赞 评论 收藏
分享
05-29 09:02
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务