[英]Sorting through list of lists?
所以我有一份清單清單:
nums = [[98,90,91],[46,76,62],[85,90,83],[77,79,81]]
我想整理他們。 當一個數字在列表(迷你列表)中
list(主列表)低於我在參數中給出的默認值,即函數
將為列表中的每個列表返回一個包含true或false的列表。
示例:[True,False,True,True]
到目前為止,這是我的代碼:
def all_passing(nums, grade = 70):
new = []
for l in nums:
boo = True
for x in l:
if x <= grade:
boo == False
new.append(boo)
return new
每次運行代碼時,我的輸出都是一個空列表。
編輯:我修復了一些事情,現在我越來越接近我的解決方案,但它在列表列表中的每個值都返回True或False。 如何使它只對單個列表返回True或False。 示例:[98,90,91]將設置為True。 [46,76,62]將被設置為False,因為列表中的x小於70.其他列表也是如此。
編輯:我開始工作,我將附加行縮進到第二個for循環的相同縮進,現在我理解它是如何工作的。 謝謝!!!
我能做的最短:
def all_passing(nums, grade = 70):
return [ [ True if elel > grade else False for elel in el ] for el in nums ]
但是如果你想讓它只返回一個列表(我不確定我是否得到了你的要求),並且對迷你列表的結果使用了真假的dedending,那么我會做
def all_passing(nums, grade = 70):
ans = [ [ True if elel > grade else False for elel in el ] for el in nums ]
return [ False if False in el else True for el in ans ]
第二個例子給了我[True, False, True, True]
,類似於你的預期。
代碼中的問題是:
boo == False
正在測試boo是否等於false。 將此更改為boo = False
將boo
設置為False
new.append(boo)
僅在x <= grade
調用。 將其移至if
語句外部和for x in l
你的最終代碼應該是:
def all_passing(nums, grade = 70):
new = []
for l in nums:
boo = True
for x in l:
if x <= grade:
boo = False
new.append(boo)
return new
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.