简体   繁体   中英

Pythonic way of Adding Columns to Array

I'm trying to combine an m x n array called data with a list of m elements called cluster_data such that each element in the list cluster_data is appended as the last element of each of the rows in data .

As an example, I would want something like to combine

data = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16],[17,18,19,20]]

cluster_data = [1,2,3,4,5]

Such that

final_data = [[1,2,3,4,1],[5,6,7,8,2],[9,10,11,12,3],[13,14,15,16,4],[17,18,19,20,5]]

I have written some code that does this, but I was hoping for a more Pythonic way.

data_with_clusters = []    
for i, row in enumerate(data):
    row.append(cluster_data[i])
    data_with_clusters.append(row)

My best guess so far, which doesn't work, is:

data_with_clusters = [row.append(cluster_data[i]) for i, row in enumerate(data)]

我认为这是最蟒蛇的方式

final_data = [i+[j] for i,j in zip(data, cluster_data)]

The problem with your approach is that append doesn't return anything.

Instead of append, merge lists:

[row + [cluster_data[i]] for i, row in enumerate(data)]

or

[e[0] + [e[1]] for e in zip(data, cluster_data)]

A Pythonic way would be to use Array first thing first. Lists are commonly abused as array , because they share some similarity.

But a more Pythonic way, if you often work with numbers is to use NumPy. Which makes such operations a piece of cake.

The answers with list comprehension given previously are fine too, but they will be extremely slow for large arrays.

Here is your intro to NumPy:

In [2]: import numpy as np
In [3]: array = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16],[17,18,19,20]])

In [3]: array
Out[3]: 
array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12],
       [13, 14, 15, 16],
       [17, 18, 19, 20]])

In [4]: col = np.array([[1],[2],[3],[4],[5]])
In [4]: col
Out[4]: 
array([[1],
       [2],
       [3],
       [4],
       [5]])

In [5]: np.append(array, col, axis=1)
Out[5]: 
array([[ 1,  2,  3,  4,  1],
       [ 5,  6,  7,  8,  2],
       [ 9, 10, 11, 12,  3],
       [13, 14, 15, 16,  4],
       [17, 18, 19, 20,  5]])

row.append(cluster_data[i]) returns None, so that doesn't work,

Try instead: row + [cluster_data[i]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM