简体   繁体   中英

Filter function doesn't work in python 2.7

For some reason I can't get the filter function to work. I'm trying to remove empty strings from a list. After reading Remove empty strings from a list of strings , I'm trying to utilize the filter function.

import csv
import itertools

importfile = raw_input("Enter Filename(without extension): ")
importfile += '.csv'
test=[]
#imports plant names, effector names, plant numbers and leaf numbers from csv file
with open(importfile) as csvfile:
    lijst = csv.reader(csvfile, delimiter=';', quotechar='|')
    for row in itertools.islice(lijst, 0, 4):
        test.append([row])

test1 = list(filter(None, test[3]))
print test1

This however returns:

[['leafs', '3', '', '', '', '', '', '', '', '', '', '']]

What am I doing wrong?

You filter a list of lists, where the inner item is a non-empty list.

>>> print filter(None, [['leafs', '3', '', '', '', '', '', '', '', '', '', '']])
[['leafs', '3', '', '', '', '', '', '', '', '', '', '']]

If you filter the inner list, the one that contains strings, everything works as expected:

>>> print filter(None, ['leafs', '3', '', '', '', '', '', '', '', '', '', ''])
['leafs', '3']

You have a list in a list, so filter(None, ...) is applied on the non-empty list, the empty strings are not affected. You can use, say a nested list comprehension to reach into the inner list and filter out falsy object:

lst = [['leafs', '3', '', '', '', '', '', '', '', '', '', '']]

test1 = [[x for x in i if x] for i in lst]
# [['leafs', '3']]

I was indeed filtering lists of lists, the problem in my code was:

    for row in itertools.islice(lijst, 0, 4):
    test.append[row]

This should be:

for row in itertools.islice(lijst, 0, 4):
        test.append(row)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM