Python 使用筛选功能按特定索引对列表进行分类+按特定索引或列表名称打印

Python 使用筛选功能按特定索引对列表进行分类+按特定索引或列表名称打印,python,list,function,filter,categories,Python,List,Function,Filter,Categories,我正在用Python创建和操作一个列表,我无法通过过滤器函数对列表进行分类 我有3个列表,我将其附加到一个列表中,我经常在一路上打印_列表,下面是我的代码: 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"

我正在用Python创建和操作一个列表,我无法通过过滤器函数对列表进行分类

我有3个列表,我将其附加到一个列表中,我经常在一路上打印_列表,下面是我的代码:

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

filterfunction,iterable从这些元素构造一个迭代器 函数返回true的iterable的。iterable可以是 序列、支持迭代的容器或迭代器。如果 函数为None,则假定标识函数为all iterable中为false的元素将被删除

请注意,filterfunction,iterable相当于生成器 iterable if function中项的表达式项item if function 如果函数为item if,则iterable中的item不为None和item for item 没有

不清楚你的意思,但我会尽力帮助你

首先:您比函数以索引作为输入更强大,至少看起来是这样。筛选器不会将索引作为参数传递给greaterThanTen,而是传递该索引处的元素

另一件事:我不知道你们是否理解过滤器只返回一个“类别”作为输出——你们一次只能对一个条件进行排序。此外,filter不会对原始列表进行操作,而是创建一个新的序列,因此FilterCreate触角,_列表实际上不会更改任何内容。您应该做的是:过滤器列表=过滤器创建天线,过滤器列表

如果要按列表中每个元素的索引1处的值进行排序,可以执行以下操作:

filter(lambda element: yourSortFunction(elmenet[1]), list)
这类似于用作排序键的函数

还有一件事:你为什么试图用大于十的词组来称呼这个列表,这毫无意义。函数在返回语句后停止计算代码

印刷:

如果要打印列表中特定索引中的值,只需请求该索引即可

打印列表[索引]


我希望这会有所帮助。

您可能只需要做一些列表理解,而不必完全过滤

the_final_list = [x for x in the_list if x[1] < 10]

这对我来说更简单/更具可读性,并且实现了你的目标

如果您想将索引作为参数传递,并保持列表的灵活性,您可以在索引处使用除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']

我认为这就是您真正想要实现的。

我喜欢我的解决方案,因为它简单得多,但您的解决方案更完整,并且提供了良好的文档,因此+1您告诉我的是,此代码将根据我的规范重新排列我的列表?def greaterThanTenindex:return index>10\u list=filtergreaterhanten,此时不需要排序,只需分类即可。@josephgood catch。没必要分类,你说得对。筛选器只返回那些您的SortFunction返回True的项。要打印它,可能需要打印一些小状态,包括:_final_列表,我想对于大状态也是这样,我只需将运算符更改为大于并打印类似的结果?这段代码似乎允许我删除def和filter函数,对吗?您将得到一个与当前列表类似的列表,您可以随意使用该列表。正确的话,您可以安全地删除您的过滤线和过滤功能。如果x[1]<10,则可以执行小的_状态=[x代表_列表中的x],如果x[1]>20,则可以执行大的_状态=[x代表_列表中的x]或任何您想要的数字