kevin
kevin

Reputation: 2014

Remove elements from nested list - Python

data = [['A', 'B', 'C', 'D'],
        ['E', 'F', 'G'],
        ['I', 'J'],
        ['A', 'B', 'C', 'E', 'F']]

I would like to remove unpopular elements (appearing only once) from the lists. So the results should look like this:

data = [['A', 'B', 'C'],
        ['E', 'F'],
        ['A', 'B', 'C', 'E', 'F']]

I was able to count the frequency of each element using the following codes:

from collections import Counter
Counter(x for sublist in data for x in sublist)

#output
Counter({'A': 2, 'C': 2, 'B': 2, 'E': 2, 'F': 2, 'D': 1, 'G': 1, 'I': 1, 'J': 1})

However, I am not sure how to use this count information to remove unpopular elements from the list. Any help?

Upvotes: 1

Views: 192

Answers (2)

Xiaoqi Chu
Xiaoqi Chu

Reputation: 1557

The complexity is similar. Just use map and filter function to make the code more pythonic.

from collections import Counter
data = [['A', 'B', 'C', 'D'],
       ['E', 'F', 'G'],
       ['I', 'J'],
       ['A', 'B', 'C', 'E', 'F']]
counter = Counter({'A': 2, 'C': 2, 'B': 2, 'E': 2, 'F': 2, 'D': 1, 'G': 1, 'I': 1, 'J': 1})


result = map(lambda row: filter(lambda x: counter.get(x) > 1, row), data)
print result

Upvotes: 1

falsetru
falsetru

Reputation: 368874

Generate the new list based on the frequency information.

The following code uses nested list comprehension to do that:

from collections import Counter
freq = Counter(x for sublist in data for x in sublist)
data = [[x for x in row if freq[x] > 1] for row in data]  # Remove non-popular item
data = [row for row in data if row]  # Remove empty rows

# data => [['A', 'B', 'C'], ['E', 'F'], ['A', 'B', 'C', 'E', 'F']]

Upvotes: 1

Related Questions