如果它包含所需的值,我正在嘗試獲取串列的名稱(型別串列):
def f(Value):
a = ['a','b','c']
b = ['d','e','f']
d = ['g','h','i']
c = ['j','k','l']
w = next(n for n,v in filter(lambda t: isinstance(t[1],list), locals().items()) if value in v)
return(w)
f()
好吧,這段代碼將回傳串列的名稱,但輸入字串,所以我以后將無法使用它。先感謝您
uj5u.com熱心網友回復:
您的代碼中有幾個錯誤 - 如果您想要串列,則回傳它而不是它的名稱:
def f(value): # capitalization not correct
a = ['a','b','c']
b = ['d','e','f']
d = ['g','h','i']
c = ['j','k','l']
# set w to v not n - n is the local name, v is the value
w = next(v for n,v in filter(lambda t: isinstance(t[1],list),
locals().items()) if value in v)
return w
# needs to be called with parameter
what_list = f("h")
print( what_list )
輸出:
['g', 'h', 'i']
我不明白為什么locals().items()在這個最小的例子中使用,你可以這樣做
for li in [a,b,c,d]: # simply add more lists if needed, no need to inspect locals
if value in li:
return li
反而。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/466085.html
上一篇:將原子向量轉換為串列(purrr::simplify()的倒數)
下一篇:如何列印串列值周圍的內容?
