循环遍历字符串列表
Cycling through a list of strings
我有以下练习,其中我有一个列表 directions = ["N", "E", "S", "W"]
用于罗盘上的每个方向。我必须创建一个函数,如果你输入 "N" 它 return 是顺时针方向的下一个 "E"。当你输入 "W" 它应该回到开始和 return "N"。当输入不在列表中时,例如"F" 应该 return none。
这是我想出的:
def turn_clockwise(direction):
directions = ["N", "E", "S", "W"]
for i in range(3):
if direction == directions[i]:
result = directions[i+1]
print(result)
else:
return(None)
这仅在我输入 "N" 时有效。当我删除 else 时,它也适用于其他项目,但是当输入为 "W" 时,它不会循环回到开始。我想知道如何使代码适合作业,或者是否有更简单的方法来做到这一点。
def turn_clockwise(direction):
directions = ["N", "E", "S", "W"]
if direction in directions:
return directions[(directions.index(direction) + 1) % len(directions)]
else:
return None
使用模 % 运算符环绕特定数字。
这里附上@AKX的建议:
def cycle(values, current):
try:
return values[(values.index(current) + 1) % len(values)]
except ValueError:
print(f"{current} not in {values}")
def turn_clockwise(direction):
directions = ["N", "E", "S", "W"]
if direction in directions:
return cycle(directions, direction]
else:
return None
您还可以使用 itertools.cycle 中的预制 cycle
:
from itertools import cycle
directions = cycle(["N", "E", "S", "W"])
for _ in range(10):
print(next(directions), end = " -> ")
print(next(directions))
输出:
N -> E -> S -> W -> N -> E -> S -> W -> N -> E -> S
或者简单地创建一个查找字典。
两个版本的可用方法:
from itertools import cycle
def next_dir(what):
d = "NESW"
directions = cycle(d)
if what in d:
while next(directions) != what:
pass
return next(directions)
else:
raise ValueError(what + " not possible")
def next_lookup(what):
d = {"N":"E", "E":"S", "S":"W", "W":"N"}
r = d.get(what)
if r:
return r
raise ValueError(what+" not possible")
for l in "NESW":
print(l, next_dir(l))
print(l, next_lookup(l))
try:
print(next_dir("q"))
except Exception as e:
print(e)
try:
print(next_lookup("q"))
except Exception as e:
print(e)
输出:
N E # next_dir
N E # next_lookup .. etc ..
E S
E S
S W
S W
W N
W N
q not possible
q not possible
我有以下练习,其中我有一个列表 directions = ["N", "E", "S", "W"]
用于罗盘上的每个方向。我必须创建一个函数,如果你输入 "N" 它 return 是顺时针方向的下一个 "E"。当你输入 "W" 它应该回到开始和 return "N"。当输入不在列表中时,例如"F" 应该 return none。
这是我想出的:
def turn_clockwise(direction):
directions = ["N", "E", "S", "W"]
for i in range(3):
if direction == directions[i]:
result = directions[i+1]
print(result)
else:
return(None)
这仅在我输入 "N" 时有效。当我删除 else 时,它也适用于其他项目,但是当输入为 "W" 时,它不会循环回到开始。我想知道如何使代码适合作业,或者是否有更简单的方法来做到这一点。
def turn_clockwise(direction):
directions = ["N", "E", "S", "W"]
if direction in directions:
return directions[(directions.index(direction) + 1) % len(directions)]
else:
return None
使用模 % 运算符环绕特定数字。
这里附上@AKX的建议:
def cycle(values, current):
try:
return values[(values.index(current) + 1) % len(values)]
except ValueError:
print(f"{current} not in {values}")
def turn_clockwise(direction):
directions = ["N", "E", "S", "W"]
if direction in directions:
return cycle(directions, direction]
else:
return None
您还可以使用 itertools.cycle 中的预制 cycle
:
from itertools import cycle
directions = cycle(["N", "E", "S", "W"])
for _ in range(10):
print(next(directions), end = " -> ")
print(next(directions))
输出:
N -> E -> S -> W -> N -> E -> S -> W -> N -> E -> S
或者简单地创建一个查找字典。
两个版本的可用方法:
from itertools import cycle
def next_dir(what):
d = "NESW"
directions = cycle(d)
if what in d:
while next(directions) != what:
pass
return next(directions)
else:
raise ValueError(what + " not possible")
def next_lookup(what):
d = {"N":"E", "E":"S", "S":"W", "W":"N"}
r = d.get(what)
if r:
return r
raise ValueError(what+" not possible")
for l in "NESW":
print(l, next_dir(l))
print(l, next_lookup(l))
try:
print(next_dir("q"))
except Exception as e:
print(e)
try:
print(next_lookup("q"))
except Exception as e:
print(e)
输出:
N E # next_dir
N E # next_lookup .. etc ..
E S
E S
S W
S W
W N
W N
q not possible
q not possible