RE: DeleteCases : several at once, conditions
- To: mathgroup at smc.vnet.net
- Subject: [mg49385] RE: [mg49369] DeleteCases : several at once, conditions
- From: "David Park" <djmp at earthlink.net>
- Date: Sat, 17 Jul 2004 06:38:51 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
Use Alternatives (the upright bar that is the capital \ on the keyboard) to enter several patterns. list = {{1, 6, 7}, {3, 6, 9}, {1, 2, 11}, {3, 5, 9}, {2, 5, 6}, {2, 9, 11}, {2, 10, 11}, {3, 7, 9}, {5, 8, 11}, {3, 7, 9}, {7, 8, 9}, {2, 5, 12}, {2, 6, 10}, {2, 10, 11}, {1, 6, 9}, {6, 10, 12}, {3, 6, 12}, {2, 5, 12}, {3, 8, 9}, {1, 2, 7}}; Length[list] 20 DeleteCases[list, {1, _, _} | {9, _, _}] Length[%] {{3, 6, 9}, {3, 5, 9}, {2, 5, 6}, {2, 9, 11}, {2, 10, 11}, {3, 7, 9}, {5, 8, 11}, {3, 7, 9}, {7, 8, 9}, {2, 5, 12}, {2, 6, 10}, {2, 10, 11}, {6, 10, 12}, {3, 6, 12}, {2, 5, 12}, {3, 8, 9}} 16 To check the cases deleted (there were no cases with leading 9)... Cases[list, {1, _, _} | {9, _, _}] {{1, 6, 7}, {1, 2, 11}, {1, 6, 9}, {1, 2, 7}} To delete all cases that contain an even number... DeleteCases[list, {___, _?EvenQ, ___}] {{3, 5, 9}, {3, 7, 9}, {3, 7, 9}} The triple break characters mean zero or more items, so the pattern picks out all cases that contain at least one even number. David Park djmp at earthlink.net http://home.earthlink.net/~djmp/ From: camerzone [mailto:camerzone at yahoo.com] To: mathgroup at smc.vnet.net Suppose you have a list of lists, something like {{1, 6, 7}, {3, 6, 9}, {1, 2, 11}, {3, 5, 9}, {2, 5, 6}, {2, 9, 11}, { 2, 10, 11}, {3, 7, 9}, {5, 8, 11}, {3, 7, 9}, {7, 8, 9}, {2, 5, 12}, {2, 6, 10}, {2, 10, 11}, {1, 6, 9}, {6, 10, 12}, {3, 6, 12}, {2, 5, 12}, {3, 8, 9}, {1, 2, 7}} and you want to delete cases matching several different patterns, say {1,_,_} and {9,_,_} What's the most efficient way? Also, can you use conditions with DeleteCases? Say, I want to delete all cases containing even numbers? tks