Hướng dẫn how do you move a value in an array in python? - làm thế nào để bạn di chuyển một giá trị trong một mảng trong python?

3

Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.

Tôi có một mảng các giá trị

a = np.array([0,3,4,5,12,3,78,53,52])

Tôi muốn di chuyển ba yếu tố cuối cùng trong mảng bắt đầu từ chỉ mục 3 để có

a 
array([ 0, 3, 4, 78, 53, 52, 5, 12, 3])

hỏi ngày 27 tháng 2 năm 2019 lúc 11:05Feb 27, 2019 at 11:05

Hướng dẫn how do you move a value in an array in python? - làm thế nào để bạn di chuyển một giá trị trong một mảng trong python?

Bạn có thể sử dụng cắt và nối.

np.concatenate((a[:3], a[-3:], a[3:-3]))

Đã trả lời ngày 27 tháng 2 năm 2019 lúc 11:11Feb 27, 2019 at 11:11

Hướng dẫn how do you move a value in an array in python? - làm thế nào để bạn di chuyển một giá trị trong một mảng trong python?

Sven Krügersven KrügerSven Krüger

1.22710 huy hiệu bạc18 Huy hiệu đồng10 silver badges18 bronze badges

1

Hãy thử điều này bằng cách sử dụng

a 
array([ 0, 3, 4, 78, 53, 52, 5, 12, 3])
1 và
a 
array([ 0, 3, 4, 78, 53, 52, 5, 12, 3])
2:

a = np.array([0,3,4,5,12,3,78,53,52])
index = 6
another_index = 0
v = a[index]
np.delete(a,index)
np.insert(a, another_index, v)

Đã trả lời ngày 27 tháng 2 năm 2019 lúc 11:11Feb 27, 2019 at 11:11

Sven Krügersven Krügernumber-swapping problem -- not a numpy problem.

1.22710 huy hiệu bạc18 Huy hiệu đồngslicing, is inefficient, involving unnecessary copying of data.

Hãy thử điều này bằng cách sử dụng

a 
array([ 0, 3, 4, 78, 53, 52, 5, 12, 3])
1 và
a 
array([ 0, 3, 4, 78, 53, 52, 5, 12, 3])
2:

a[3],a[4],a[5], a[-3],a[-2],a[-1] = a[-3],a[-2],a[-1], a[3],a[4],a[5]

print(a)

Output:

[ 0  3  4 78 53 52  5 12  3]

Đây chỉ là một vấn đề hoán đổi số-không phải là một vấn đề khó khăn.Feb 27, 2019 at 12:49

Bất kỳ giải pháp nào cho vấn đề này liên quan đến các chức năng numpy như

a 
array([ 0, 3, 4, 78, 53, 52, 5, 12, 3])
3,
a 
array([ 0, 3, 4, 78, 53, 52, 5, 12, 3])
4,
a 
array([ 0, 3, 4, 78, 53, 52, 5, 12, 3])
5 hoặc thậm chí cắt, không hiệu quả, liên quan đến việc sao chép dữ liệu không cần thiết.fountainhead

Điều này sẽ hoạt động, với việc sao chép dữ liệu tối thiểu:1 gold badge8 silver badges17 bronze badges

0

Đã trả lời ngày 27 tháng 2 năm 2019 lúc 12:49

a = np.array([0,3,4,5,12,3,78,53,52])

Fountainheadfountainhead

newa=[]
for index, each in enumerate(a):
    if index<3:
        newa.append(a[index])
    else:
        newa.append(a[3+index%6])

Phim huy hiệu vàng 3.5141

[0, 3, 4, 78, 53, 52, 5, 12, 3]

Bắt đầu vớiFeb 27, 2019 at 11:10

Hướng dẫn how do you move a value in an array in python? - làm thế nào để bạn di chuyển một giá trị trong một mảng trong python?

Bạn chỉ có thể làm:zabop

cho kết quả

