使用过滤器功能按特定索引对列表进行分类+按特定索引或列表名称打印
Using Filter Function to categorize list by specific index + Printing by specific index or list name
我正在 Python 中创建和操作一个列表,我在通过过滤功能对我的列表进行分类时遇到问题...
我有 3 个列表,我将它们附加到一个列表中,我经常打印 (the_list),这是我的代码:
list1 = ['Georgia', 10.5, 'Peach'];
list2 = ['Florida', 21.3, 'Sunshine'];
list3= ['Alabama', 4.9, 'Dixie'];
#List that includes list1, list2 and list3 called "the_list"
the_list = []
the_list.append(list1)
the_list.append(list2)
the_list.append(list3)
the_list
#insert new values into the list (these values represent state numbers)
list1.insert(3, 4)
list2.insert(3, 27)
list3.insert(3, 22)
#print the modified list
print (the_list)
#Organize the list from lowest to highest (based off numbers in index 1)
the_list.sort(key=lambda tup: tup[1])
print (the_list)
#filter states by category based off their population
#This is where I need help
#Small States
def lessThanTen(index):
return index < 10
the_list
filter(lessThanTen, the_list)
print (the_list)
#Big States
def greaterThanTen(index):
return index > 10
the_list
filter(greaterThanTen, the_list)
print (the_list)
有没有办法通过特定索引号将这些列表过滤到类别中,在本例中为索引 [1](即人口),然后通过打印输出这些列表项,或者它们的列表名称或它们的值在索引 [0]...示例 'Georgia' 或 "list1"
filter(function, iterable) Construct an iterator from those elements
of iterable for which function returns true. iterable may be either a
sequence, a container which supports iteration, or an iterator. If
function is None, the identity function is assumed, that is, all
elements of iterable that are false are removed.
Note that filter(function, iterable) is equivalent to the generator
expression (item for item in iterable if function(item)) if function
is not None and (item for item in iterable if item) if function is
None.
不清楚你的意思,但我会尽力帮助你。
首先:你的greaterThanTen
函数将index
作为输入,至少看起来是这样。 filter
不会将索引作为参数传递给 greaterThanTen
,而是将索引处的元素作为参数传递。
还有一点:不知道大家是否理解filter
只有returns一个'category'作为输出——你可以一次只对一个条件进行排序。此外,过滤器不对原始列表进行操作,而是创建一个新序列,因此 filter(greaterThanTen, the_list)
实际上并没有改变任何东西。你应该做的是:the_list = filter(greaterThanTen, the_list)
.
如果要对列表中的每个元素按索引 1 处的值排序,可以这样做:
filter(lambda element: yourSortFunction(elmenet[1]), list)
这类似于您用作排序键的函数。
另一件事: 你为什么要在 greaterThanTen
中调用 the_list
,它不会感觉。该函数在 return
语句后停止计算代码。
正在打印:
如果您想打印列表中特定索引的值,只需询问该索引即可。
print(LIST[index])
希望对您有所帮助。
您可能只做某种列表理解并完全避免过滤。
the_final_list = [x for x in the_list if x[1] < 10]
这对我来说是 simpler/more 可读的并且完成了你的 objective。
如果您想将索引作为参数传递并保持列表的一些灵活性(您的索引可能不是 1),您可以这样做
def greaterThanTen(index):
return lambda x: x[index] > 10
def lessThanTen(index):
return lambda x: x[index] < 10
def myfilter(f, L):
return [x[0] for x in filter(f, L)]
print(myfilter(greaterThanTen(1), the_list)) # -> ['Georgia', 'Florida']
print(myfilter(lessThanTen(1), the_list)) # -> ['Alabama']
或者更笼统地说,
import operator
def index_vs_num(index):
ops = {
'>' : operator.gt,
'<' : operator.lt,
'>=': operator.ge,
'<=': operator.le,
'=' : operator.eq
}
return lambda relation: lambda num: lambda x: ops[relation](x[index], num)
greaterThanTwenty = index_vs_num(1)('>')(20)
# the 1st argument is index of your population in the list
# the 2nd argument is the type of comparation
# the 3rd argument is the number to be compared
lessThanFive = index_vs_num(1)('<')(5)
def filter_by_outindex(*index):
def filter_by_f(f, L):
try:
return [x[index[0]] for x in filter(f, L)]
except IndexError:
return list(filter(f, L))
return filter_by_f
myfilter=filter_by_outindex(0)
print(myfilter(greaterThanTwenty, the_list)) # -> ['Florida']
print(myfilter(lessThanFive, the_list)) # -> ['Alabama']
我认为这就是您真正想要实现的目标。
我正在 Python 中创建和操作一个列表,我在通过过滤功能对我的列表进行分类时遇到问题...
我有 3 个列表,我将它们附加到一个列表中,我经常打印 (the_list),这是我的代码:
list1 = ['Georgia', 10.5, 'Peach'];
list2 = ['Florida', 21.3, 'Sunshine'];
list3= ['Alabama', 4.9, 'Dixie'];
#List that includes list1, list2 and list3 called "the_list"
the_list = []
the_list.append(list1)
the_list.append(list2)
the_list.append(list3)
the_list
#insert new values into the list (these values represent state numbers)
list1.insert(3, 4)
list2.insert(3, 27)
list3.insert(3, 22)
#print the modified list
print (the_list)
#Organize the list from lowest to highest (based off numbers in index 1)
the_list.sort(key=lambda tup: tup[1])
print (the_list)
#filter states by category based off their population
#This is where I need help
#Small States
def lessThanTen(index):
return index < 10
the_list
filter(lessThanTen, the_list)
print (the_list)
#Big States
def greaterThanTen(index):
return index > 10
the_list
filter(greaterThanTen, the_list)
print (the_list)
有没有办法通过特定索引号将这些列表过滤到类别中,在本例中为索引 [1](即人口),然后通过打印输出这些列表项,或者它们的列表名称或它们的值在索引 [0]...示例 'Georgia' 或 "list1"
filter(function, iterable) Construct an iterator from those elements of iterable for which function returns true. iterable may be either a sequence, a container which supports iteration, or an iterator. If function is None, the identity function is assumed, that is, all elements of iterable that are false are removed.
Note that filter(function, iterable) is equivalent to the generator expression (item for item in iterable if function(item)) if function is not None and (item for item in iterable if item) if function is None.
不清楚你的意思,但我会尽力帮助你。
首先:你的greaterThanTen
函数将index
作为输入,至少看起来是这样。 filter
不会将索引作为参数传递给 greaterThanTen
,而是将索引处的元素作为参数传递。
还有一点:不知道大家是否理解filter
只有returns一个'category'作为输出——你可以一次只对一个条件进行排序。此外,过滤器不对原始列表进行操作,而是创建一个新序列,因此 filter(greaterThanTen, the_list)
实际上并没有改变任何东西。你应该做的是:the_list = filter(greaterThanTen, the_list)
.
如果要对列表中的每个元素按索引 1 处的值排序,可以这样做:
filter(lambda element: yourSortFunction(elmenet[1]), list)
这类似于您用作排序键的函数。
另一件事: 你为什么要在 greaterThanTen
中调用 the_list
,它不会感觉。该函数在 return
语句后停止计算代码。
正在打印:
如果您想打印列表中特定索引的值,只需询问该索引即可。
print(LIST[index])
希望对您有所帮助。
您可能只做某种列表理解并完全避免过滤。
the_final_list = [x for x in the_list if x[1] < 10]
这对我来说是 simpler/more 可读的并且完成了你的 objective。
如果您想将索引作为参数传递并保持列表的一些灵活性(您的索引可能不是 1),您可以这样做
def greaterThanTen(index):
return lambda x: x[index] > 10
def lessThanTen(index):
return lambda x: x[index] < 10
def myfilter(f, L):
return [x[0] for x in filter(f, L)]
print(myfilter(greaterThanTen(1), the_list)) # -> ['Georgia', 'Florida']
print(myfilter(lessThanTen(1), the_list)) # -> ['Alabama']
或者更笼统地说,
import operator
def index_vs_num(index):
ops = {
'>' : operator.gt,
'<' : operator.lt,
'>=': operator.ge,
'<=': operator.le,
'=' : operator.eq
}
return lambda relation: lambda num: lambda x: ops[relation](x[index], num)
greaterThanTwenty = index_vs_num(1)('>')(20)
# the 1st argument is index of your population in the list
# the 2nd argument is the type of comparation
# the 3rd argument is the number to be compared
lessThanFive = index_vs_num(1)('<')(5)
def filter_by_outindex(*index):
def filter_by_f(f, L):
try:
return [x[index[0]] for x in filter(f, L)]
except IndexError:
return list(filter(f, L))
return filter_by_f
myfilter=filter_by_outindex(0)
print(myfilter(greaterThanTwenty, the_list)) # -> ['Florida']
print(myfilter(lessThanFive, the_list)) # -> ['Alabama']
我认为这就是您真正想要实现的目标。