我有两个numpy.ndarray
[[' THE OLD TESTAMENT ']
[' SEAN SONG ']
[' CITY WALK ']]
和
[[' This is the name of an Old Testament ']
[' Hello this is great ']
[' Wait the way you are doing ']]
我想将这些ndarray转换成字典。
{
"THE OLD TESTAMENT": "This is the name of an Old Testament",
"SEAN SONG": "Hello this is great",
"CITY WALK": Wait the way you are doing
}
我正在使用下面编写的代码
keys = df.as_matrix()
print (keys)
values = df1.as_matrix()
print (values)
new_dict = dict(izip(keys, values))
答案 0 :(得分:2)
不必转换为arrays
,请使用Android Documentation of Bundle Tool和zip
选择第一列:
new_dict = dict(zip(df.iloc[:, 0], df1.iloc[:, 0]))
或按名称选择列:
new_dict = dict(zip(df['col'], df1['col']))
答案 1 :(得分:1)
先压缩数组:
In [1]: import numpy as np
In [2]: keys = np.array(
...: [[' THE OLD TESTAMENT '],
...: [' SEAN SONG '],
...: [' CITY WALK ']]
...: )
In [3]: values = np.array(
...: [[' This is the name of an Old Testament '],
...: [' Hello this is great '],
...: [' Wait the way you are doing ']]
...: )
In [4]: dict(zip(keys.squeeze(), values.squeeze()))
Out[4]:
{' CITY WALK ': ' Wait the way you are doing ',
' SEAN SONG ': ' Hello this is great ',
' THE OLD TESTAMENT ': ' This is the name of an Old Testament '}
或仅使用切片:
In [5]: dict(zip(keys[:,0], values[:,0]))
Out[5]:
{' CITY WALK ': ' Wait the way you are doing ',
' SEAN SONG ': ' Hello this is great ',
' THE OLD TESTAMENT ': ' This is the name of an Old Testament '}
答案 2 :(得分:0)
keyh=[[' THE OLD TESTAMENT '],
[' SEAN SONG '],
[' CITY WALK ']]
valueh=[[' This is the name of an Old Testament '],
[' Hello this is great '],
[' Wait the way you are doing ']]
dictionary = dict(zip([item[0] for item in keyh], [item[0] for item in valueh]))
输出
{
"THE OLD TESTAMENT": "This is the name of an Old Testament",
"SEAN SONG": "Hello this is great",
"CITY WALK": "Wait the way you are doing"
}
答案 3 :(得分:0)
keys = [[' THE OLD TESTAMENT '],[' SEAN SONG '],[' CITY WALK ']]
values = [[' This is the name of an Old Testament '], [' Hello this is great '],[' Wait the way you are doing ']]
keys = [x[0] for x in keys]
values = [x[0] for x in values]
dictionary = dict(zip(keys, values))