Message Boards Message Boards

6 Replies
0 Total Likes
View groups...
Share this post:

Removing Items from Multi-Level Lists

Posted 10 years ago

Hi, All

I've pulled some data down from the new Entity[] feature of Mathematica by mapping across all known properties of a given entity. So my data has come back with some Missing[] items.

Schematically, what I have got is something like:

{{A, 1}, {B, 2}, {C, Missing[NotAvailable]}, {D, 4}}

...and what I want is...

{{A, 1}, {B, 2}, {D, 4}}

I know I should be able to use some combination of DeleteCases, MissingQ or the _Missing pattern, but I just don't seem to be able to combine them such that I am deleting the WHOLE of a level two list entry if it's second property is Missing.

It's embarrassing!

Please help.

Thanks in advance


POSTED BY: Brad Varey
6 Replies

Would this work?

Select[{{A, 1}, {B, 2}, {C, Missing[NotAvailable]}, {D, 4}}, NumberQ[#[[2]]] &]

or this

DeleteCases[{{A, 1}, {B, 2}, {C, Missing["NotAvailable"]}, {D, 4}} , {_, _Missing}]


POSTED BY: Marco Thiel
Posted 10 years ago

Thanks, Marco, but that doesn't quite do the trick.

It works on the schematic data I produced, which for simplicity showed all numerics in the second position of level 2.

But in fact, if you are looking at something like all the attributes of a country, some returned info is a String (the country name), another a graphic (the flag), another a number (the GDP) and (sadly!) another Missing[].

Your solution would work if there were a MissingQ[] function, which I could Not[]. But it appears there isn't one.



POSTED BY: Brad Varey

Hi Brad,

could you give an example?


POSTED BY: Marco Thiel

Say we use

{# , CountryData["Montenegro", #]} & /@ CountryData["Montenegro", "Properties"][[1 ;; 14]]

and obtain

{{"AdultPopulation", Quantity[431742., "People"]}, {"AgriculturalProducts", {"Cereals", "Citrus", "Grapes", "Olives", "Potatoes", "Sheep", "Tobacco"}}, {"AgriculturalValueAdded",
Quantity[3.84878*10^8, ("USDollars")/("Years")]}, {"Airports", 5}, {"AlternateNames", {}}, {"AlternateStandardNames", {}}, \ {"AMRadioStations", Missing["NotAvailable"]}, {"AnnualBirths",
Quantity[7137., ("People")/("Years")]}, {"AnnualDeaths",
Quantity[6564., ("People")/("Years")]}, {"AnnualHIVAIDSDeaths",
Missing["NotAvailable"]}, {"ArableLandArea", Quantity[1842.9, ("Kilometers")^2]}, {"ArableLandFraction", 0.137}, {"Area", Quantity[13812., ("Kilometers")^2]}, {"BirthRateFraction", Quantity[0.01114, ("People")/("People" "Years")]}}

That appears to have the structure you are taking about. The second function I suggested:

DeleteCases[{# , CountryData["Montenegro", #]} & /@ CountryData["Montenegro", "Properties"][[1 ;; 10]], {_, _Missing}]


{{"AdultPopulation", Quantity[431742., "People"]}, {"AgriculturalProducts", {"Cereals", "Citrus", "Grapes", "Olives", "Potatoes", "Sheep", "Tobacco"}}, {"AgriculturalValueAdded",
Quantity[3.84878*10^8, ("USDollars")/("Years")]}, {"Airports", 5}, {"AlternateNames", {}}, {"AlternateStandardNames", {}}, \ {"AnnualBirths", Quantity[7137., ("People")/("Years")]}, {"AnnualDeaths", Quantity[6564., ("People")/("Years")]}, {"ArableLandArea", Quantity[1842.9, ("Kilometers")^2]}, {"ArableLandFraction", 0.137}, {"Area", Quantity[13812., ("Kilometers")^2]}, {"BirthRateFraction", Quantity[0.01114, ("People")/("People" "Years")]}}

which appears to have all the Missing items deleted. I think it would help to have an example. It does appear to delete the whole level, but I still might misunderstand the problem.

Cheers, Marco

POSTED BY: Marco Thiel
Posted 10 years ago

Hi, Marco

Please excuse me.

I copied your code from the e-mail message that sent me, notifying me of your reply, which I hadn't noticed was only a truncated version of your full message. It only showed me your first suggestion, which did not work for the reasons I stated.

However, I now see your second suggestion, which in fact works perfectly.

So thank you very much.

Just by the way (and showcasing my inexperience!), I had tried pretty much what you had proposed in your second example, but had replaced the first underscore with All.

One of these days, I'll figure out why that doesn't work!

Regards & Thanks


POSTED BY: Brad Varey

Ok, sorry for the moot second post. Had not seen your reply.

Best wishes, Marco

POSTED BY: Marco Thiel
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract