Skip to main content
Tweeted twitter.com/StackMma/status/948398681576886273
deleted 195 characters in body
Source Link
corey979
  • 24.3k
  • 7
  • 60
  • 105

Consider

DeleteCases[{{1, 2, 3, 4}, {1, 3, 4, 5}}, {_, 3, __}]

{{1, 2, 3, 4}}

i.e., keep only those lists that don't have 3 on their second position. That works well.

Now,

DeleteCases[{{3, 5, 7, 5, 2}, {1, 2, 3, 4, 5}, {1, 3, 4, 5}}, {__, 3, __}]

{{3, 5, 7, 5, 2}}

gives only those lists that don't have 3 somewhere in the middle, i.e. on positions 2, 3 or 4 (or, in other words, keeps only those lists where 3 is in positions 1 or 5).


Questions:Question

  1. How to make a general pattern that will drop only those lists that have 3 on the third position? (This is for me to learn and understand how to do it.)
  2. How to make a general pattern/function that will keep only those lists (of arbitrary length n) that don't have a given number a on a specific position k($1\leq k\leq n$)? (This is my overall purpose.)

How to make a general pattern/function that will keep only those lists (of arbitrary length n) that don't have a given number a on a specific position k($1\leq k\leq n$)?

Consider

DeleteCases[{{1, 2, 3, 4}, {1, 3, 4, 5}}, {_, 3, __}]

{{1, 2, 3, 4}}

i.e., keep only those lists that don't have 3 on their second position. That works well.

Now,

DeleteCases[{{3, 5, 7, 5, 2}, {1, 2, 3, 4, 5}, {1, 3, 4, 5}}, {__, 3, __}]

{{3, 5, 7, 5, 2}}

gives only those lists that don't have 3 somewhere in the middle, i.e. on positions 2, 3 or 4 (or, in other words, keeps only those lists where 3 is in positions 1 or 5).


Questions:

  1. How to make a general pattern that will drop only those lists that have 3 on the third position? (This is for me to learn and understand how to do it.)
  2. How to make a general pattern/function that will keep only those lists (of arbitrary length n) that don't have a given number a on a specific position k($1\leq k\leq n$)? (This is my overall purpose.)

Consider

DeleteCases[{{1, 2, 3, 4}, {1, 3, 4, 5}}, {_, 3, __}]

{{1, 2, 3, 4}}

i.e., keep only those lists that don't have 3 on their second position. That works well.

Now,

DeleteCases[{{3, 5, 7, 5, 2}, {1, 2, 3, 4, 5}, {1, 3, 4, 5}}, {__, 3, __}]

{{3, 5, 7, 5, 2}}

gives only those lists that don't have 3 somewhere in the middle, i.e. on positions 2, 3 or 4 (or, in other words, keeps only those lists where 3 is in positions 1 or 5).


Question

How to make a general pattern/function that will keep only those lists (of arbitrary length n) that don't have a given number a on a specific position k($1\leq k\leq n$)?

Source Link
corey979
  • 24.3k
  • 7
  • 60
  • 105

How to make a pattern to match?

Consider

DeleteCases[{{1, 2, 3, 4}, {1, 3, 4, 5}}, {_, 3, __}]

{{1, 2, 3, 4}}

i.e., keep only those lists that don't have 3 on their second position. That works well.

Now,

DeleteCases[{{3, 5, 7, 5, 2}, {1, 2, 3, 4, 5}, {1, 3, 4, 5}}, {__, 3, __}]

{{3, 5, 7, 5, 2}}

gives only those lists that don't have 3 somewhere in the middle, i.e. on positions 2, 3 or 4 (or, in other words, keeps only those lists where 3 is in positions 1 or 5).


Questions:

  1. How to make a general pattern that will drop only those lists that have 3 on the third position? (This is for me to learn and understand how to do it.)
  2. How to make a general pattern/function that will keep only those lists (of arbitrary length n) that don't have a given number a on a specific position k($1\leq k\leq n$)? (This is my overall purpose.)