Filter a list of lists

Paktwis Homayun

I would like to know how to filter a whole list out of list of lists

Example: [ ["Bob", "Baker", "male", "70000"],
         ["Alice", "Allen", "female", "82000"] ] 

And now I would like to filter the list which contains female. So output would be:

["Alice", "Allen", "female", "82000"]

Thanks

bheklilr

Ankur's answer will certainly solve your problem, but I would like to make a suggestion that could make your life easier. It seems that you're storing all your data as strings in lists, but really what you'd like is a data type that could hold all this data in a more organized fashion, which can be done using Haskell data types, something like:

data Person = Person {
    firstName :: String,
    lastName :: String,
    gender :: String,
    salary :: String
    } deriving (Eq, Show)

Then you could easily sort your data with filter (("female" ==) . gender) a. While this is a bit more code up front, later on if you were to add a "title" field for "Mr", "Mrs", etc, then it wouldn't matter if you added it at as the first field or the last field, this code would still work. Also, if for whatever reason you had an invalid value like ["Bob", "Baker", "male", "70000", "female"], Ankur's solution would give you an incorrect result, but with a custom data type, this would not even compile.

You could further improve your data structure with a few tweaks. I would suggest making a data type for gender, and then use Int or Double for the salary field, so you would have

data Gender = Male | Female deriving (Eq, Show, Read)

data Person = Person {
    firstName :: String,
    lastName :: String,
    gender :: Gender,
    salary :: Int
    } deriving (Eq, Show)

filtGender :: Gender -> [Person] -> [Person]
filtGender gend people = filter ((gend ==) . gender) people

main :: IO ()
main = do
    let people = [Person "Bob" "Baker" Male 70000,
                  Person "Alice" "Allen" Female 82000]
    putStr "The females are: "
    print $ filtGender Female people
    putStr "The males are: "
    print $ filtGender Male people

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

filter list into separate lists

From Dev

Filter list of lists in Scheme

From Dev

filter list into separate lists

From Dev

Filter tuples in list of lists [Haskell]

From Dev

scala filter a list of lists of tuples

From Dev

Filter Lists for Repeats - List Deduplication

From Dev

Filter tuples in list of lists [Haskell]

From Dev

Filter Python list of lists by another list

From Dev

Filter out null string in a list of lists

From Dev

How to filter two lists and make a new list

From Dev

How to filter list of lists using streams on the basis of a value in a list?

From Dev

How to use purrr with dplyr to filter list elements and export lists into Excel

From Dev

Filter and convert multiple dimension list-of-lists into matrix

From Dev

Erlang filter list using lists:keyfind partial string

From Dev

Linq to join two lists and filter using a value of inner list value

From Dev

Erlang filter list using lists:keyfind partial string

From Dev

Django filter filter with lists

From Dev

Uniqueness in a list of lists with lists

From Dev

Merging a list of lists of lists

From Dev

Uniqueness in a list of lists with lists

From Dev

Merging a list of lists of lists

From Dev

Lists in a list

From Dev

multiply list of lists by list of lists

From Dev

List of lists with tuples to list of lists

From Dev

List of lists with tuples to list of lists

From Dev

Filter DataFrame rows that are lists

From Dev

Lists except - filter a seq of string which not equal any item.A of another list

From Dev

Filter list of tuples to removes lists with all odd number or sum less than 80

From Dev

Java filter List that so it only contains objects that have same attribute as in another lists

Related Related

HotTag

Archive