Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to filter an array of object in Python?

Tags:

I have a collection called project, this collection contain different documents, and every document contain an array of object called data.

enter image description here

I want to be able to filter the data (Excel files) by projectAlias and use pymongon and pandas to structure this file in SQL (Columns and row)

For instance

enter image description here

like image 826
Manfred Tijerino Avatar asked Oct 31 '19 19:10

Manfred Tijerino


People also ask

Can I filter an array of objects?

One can use filter() function in JavaScript to filter the object array based on attributes. The filter() function will return a new array containing all the array elements that pass the given condition. If no elements pass the condition it returns an empty array.

How do you filter an object in Python?

Filter() is a built-in function in Python. The filter function can be applied to an iterable such as a list or a dictionary and create a new iterator. This new iterator can filter out certain specific elements based on the condition that you provide very efficiently.

How do you filter an array of strings in Python?

filter() method is a very useful method of Python. One or more data values can be filtered from any string or list or dictionary in Python by using filter() method. It filters data based on any particular condition. It stores data when the condition returns true and discard data when returns false.


1 Answers

There's no code here, so I have to make some guesses:

  • I'm going to assume all your data is already in its own array, extracted from whatever form it originally came in. If it needed to be collected from multiple documents, I assume that's already done
  • I assume each object has a key "projectAlias" with a string value
  • I assume any objects without a "projectAlias" key have been dealt with
  • For laziness, I assume you want to order the data lexicographically (e.g. "a" < "b", "A" < "a")

Something like this might be useful:

#Made up function for first and third assumptions
data_array = collect_data(documents)
data_array.sort(key=lambda obj: obj["projectAlias"])

#Or, to create a new array with sorted data
sorted_data = sorted(data_array, key=lambda obj: obj["projectAlias"])

The key arg for python's built in sort function takes some sort of other function and runs it on each element of the array before sorting the results of that array. Then because python is helpful, it has predefined comparisons of strings for sorting which puts capital letters first then lowercase for the English alphabet. That changes when you get into accents, umlauts, and other variations. I have no insight there.

If your data needs some other sorting, you would want to define a different lambda function for key that results in outputs that fit your desired sort more. Another one could be by the length of the value:

#Made up function for first and third assumptions
data_array = collect_data(documents)
data_array.sort(key=lambda obj: len(obj["projectAlias"]))

#Or, to create a new array with sorted data
sorted_data = sorted(data_array, key=lambda obj: len(obj["projectAlias"]))

If you want more info, the "Key Function" section here in python.org's wiki might be useful

like image 81
ig- Avatar answered Sep 22 '22 16:09

ig-