list1 = []
key1 = key
for i in dict:
list1.append(i)
if key not in list1:
return "输入错误,没有该key值"
else:
while True:
if key not in list1:
return key
elif dict == key1:
return None
else:
key = dict dict1 = {'A':'B','B':'C','C':'D','D':'A'}
def fun(dict,key):
abkey = key
while dict1 in dict1:
key = dict1
if key == abkey:
dict = None
break
return dict1
def test195(dic,key):
temp = []
temp.append(key)
for i in range(len(dic)):
if dic in temp:
return None
else:
key = dic
temp.append(key)
else:
return key
dict1 = {"A": "B", "B": "C", "C": "D", "D": "A", "E": "F"}
dict2 = {"A": "B", "B": "C", "C": "D"}
print(test195(dict1,'A'))
print(test195(dict2,'A'))
def find_key(input_dict, x):
return find_key(input_dict, input_dict)
def fun195(input_dict, x):
try:
y = find_key(input_dict, x)
except KeyError as e:
print(e)
except RecursionError:
print(None)
dict1 = {"A": "B", "B": "C", "C": "D"}
##dict1 = {"A": "B", "B": "C", "C": "D", "D": "A", "E": "F"}
print('-----try A')
fun195(dict1,'A')
print('-----try D')
fun195(dict1,'D')
print('-----try E')
fun195(dict1,'E')
print('-----try F')
fun195(dict1,'F') dict1 = {"A": "B", "B": "C", "C": "D", "D": "A", "E": "F"}
def func(Dict, Item):
start = Item
times = 0
while True:
if times > len(Dict):
return None
next = Dict
if next in Dict.keys():
Item = next
times += 1
else:
return next
if __name__ == '__main__':
print(func(dict1,'A'))
def fun195(dict1, key):
i = 0
while dict1 in list(dict1.keys()):
i += 1
key = dict1
if i >= len(list(dict1.keys())):
return None
return dict1
dict1 = {"A": "B", "B": "C", "C": "D", "D": "A", "E": "F"}
key = "A"
fun195(dict1, key)
def fun(dict1,a):
i=1
c=a
try:
while i>0:
b=dict1
if (len(b)>0):
i=1
a=b
if (str(a)==c):
a=None
break
else:
i=0
break
except:
pass
return a
if __name__=='__main__':
dict1 = {"A": "B", "B": "C", "C": "D", "D": "H", "E": "F"}
y = fun(dict1,"A")
print (y)
dict1 = {"A": "B", "B": "C", "C": "D", "D": "Q", "E": "F"}
这条貌似很多也过不了啊,输入key='F' ,正确应该返回None,很多返回F 凌九霄 发表于 2018-8-9 21:03
dict1 = {"A": "B", "B": "C", "C": "D", "D": "Q", "E": "F"}
这条貌似很多也过不了啊,输入key='F' , ...
主要是题目中没有要求检测不存在的key,所以就不强求大家了 冬雪雪冬 发表于 2018-8-9 21:05
主要是题目中没有要求检测不存在的key,所以就不强求大家了
当时候有点迟疑,但觉得竟然题目没有特别要求,才用抛出异常的手法{:5_96:} claws0n 发表于 2018-8-9 23:55
当时候有点迟疑,但觉得竟然题目没有特别要求,才用抛出异常的手法
你考虑的周全 感谢版主仔细评分,没有用到递归算法,还是要多学习 d1 = {"A": "B", "B": "C", "C": "D", "E": "F"}
d2 = {"A":"B", "B":"C","C":"D","D":"B"}
def f(d,s):
tmp1 = d
tmp2 = None
if tmp1 not in d.keys():
tmp2 = tmp1
else:
for k, v in d.items():
if k != s and v not in d.keys():
tmp2 = v
break
return tmp2
print(f(d1, "A")) #链状
print(f(d1, "E")) #单独
print(f(d2, "A")) #循环 本帖最后由 graceasyi 于 2018-8-14 10:35 编辑
第一种方法只能处理顺序的情况,就是题目中的dict。第二种方法可处理非顺序情况。
def fun195_1(dict1, key):
tmp = []
for k, v in dict1.items():
tmp.append(k)
if key == k:
key = v
return None if key in tmp else key
def fun195_2(dict1, key):
v = dict1
tmp = []
while True:
tmp.append(v)
try:
v = dict1
if v in tmp:
return None
except KeyError:
break
return v
d1 = {"A": "B", "B": "C", "C": "D", "E": "F"}
d2 = {"A": "B", "B": "C", "C": "D", "D": "B"}
d3 = {"A": "B", "C": "D", "B": "C", "D": "F", "F": "E", "E": "G"}
d4 = {"A": "B", "C": "D", "B": "C", "D": "F", "F": "E", "E": "D"}
print(fun195_2(d1, 'A'))
print(fun195_2(d1, 'E'))
print(fun195_2(d2, 'A'))
print(fun195_2(d3, 'B'))
print(fun195_2(d4, 'A'))
结果:
D
F
None
G
None def fun_195(dict1,key1,temp=[]):
while True:
if key1 in dict1.keys():
temp.append(key1)
key1=dict1
else:
break
if key1 in temp:
key1=None
break
print(key1)
d1 = {"A": "B", "B": "C", "C": "D", "E": "F"}
d2 = {"A":"B", "B":"C","C":"D","D":"B"}
fun_195(d1,"E")
fun_195(d1,"A")
fun_195(d2,"A") def f(dic1,a):
dic = dic1.copy()
while True:
b = dic.get(a)
dic.pop(a,None)
if dic.get(b,None):pass
else:
if dic1.get(b,None):return None
return b
a = b
d1 = {"A": "B", "B": "C", "C": "D", "E": "F"}
d2 = {"A":"B", "B":"C","C":"D","D":"B"}
print(f(d1,'A'))
print(f(d1,'E'))
print(f(d2,'A'))
def fun195(dict_x,key):
repeat_key = []
while True:
if key in repeat_key:
return None
try:
dict_x]
except KeyError:
return dict_x
repeat_key.append(key)
key = dict_x
if __name__ == '__main__':
d1 = {'A': 'B', 'B': 'C', 'C': 'D', 'E': 'F'}
d2 = {'A':'B', 'B':'C','C':'D','D':'B'}
print(fun195(d1,'A'))
print(fun195(d1,'E'))
print(fun195(d2,'A')) def func(dic,key):
originalkey=key
while 1:
try:key=dic
except:return key
if key==originalkey:return def fun(dic, val):
found = []
while dic.get(val):
found.append(val)
val = dic.pop(val)
if val in found:
return None
else:
return val
页:
1
[2]