Python:每日一题 195
本帖最后由 冬雪雪冬 于 2018-8-9 19:47 编辑我们的玩法做了一下改变:
1. 楼主不再提供答案。
2. 请大家先独立思考,再参考其他鱼油的解答,这样才有助于自己编程水平的提高。开始阶段是看不到其他人的回帖的,等答题完成,开始评分时再取消限制。
3. 鼓励大家积极答题,奖励的期限为出题后24小时内。
4. 根据答案的质量给予1~3鱼币的奖励。
题目:
这是一个奇怪的字典{"A": "B", "B": "C", "C": "D"},一个item的value是另一个item的key
编写一个函数,参数为一个字典和一个key,如果得到的value是另一个item的key则继续求value,直到value不是key为止,返回这个value。
例如:
dict1 = {"A": "B", "B": "C", "C": "D"}
fun(dict1, "A") 返回 "D"
fun(dict1, "C") 返回 "D"
注意,如果value都是其他的item的key,即形成循环则返回None
例如
dict1 = {"A": "B", "B": "C", "C": "D", "D": "A", "E": "F"}
fun(dict1, "A") 返回 None
--------------------------------------------------------------------
对大家答案的测试如下:
d1 = {"A": "B", "B": "C", "C": "D", "E": "F"}
d2 = {"A":"B", "B":"C","C":"D","D":"B"}
def f.....
print(f(d1, "A")) #链状
print(f(d1, "E")) #单独
print(f(d2, "A")) #循环 def fun195(d,k):
t=dict()
while True:
t=d
d.pop(k)
if t in d:
k=t
elif t in t:
return None
else:
return t def loop(dict1, key):
new_key = key
while True:
if dict1 in dict1.keys(): #判断该key对应的值是否在key中
new_key = dict1
if new_key == key:
return None
else:
return dict1
结果
dict1 = {"A": "B", "B": "C", "C": "D"}
print(loop(dict1, "A"))
D
dict1 = {"A": "B", "B": "C", "C": "D", "D": "A", "E": "F"}
print(loop(dict1, "A"))
None def func(dict0, key1):
if key1 not in dict0:
return "字典中不包含该key!!!"
key2 = dict0
while True:
if key2 == key1:
return None
if key2 not in dict0:
return key2
key2 = dict0
dict0 = {"A": "B", "B": "C", "C": "D"}
print(func(dict0, "A"))
dict1 = {"A": "B", "B": "C", "C": "D", "D": "A", "E": "F"}
print(func(dict1, "A")) def fun195(dict1,key):
list1 = []
value = dict1
while value in dict1:
list1.append(value)
value = dict1
if value in list1:
return None
return value
>>> dict1 = {"A": "B", "B": "C", "C": "D"}
>>> fun195(dict1,'A')
'D'
>>> fun195(dict1,'C')
'D'
>>> dict2 = {"A": "B", "B": "C", "C": "D", "D": "A", "E": "F"}
>>> fun195(dict2,'A')
>>> def func195(dct: dict, key: str):
if dct in dct:
value = dct
del dct
return func195(dct, value)
else:
return None if dct=={} else dct
if __name__ == '__main__':
dict1 = {"A": "B", "B": "C", "C": "D"}
res = func195(dict1,"A")
print(res) 本帖最后由 chongchuigu 于 2018-8-7 14:11 编辑
def a195(x,y):
for i in range(len(x)):
y=x
if y not in dict1:
return y
elif x==y:
return None
dict1 = {"A": "B", "B": "C", "C": "D", "D": "A", "E": "F"}
print(a195(dict1, "A"))
dict1 = {"A": "B", "B": "C", "C": "D"}
print(a195(dict1, "A"))
def fun(dicts,key):
count=1
while dicts in dicts.keys():
key=dicts
count+=1
if count>len(dicts):
print(None)
break
if count<=len(dicts):
print(dicts)
dict1 = {"A": "B", "B": "C", "C": "D", "D": "A", "E": "F"}
fun(dict1, "A")
>>> def fun( dicts, key, rund=None):
if rund == None:
rund = []
if key in rund:
return None
rund.append(key)
v = dicts.get(key)
if v in dicts:
return fun(dicts, v, rund)
return v
>>> d = {1:2,2:3,3:4}
>>> fun(d, 1)
4
>>> d = {1:2,2:3,3:4, 4:1}
>>> fun(d,1)
>>> d = {"A": "B", "B": "C", "C": "D"}
>>> fun(d,'A')
'D'
>>> fun(d,'C')
'D'
>>> d = {"A": "B", "B": "C", "C": "D", "D": "A", "E": "F"}
>>> fun(d, 'A')
>>> def fun(dic1, str_a):
str_b = str_a
str_a = dic1
while(1):
if str_a in dic1:
if str_a != str_b:
str_a = dic1
else:
str_b = None
return str_b
break
else:
str_b = str_a
return str_b
break
dic1 = {"A": "B", "B": "C", "C": "D", "D": "A", "E": "F"}
print(fun(dic1,"A"))
print(fun(dic1,"E")) def fun195(dicts,key):
try:
fun195(dicts,dicts)
except KeyError:
print(dicts)
except RuntimeError:
return None
fun195({'A':'B','B':'C'},'A') #coding:utf-8
def result(dic,str):
try:
if dic in dic.keys():
return result(dic, dic)
else:
return dic
except:
return None
dict1 = {"A": "B", "B": "C", "C": "D"}
dict2 = {"A": "B", "B": "C", "C": "D", "D": "A"}
print(result(dict1,'A'))
print(result(dict1,'C'))
print(result(dict1,'E'))
print(result(dict2,'A')) 本帖最后由 popython 于 2018-8-7 11:16 编辑
def fun(di,di_key):
A = di.keys()
ai = len(di)
di_key = di
while ai:
if di_key in A:
di_key = di
else:
return di_key
ai -= 1
if ai == 0:
return None
只想到这样的。。。 本帖最后由 故梦L 于 2018-8-7 11:58 编辑
def function(dict, _key, keys=list(), values=list()):
"""
:param count:搜索_key的位置
:param flag:从_key开始索引
:return:value or None
"""
count = 0
for key,value in dict.items():
values.append(value)
keys.append(key)
if key == _key:
flag = count
else:
count += 1
for i in range(flag, len(dict)-1):
if keys != values:
return values
else:
return None
dict1 = {"A": "H", "B": "C", "C": "D", "D": "E", "E": "F"}
print(function(dict1,'A'))
gtmp = []
def func195(dic, str):
global gtmp
try:
if dic:
if dic in gtmp:
gtmp = []
return
else:
gtmp.append(dic)
return func195(dic, dic)
except:
gtmp = []
return dic
if __name__=='__main__':
dict1 = {"A": "B", "B": "C", "C": "D"}
print(func195(dict1, "A"))
print(func195(dict1, "C"))
dict1 = {"A": "B", "B": "C", "C": "D", "D": "A", "E": "F"}
print(func195(dict1, "A")) def fun195(dic,x):
usedkeys=
whileTrue:
x=dic.get(x)
if x in usedkeys:
return None
if x in dic:
usedkeys.append(x)
else:
return x
dict1 = {"A": "B", "B": "C", "C": "D"}
print(fun195(dict1,"A"))
print(fun195(dict1,"C"))
dict1 = {"A": "B", "B": "C", "C": "D", "D": "A", "E": "F"}
print(fun195(dict1, "A")) def fun195(dic,key):
if dic in dic:
dic,key=None,dic
return fun195(dic,key)
else:
return dic 本帖最后由 凌九霄 于 2018-8-7 21:26 编辑
dict1 = {"A": "B", "B": "C", "C": "D"}
dict2 = {"A": "B", "B": "C", "C": "D", "D": "A", "E": "F"}
dict3 = {"A": "B", "B": "C", "C": "D", "D": "Q", "E": "F"}
def func(ldict, key):
value = None
cdict = ldict.copy()
for i in range(len(ldict)):
if ldict.__contains__(key):
value = ldict
try:
del cdict[ key ]
except KeyError:
return None
else:
key = value
else:
break
return value
print(func(dict1, "A"))#D
print(func(dict1, "M"))#None
print(func(dict2, "B"))#None
print(func(dict3, "C"))#Q def fun193(dict1,str1):
value = dict1
while value in dict1.keys():
value = dict1
if value == str1:
return None
return value def fun195(dict1, key):
key1 = dict1.get(key)
if key1:
while dict1.get(key1) and key1 != key:
key1 = dict1.get(key1)
if not dict1.get(key1):
return key1
else:
return
else:
return
dict1 = {"A": "B", "B": "C", "C": "D"}
dict2 = {"A": "B", "B": "C", "C": "D", "D": "A", "E": "F"}
print(fun195(dict1, 'A'))
print(fun195(dict1, 'C'))
print(fun195(dict1, 'D'))
print(fun195(dict2, 'A'))
页:
[1]
2