I want to delete any column in my python array that has a number lesshan 2 in it
ID: 3553366 • Letter: I
Question
I want to delete any column in my python array that has a number lesshan 2 in it. How do I go about doing this?I have created an array using python like this:
import numpy as np
vecs=np.zeros((20,3))
for i in range(20):
for j in range(3):
vecs[i][0]=i+2
vecs[j][0]=j+2
vecs[i][1]= i+1
vecs[j][1]=j+1
vecs[i][2]=i+5
vecs[j][2]=j+5
print vecs
output
:[[ 2. 1. 5.]
[ 3. 2. 6.]
[ 4. 3. 7.]
[ 5. 4. 8.]
[ 6. 5. 9.]
[ 7. 6. 10.]
[ 8. 7. 11.]
[ 9. 8. 12.]
[ 10. 9. 13.]
[ 11. 10. 14.]
[ 12. 11. 15.]
[ 13. 12. 16.]
[ 14. 13. 17.]
[ 15. 14. 18.]
[ 16. 15. 19.]
[ 17. 16. 20.]
[ 18. 17. 21.]
[ 19. 18. 22.]
[ 20. 19. 23.]
[ 21. 20. 24.]]
so the entire second column should be deleted.How do i do this?
I want to delete any column in my python array that has a number lesshan 2 in it. How do I go about doing this?
I have created an array using python like this:
import numpy as np
vecs=np.zeros((20,3))
for i in range(20):
for j in range(3):
vecs[i][0]=i+2
vecs[j][0]=j+2
vecs[i][1]= i+1
vecs[j][1]=j+1
vecs[i][2]=i+5
vecs[j][2]=j+5
print vecs
output
:[[ 2. 1. 5.]
[ 3. 2. 6.]
[ 4. 3. 7.]
[ 5. 4. 8.]
[ 6. 5. 9.]
[ 7. 6. 10.]
[ 8. 7. 11.]
[ 9. 8. 12.]
[ 10. 9. 13.]
[ 11. 10. 14.]
[ 12. 11. 15.]
[ 13. 12. 16.]
[ 14. 13. 17.]
[ 15. 14. 18.]
[ 16. 15. 19.]
[ 17. 16. 20.]
[ 18. 17. 21.]
[ 19. 18. 22.]
[ 20. 19. 23.]
[ 21. 20. 24.]]
so the entire second column should be deleted.How do i do this?
I want to delete any column in my python array that has a number lesshan 2 in it. How do I go about doing this?
I have created an array using python like this:
import numpy as np
vecs=np.zeros((20,3))
for i in range(20):
for j in range(3):
vecs[i][0]=i+2
vecs[j][0]=j+2
vecs[i][1]= i+1
vecs[j][1]=j+1
vecs[i][2]=i+5
vecs[j][2]=j+5
print vecs
output
:[[ 2. 1. 5.]
[ 3. 2. 6.]
[ 4. 3. 7.]
[ 5. 4. 8.]
[ 6. 5. 9.]
[ 7. 6. 10.]
[ 8. 7. 11.]
[ 9. 8. 12.]
[ 10. 9. 13.]
[ 11. 10. 14.]
[ 12. 11. 15.]
[ 13. 12. 16.]
[ 14. 13. 17.]
[ 15. 14. 18.]
[ 16. 15. 19.]
[ 17. 16. 20.]
[ 18. 17. 21.]
[ 19. 18. 22.]
[ 20. 19. 23.]
[ 21. 20. 24.]]
so the entire second column should be deleted.How do i do this?
Explanation / Answer
The zip function is your friend. The output list has 20 rows of 3 columns. zip(*output) has 3 rows of 20 columns...and, yes, the * is required.I'll think of that as a list of columns, so take a look at this Idle 2.7 session, using your data:
>>> output
[[2, 1, 5], [3, 2, 6], [4, 3, 7], [5, 4, 8], [6, 5, 9], [7, 6, 10], [8, 7, 11], [9, 8, 12], [10, 9, 13], [11, 10, 14], [12, 11, 15], [13, 12, 16], [14, 13, 17], [15, 14, 18], [16, 15, 19], [17, 16, 20], [18, 17, 21], [19, 18, 22], [20, 19, 23], [21, 20, 24]]
>>> zip(*output)
[(2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21), (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20), (5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24)]
As advertised. The columns are now rows. That lets list comprehensions (or a for loop) look at whole columns:
>>> [min(col) for col in zip(*output)]
[2, 1, 5]
>>> [col for col in zip(*output) if min(col)>=2]
[(2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21), (5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24)]
That keeps each column whose minimum value is at least 2. All that's needed is to zip that back into 20 rows of however many columns remain.
>>> zip(*[col for col in zip(*output) if min(col)>=2])
[(2, 5), (3, 6), (4, 7), (5, 8), (6, 9), (7, 10), (8, 11), (9, 12), (10, 13), (11, 14), (12, 15), (13, 16), (14, 17), (15, 18), (16, 19), (17, 20), (18, 21), (19, 22), (20, 23), (21, 24)]
>>>
There you are, all in one expression that takes advantage of Python's builtins, min() and zip() in this case.
If having the rows be tuples instead of lists is a deal-breaker, you can do one last conversion:
>>> [list(row) for row in zip(*[col for col in zip(*output) if min(col)>=2])]
[[2, 5], [3, 6], [4, 7], [5, 8], [6, 9], [7, 10], [8, 11], [9, 12], [10, 13], [11, 14], [12, 15], [13, 16], [14, 17], [15, 18], [16, 19], [17, 20], [18, 21], [19, 22], [20, 23], [21, 24]]
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.