給定存盤在變數 numbers 中的整數串列,宣告變數 result1 并初始化僅包含偶數正數的初始串列的值,尊重初始順序。
numbers = [-85,34,-7,-6,57,-6,-65,-49,-51,79,90,-75,33,36,9,30,-61,-66,95 .15,-56.45,-21,-81,-83,-31.82,-92,21.95,-70.88,-25,100,-17,-52,-74,-24.85 ,-40,-35,82,-74,-89,73,-44,53,88,35,-77,-90, 54,-41,-23,58,-95,47,-65, -92,91,64,-69,-21,-58, -63,-51,95,45,95,50,4,-15,-98,27,-84,32,57,-50, -54,-14,-46,98,76,0,-85,-1,15,67,-5,92,-39,-68, -73,-85,0,-39,-72, -9,52,27,52,-41,69,52,-42, -78,-37.59,-77,-30,77,56,-13,-80,-14,-30.94 ,34, 81,-46,-77,50,-23,-20,-59,-4,59,94,34,50,46,87, -78,88,-47,66,92,- 42,0,-28,-31,-18,-32,34,66,-12, 81,17,-11,37,20,-82,9,41,28,-81,26,47, 70.98, -82,-15.46,-13.89,-33,19,94,-88,52,14,27,-4, 6,-60.44,-98.83,-13 ,-80,-58,-10,-33,-70.87, -50.51,-3,-36,-60.61,-40,41.91,-25.38,-60.67 ,-85.36,-83.43]
我試圖做的事情:
def filterList(p, xs):
return [x for x in xs if p(x)]
def ePar(numbers):
return numbers%2==0
def ePositive(numbers):
return numbers>0
def result1():
return filterList(eEven, ePositive)
print (result1[:7]) ## because i want the last seven even positive integers
我希望使用我的函式 result1,它只會過濾偶數和正整數,但錯誤如下:
***Run error***
Traceback (most recent call last):
File "__tester__.python3", line 35, in <module>
print(resultado1[:7])
TypeError: 'function' object is not subscriptable
uj5u.com熱心網友回復:
您的代碼的問題在于您的filterList函式將一個函式和一個整數串列作為引數(為清楚起見,在下面添加了型別注釋):
from typing import Callable
def filterList(p: Callable[[int], bool], xs: list[int]) -> list[int]:
return [x for x in xs if p(x)]
但是你用兩個函式呼叫它:
return filterList(eEven, ePositive) # two Callable[[int], bool]s
您可以撰寫您filterList的函式來代替任意數量的函式,如下所示:
def filterList(xs: list[int], *p: Callable[[int], bool]) -> list[int]:
return [x for x in xs if all(f(x) for f in p)]
然后這樣稱呼它:
def filterPositiveAndEven(xs: list[int]) -> list[int]:
return filterList(xs, ePar, ePositive)
numbers = [-85,34,-7,-6,57,-6,-65,-49,-51,79,90,-75,33,36,9,30,-61,-66,95.15,-56.45,-21,-81,-83,-31.82,-92,21.95,-70.88,-25,100,-17,-52,-74,-24.85 ,-40,-35,82,-74,-89,73,-44,53,88,35,-77,-90, 54,-41,-23,58,-95,47,-65, -92,91,64,-69,-21,-58, -63,-51,95,45,95,50,4,-15,-98,27,-84,32,57,-50, -54,-14,-46,98,76,0,-85,-1,15,67,-5,92,-39,-68, -73,-85,0,-39,-72, -9,52,27,52,-41,69,52,-42, -78,-37.59,-77,-30,77,56,-13,-80,-14,-30.94 ,34, 81,-46,-77,50,-23,-20,-59,-4,59,94,34,50,46,87, -78,88,-47,66,92,- 42,0,-28,-31,-18,-32,34,66,-12, 81,17,-11,37,20,-82,9,41,28,-81,26,47, 70.98, -82,-15.46,-13.89,-33,19,94,-88,52,14,27,-4, 6,-60.44,-98.83,-13 ,-80,-58,-10,-33,-70.87, -50.51,-3,-36,-60.61,-40,41.91,-25.38,-60.67 ,-85.36,-83.43]
print(filterPositiveAndEven(numbers))
# prints [34, 90, 36, 30, 100, 82, 88, 54, 58, 64, 50, 4, 32, 98, 76, 92, 52, 52, 52, 56, 34, 50, 94, 34, 50, 46, 88, 66, 92, 34, 66, 20, 28, 26, 94, 52, 14, 6]
uj5u.com熱心網友回復:
您可以方便地為此使用生成器,如下所示:
numbers = [-85,34,-7,-6,57,-6,-65,-49,-51,79,90,-75,33,36,9,30,-61,-66,95.15,-56.45,-21,-81,-83,-31.82,-92,21.95,-70.88,-25,100,-17,-52,-74,-24.85 ,-40,-35,82,-74,-89,73,-44,53,88,35,-77,-90, 54,-41,-23,58,-95,47,-65, -92,91,64,-69,-21,-58, -63,-51,95,45,95,50,4,-15,-98,27,-84,32,57,-50, -54,-14,-46,98,76,0,-85,-1,15,67,-5,92,-39,-68, -73,-85,0,-39,-72, -9,52,27,52,-41,69,52,-42, -78,-37.59,-77,-30,77,56,-13,-80,-14,-30.94 ,34, 81,-46,-77,50,-23,-20,-59,-4,59,94,34,50,46,87, -78,88,-47,66,92,- 42,0,-28,-31,-18,-32,34,66,-12, 81,17,-11,37,20,-82,9,41,28,-81,26,47, 70.98, -82,-15.46,-13.89,-33,19,94,-88,52,14,27,-4, 6,-60.44,-98.83,-13 ,-80,-58,-10,-33,-70.87, -50.51,-3,-36,-60.61,-40,41.91,-25.38,-60.67 ,-85.36,-83.43]
def even_pos(numlist):
for v in numlist:
if v > 0 and v % 2 == 0:
yield v
print([*even_pos(numbers)][-7:]) # last 7 values
輸出:
[20, 28, 26, 94, 52, 14, 6]
uj5u.com熱心網友回復:
我不知道你的resultado1orresult1變數是什么。我認為它是res程式員使用的專利變數。
def filter_even_and_positive(num):
return (num % 2 == 0 and num > 0)
res = []
for n in numbers:
if filter_even_and_positive(n): res.append(n)
print(res[:7]) # First 7 values
使用串列理解,您可以將代碼縮短為:
res = [n for n in numbers if (n % 2 == 0 and n > 0)]
print(res[:7]) # First 7 Value
輸出:
[34, 90, 36, 30, 100, 82, 88]
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/523701.html
標籤:Python列表功能筛选
上一篇:這是回呼函式嗎?
下一篇:將R中矩陣的列制表
