簡體   English   中英

重組數組的數組並合並相同的項

[英]Restructure an array of arrays and combine same terms

我正在嘗試編寫一個函數,該函數需要一個數組數組,並在一定條件下將其重組為其他形式。 例如,假設:

array = [
    ["City1","Spanish", "163"],
    ["City1", "French", "194"],
    ["City2","English", "1239"],
    ["City2","Spanish", "1389"],
    ["City2", "French", "456"]
]

因此,我想創建一個新的數組,該數組按城市按字母順序排序,按語言按列排序(對列進行排序),所有null都將替換為0。例如,輸出到上述數組的應該是:

[
[0, 163, 194],
[1239, 1389, 456]
]

我寫了這種方法,但是我不確定邏輯上是否有意義。 它絕對是硬編碼的,我正在嘗試使其能夠以上述格式用於任何輸入。

import numpy as np

new_array = [[]]
x = 'City1'
y = 'City2'

def solution(arr):
    for row in arr:
        if row[0]==x:
            new_array[-1].append(row[2])
        else:
            x = x + 1
            c.append([row[2]])
solution(array)

我知道我需要修復語法,還需要編寫一個循環以按字母順序對事物進行排序。 我們將不勝感激,我想了解如何迭代這樣的數組並執行不同的功能並將數組重組為新格式。

如果性能不是您最關心的問題,則可以將Pandas與Categorical Datagroupby 之所以groupby ,是因為默認情況下,帶有類別的groupby使用類別系列的笛卡爾積:

import pandas as pd, numpy as np

# construct dataframe
df = pd.DataFrame(array, columns=['city', 'language', 'value'])

# convert to categories
for col in ['city', 'language']:
    df[col] = df[col].astype('category')

# groupby.first or groupby.sum works if you have unique combinations
res = df.sort_values(['city', 'language'])\
        .groupby(['city', 'language']).first().fillna(0).reset_index()

print(res)

    city language value
0  City1  English     0
1  City1   French   194
2  City1  Spanish   163
3  City2  English  1239
4  City2   French   456
5  City2  Spanish  1389

然后,對於所需的列表輸出列表:

res_lst = res.groupby('city')['value'].apply(list).tolist()
res_lst = [list(map(int, x)) for x in res_lst]

print(res_lst)

[[0, 194, 163], [1239, 456, 1389]]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM