簡體   English   中英

如何將列表的列表轉換為 python 中的數組?

[英]how to convert lists of lists into array in python?

我正在計算圖中一對節點的相似性分數,結果是列表的列表,如下所述:示例 output

[[[(0, 1), 0.3666666666666667], [(0, 13), 0.3333333333333333], [(0, 20), 0.23809523809523808],
      [(0, 23), 0.30952380952380953], [(20, 23), 0.21428571428571427], [(20, 31), 0.18253968253968253],
      [(20, 45), 0.21428571428571427], [(20, 46), 0.23809523809523808], [(20, 81), 0.27142857142857146],
      [(22, 24), 0.39285714285714285], [(22, 45), 0.39285714285714285], [(24, 45), 0.2857142857142857],
      [(24, 80), 0.6428571428571428], [(25, 26), 1.0], [(27, 28), 0.5333333333333333], [(27, 29), 0.45000000000000007],
      [(29, 71), 0.375]]]

在這里我有每個節點對相似度分數我怎么能把它放在矩陣形式中,每列都有節點和行裸露相似度分數? 任何幫助都感激不盡

這里的例子 output

在此處輸入圖像描述

你可以這樣做:

lst = [[[(0, 1), 0.3666666666666667], [(0, 13), 0.3333333333333333], [(0, 20), 0.23809523809523808], [(0, 23), 0.30952380952380953], [(20, 23), 0.21428571428571427], [(20, 31), 0.18253968253968253], [(20, 45), 0.21428571428571427], [(20, 46), 0.23809523809523808], [(20, 81), 0.27142857142857146], [(22, 24), 0.39285714285714285], [(22, 45), 0.39285714285714285], [(24, 45), 0.2857142857142857], [(24, 80), 0.6428571428571428], [(25, 26), 1.0], [(27, 28), 0.5333333333333333], [(27, 29), 0.45000000000000007], [(29, 71), 0.375]]]

# list of nodes in the graph
nodes = set()
for (x, y), _ in lst[0]:
    nodes.add(x)
    nodes.add(y)
nodes = sorted(nodes)

# create the 2D array to store similarity
# here, -1 denotes no data
similarity = [[-1] * len(nodes) for _ in range(len(nodes))]

# fill the 2D array
for (x, y), val in lst[0]:
    similarity[nodes.index(x)][nodes.index(y)] = val
    # if you don't want symmetry, comment the next line out
    similarity[nodes.index(y)][nodes.index(x)] = val

首先,我將刪除[ ]的一個外部級別和 , , 之一, ,因此我們可以將其轉換為字典,作為將其放入矩陣的一個步驟。

a=[[[(0, 1), 0.3666666666666667], [(0, 13), 0.3333333333333333], [(0, 20), 0.23809523809523808], [(0, 23), 0.30952380952380953],[(20, 23), 0.21428571428571427], [(20, 31), 0.18253968253968253], [(20, 45), 0.21428571428571427], [(20, 46), 0.23809523809523808], [(20, 81), 0.27142857142857146], [(22, 24), 0.39285714285714285], [(22, 45), 0.39285714285714285], [(24, 45), 0.2857142857142857], [(24, 80), 0.6428571428571428], [(25, 26), 1.0], [(27, 28), 0.5333333333333333], [(27, 29), 0.45000000000000007], [(29, 71), 0.375]]]
a=a[0] # remove one external level of `[ ]`
b=dict(a)

所以我們可以得到節點(n,m)的值如下:

if (n,m) in b.keys():
    print(b[n,m])
else:
    print(None) # or Zero

根據節點的最大維度,是30行72列,然后將字典轉化為數組,取值放入矩陣

import numpy as np

a = [[[(0, 1), 0.3666666666666667], [(0, 13), 0.3333333333333333], [(0, 20), 0.23809523809523808],
     [(0, 23), 0.30952380952380953], [(20, 23), 0.21428571428571427], [(20, 31), 0.18253968253968253],
     [(20, 45), 0.21428571428571427], [(20, 46), 0.23809523809523808], [(20, 81), 0.27142857142857146],
     [(22, 24), 0.39285714285714285], [(22, 45), 0.39285714285714285], [(24, 45), 0.2857142857142857],
     [(24, 80), 0.6428571428571428], [(25, 26), 1.0], [(27, 28), 0.5333333333333333], [(27, 29), 0.45000000000000007],
     [(29, 71), 0.375]]]
a=a[0] # remove one external level of `[ ]`
b = dict(a)
r = 30
c = 72
Nodes = np.zeros([r, c])
for k, values in b.items():
    Nodes[k[1]][k[0]] = values

現在,我們有了矩陣節點,它具有 position (r,c) 中原始列表的所有值。 節點的 rest 有零。

暫無
暫無

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

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