Python 在字符串列表中循环
我有下面的练习,其中列出了指南针上每个方向的方向=[N,E,S,W]。我必须做一个函数,如果你输入N,它将以顺时针方向返回下一个。当你输入W时,它将返回到开始位置并返回N。当输入不在列表中时,例如F,它将返回无。 这就是我想到的: def顺时针转动方向: 方向=[N,E,S,W] 对于范围3中的i: 如果方向==方向[i]: 结果=方向[i+1] 打印结果 其他: Returnone 这只在我输入N时有效。当我删除else时,它也适用于其他项,但是当输入为W时,它不会循环回到开始。我想知道如何使代码适合分配,或者是否有更简单的方法来完成Python 在字符串列表中循环,python,python-3.x,cycle,Python,Python 3.x,Cycle,我有下面的练习,其中列出了指南针上每个方向的方向=[N,E,S,W]。我必须做一个函数,如果你输入N,它将以顺时针方向返回下一个。当你输入W时,它将返回到开始位置并返回N。当输入不在列表中时,例如F,它将返回无。 这就是我想到的: def顺时针转动方向: 方向=[N,E,S,W] 对于范围3中的i: 如果方向==方向[i]: 结果=方向[i+1] 打印结果 其他: Returnone 这只在我输入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
要将内容围绕一个特定的数字进行包装,需要使用模%运算符
这里有@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
也可以从以下位置使用预制循环: 输出:
N -> E -> S -> W -> N -> E -> S -> W -> N -> E -> S
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
或者简单地创建一个查找目录
可用方法中的两个版本:
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 -> S -> W -> N -> E -> S -> W -> N -> E -> S
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
也可以从以下位置使用预制循环: 输出:
N -> E -> S -> W -> N -> E -> S -> W -> N -> E -> S
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
或者简单地创建一个查找目录
可用方法中的两个版本:
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 -> S -> W -> N -> E -> S -> W -> N -> E -> S
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
这可以进一步重构为一个通用函数,在给定当前项的列表中循环:def cycleevalues,current:return values[values.indexcurrent+1%lenvalues]如果当前不在列表中,它将引发ValueError。如果当前不在列表中,它可以进一步重构为一个通用函数,在给定当前项的列表中循环:def cyclevalues,current:return values[values.indexcurrent+1%lenvalues],这将引发ValueError。