a 
array([ 0, 3, 4, 78, 53, 52, 5, 12, 3])
6 là:3 gold badges28 silver badges65 bronze badges

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc

    Bàn luận
    This particular functionality can be performed in one line by combining these functions. The append function adds the element removed by pop function using the index provided by index function.

    a 
    array([ 0, 3, 4, 78, 53, 52, 5, 12, 3])
    
    8
    a 
    array([ 0, 3, 4, 78, 53, 52, 5, 12, 3])
    
    9
    np.concatenate((a[:3], a[-3:], a[3:-3]))
    
    0
    np.concatenate((a[:3], a[-3:], a[3:-3]))
    
    1
    np.concatenate((a[:3], a[-3:], a[3:-3]))
    
    2223__2222225252222272722222929

    a = np.array([0,3,4,5,12,3,78,53,52])
    index = 6
    another_index = 0
    v = a[index]
    np.delete(a,index)
    np.insert(a, another_index, v)
    
    1
    a = np.array([0,3,4,5,12,3,78,53,52])
    index = 6
    another_index = 0
    v = a[index]
    np.delete(a,index)
    np.insert(a, another_index, v)
    
    2
    a = np.array([0,3,4,5,12,3,78,53,52])
    index = 6
    another_index = 0
    v = a[index]
    np.delete(a,index)
    np.insert(a, another_index, v)
    
    3
    a = np.array([0,3,4,5,12,3,78,53,52])
    index = 6
    another_index = 0
    v = a[index]
    np.delete(a,index)
    np.insert(a, another_index, v)
    
    4
    a = np.array([0,3,4,5,12,3,78,53,52])
    index = 6
    another_index = 0
    v = a[index]
    np.delete(a,index)
    np.insert(a, another_index, v)
    
    5
    a = np.array([0,3,4,5,12,3,78,53,52])
    index = 6
    another_index = 0
    v = a[index]
    np.delete(a,index)
    np.insert(a, another_index, v)
    
    6

    a = np.array([0,3,4,5,12,3,78,53,52])
    index = 6
    another_index = 0
    v = a[index]
    np.delete(a,index)
    np.insert(a, another_index, v)
    
    7
    a = np.array([0,3,4,5,12,3,78,53,52])
    index = 6
    another_index = 0
    v = a[index]
    np.delete(a,index)
    np.insert(a, another_index, v)
    
    8
    a = np.array([0,3,4,5,12,3,78,53,52])
    index = 6
    another_index = 0
    v = a[index]
    np.delete(a,index)
    np.insert(a, another_index, v)
    
    9

    a = np.array([0,3,4,5,12,3,78,53,52])
    index = 6
    another_index = 0
    v = a[index]
    np.delete(a,index)
    np.insert(a, another_index, v)
    
    1
    a = np.array([0,3,4,5,12,3,78,53,52])
    index = 6
    another_index = 0
    v = a[index]
    np.delete(a,index)
    np.insert(a, another_index, v)
    
    2
    a[3],a[4],a[5], a[-3],a[-2],a[-1] = a[-3],a[-2],a[-1], a[3],a[4],a[5]
    
    print(a)
    
    2
    a = np.array([0,3,4,5,12,3,78,53,52])
    index = 6
    another_index = 0
    v = a[index]
    np.delete(a,index)
    np.insert(a, another_index, v)
    
    4
    a = np.array([0,3,4,5,12,3,78,53,52])
    index = 6
    another_index = 0
    v = a[index]
    np.delete(a,index)
    np.insert(a, another_index, v)
    
    5
    a = np.array([0,3,4,5,12,3,78,53,52])
    index = 6
    another_index = 0
    v = a[index]
    np.delete(a,index)
    np.insert(a, another_index, v)
    
    6

    Đầu ra:

    The original list is : ['3', '5', '7', '9', '11']
    The modified element moved list is : ['3', '7', '9', '11', '5']
    

    Phương pháp số 2: Sử dụng phương thức sắp xếp

    a[3],a[4],a[5], a[-3],a[-2],a[-1] = a[-3],a[-2],a[-1], a[3],a[4],a[5]
    
    print(a)
    
    6 cũng có thể được sử dụng để đạt được nhiệm vụ cụ thể này, trong đó chúng tôi cung cấp khóa bằng với chuỗi mà chúng tôi muốn chuyển để nó được chuyển sang cuối.
    The sort method can also be used to achieve this particular task in which we provide the key as equal to the string we wish to shift so that it is moved to the end.

    a 
    array([ 0, 3, 4, 78, 53, 52, 5, 12, 3])
    
    8
    a 
    array([ 0, 3, 4, 78, 53, 52, 5, 12, 3])
    
    9
    np.concatenate((a[:3], a[-3:], a[3:-3]))
    
    0
    np.concatenate((a[:3], a[-3:], a[3:-3]))
    
    1
    np.concatenate((a[:3], a[-3:], a[3:-3]))
    
    2223__2222225252222272722222929

    a = np.array([0,3,4,5,12,3,78,53,52])
    index = 6
    another_index = 0
    v = a[index]
    np.delete(a,index)
    np.insert(a, another_index, v)
    
    1
    a = np.array([0,3,4,5,12,3,78,53,52])
    index = 6
    another_index = 0
    v = a[index]
    np.delete(a,index)
    np.insert(a, another_index, v)
    
    2
    a = np.array([0,3,4,5,12,3,78,53,52])
    index = 6
    another_index = 0
    v = a[index]
    np.delete(a,index)
    np.insert(a, another_index, v)
    
    3
    a = np.array([0,3,4,5,12,3,78,53,52])
    index = 6
    another_index = 0
    v = a[index]
    np.delete(a,index)
    np.insert(a, another_index, v)
    
    4
    a = np.array([0,3,4,5,12,3,78,53,52])
    index = 6
    another_index = 0
    v = a[index]
    np.delete(a,index)
    np.insert(a, another_index, v)
    
    5
    a = np.array([0,3,4,5,12,3,78,53,52])
    index = 6
    another_index = 0
    v = a[index]
    np.delete(a,index)
    np.insert(a, another_index, v)
    
    6

    a = np.array([0,3,4,5,12,3,78,53,52])
    
    6
    a 
    array([ 0, 3, 4, 78, 53, 52, 5, 12, 3])
    
    9
    np.concatenate((a[:3], a[-3:], a[3:-3]))
    
    3
    a = np.array([0,3,4,5,12,3,78,53,52])
    
    9

    a = np.array([0,3,4,5,12,3,78,53,52])
    index = 6
    another_index = 0
    v = a[index]
    np.delete(a,index)
    np.insert(a, another_index, v)
    
    1
    a = np.array([0,3,4,5,12,3,78,53,52])
    index = 6
    another_index = 0
    v = a[index]
    np.delete(a,index)
    np.insert(a, another_index, v)
    
    2
    a[3],a[4],a[5], a[-3],a[-2],a[-1] = a[-3],a[-2],a[-1], a[3],a[4],a[5]
    
    print(a)
    
    2
    a = np.array([0,3,4,5,12,3,78,53,52])
    index = 6
    another_index = 0
    v = a[index]
    np.delete(a,index)
    np.insert(a, another_index, v)
    
    4
    a = np.array([0,3,4,5,12,3,78,53,52])
    index = 6
    another_index = 0
    v = a[index]
    np.delete(a,index)
    np.insert(a, another_index, v)
    
    5
    a = np.array([0,3,4,5,12,3,78,53,52])
    index = 6
    another_index = 0
    v = a[index]
    np.delete(a,index)
    np.insert(a, another_index, v)
    
    6

    Đầu ra:

    The original list is : ['3', '5', '7', '9', '11']
    The modified element moved list is : ['3', '7', '9', '11', '5']
    


    Làm thế nào để bạn di chuyển một phần tử trong một mảng trong Python?

    Làm thế nào để bạn di chuyển một phần tử trong một mảng trong Python ?..
    A_List = Bộ sưu tập. Deque ([1, 2, 3, 4, 5]).
    một danh sách. xoay (2) dịch chuyển `a_list` 2 vị trí sang phải ..
    shifted_list = list (a_list).
    print(shifted_list).

    Làm thế nào để bạn di chuyển một số trong một mảng trong Python?

    Để chuyển các bit của các phần tử mảng số nguyên sang bên phải, hãy sử dụng phương thức numpy.right_shift () trong Python Numpy.Các bit được dịch chuyển sang bên phải x2 bên phải.Vì biểu diễn nội bộ của các số ở định dạng nhị phân, hoạt động này tương đương với việc chia x1 cho 2 ** x2.use the numpy. right_shift() method in Python Numpy. Bits are shifted to the right x2. Because the internal representation of numbers is in binary format, this operation is equivalent to dividing x1 by 2**x2.

    Làm thế nào để bạn di chuyển một giá trị trong một mảng?

    Để thay đổi vị trí của một phần tử trong một mảng: sử dụng phương thức splice () để chèn phần tử vào chỉ mục mới trong mảng.Phương thức Splice thay đổi mảng ban đầu bằng cách loại bỏ hoặc thay thế các phần tử hiện có hoặc thêm các phần tử mới vào một chỉ mục cụ thể.Use the splice() method to insert the element at the new index in the array. The splice method changes the original array by removing or replacing existing elements, or adding new elements at a specific index.

    Làm thế nào để bạn di chuyển một phần tử trong một mảng?

    Phương thức Shift () loại bỏ mục đầu tiên của một mảng.Phương thức Shift () thay đổi mảng ban đầu.Phương thức Shift () trả về phần tử thay đổi. removes the first item of an array. The shift() method changes the original array. The shift() method returns the shifted element.