Python 3 Array Operations I want to create an array of specified indexes.

Asked 2 years ago, Updated 2 years ago, 124 views

Let me ask you a question about Python 3 array operations.

I want to create an array of the specified indexes, but I don't know what to do.

I'd like to create an array B that consists of the third, sixth, and ninth of array A from array A. Does anyone have a good idea?

Array A = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Array B = [4,7,10]

If possible, I would like to do it all at once by not using For or append...

I wish del and pop could handle multiple ranges.

Thank you for your cooperation.

python array

2022-09-30 21:25

3 Answers

If the slices that have already been discussed do not work, I do not know if they can be called "bulk", but there is a inclusive.An example of the question is as follows:

# [Original list [i] for in extracting indexes]

a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
b = [a[i]for in(3,6,9)]
print(b)
# => [4,7,10]

(The word "nth" in Japanese is ambiguous, so you should be careful. Python and others have a zero index for the first element, but in Japanese, they are often referred to as "first").


2022-09-30 21:25

operator.itemgetter.

https://docs.python.org/ja/3/library/operator.html#operator.itemgetter

In[1]: A = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

In [2]: B = [4,7,10]

In[3]: from operator import itemgetter

In[4]: list (itemgetter(3,6,9)(A))
Out [4]: [4,7,10]


2022-09-30 21:25

I would like to do it all at once without For or append.

If you don't want to use For or append and you want to take out any array, isn't this OK?

a=[1, 2, 3, 4, 5, 8, 7, 8, 9, 10]
b=[a[3], a[6], a[9]]
print(b)
# = > [4,7,10]


2022-09-30 21:25

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.