用于分隔numpy数组的字典键和值

时间:2014-05-15 03:00:14

标签: python arrays numpy dictionary

我有一本词典

Samples = {5.207403005022627: 0.69973543384229719, 6.8970222167794759: 0.080782939731898179, 7.8338517407140973: 0.10308033284258854, 8.5301143255505334: 0.018640838362318335, 10.418899728838058: 0.14427355015329846, 5.3983946820220501: 0.51319796560976771}

我想将keysvalues分成2 numpy个数组。 我尝试np.array(Samples.keys(),dtype=np.float),但收到错误TypeError: float() argument must be a string or a number

6 个答案:

答案 0 :(得分:21)

您可以使用np.fromiter从字典键和值视图直接创建numpy数组:

在python 3中:

keys = np.fromiter(Samples.keys(), dtype=float)
vals = np.fromiter(Samples.values(), dtype=float)

在python 2中:

keys = np.fromiter(Samples.iterkeys(), dtype=float)
vals = np.fromiter(Samples.itervalues(), dtype=float)

答案 1 :(得分:10)

在python 3.4上,以下只是起作用:

Samples = {5.207403005022627: 0.69973543384229719, 6.8970222167794759: 0.080782939731898179, 7.8338517407140973: 0.10308033284258854, 8.5301143255505334: 0.018640838362318335, 10.418899728838058: 0.14427355015329846, 5.3983946820220501: 0.51319796560976771}

keys = np.array(list(Samples.keys()))
values = np.array(list(Samples.values()))

np.array(Samples.values())没有给出你在Python 3中的期望是因为在Python 3中,dict的values()方法返回一个可迭代的视图,而在Python 2中,它返回一个实际的列表钥匙。

keys = np.array(list(Samples.keys()))实际上也可以在Python 2.7中使用,并且会使您的代码更加与版本无关。但对list()的额外调用会使其缓慢减速。

答案 2 :(得分:1)

只需将所有值分配到列表中,然后转换为np.array()

import numpy as np

Samples = {5.207403005022627: 0.69973543384229719, 6.8970222167794759: 0.080782939731898179, 7.8338517407140973: 0.10308033284258854, 8.5301143255505334: 0.018640838362318335, 10.418899728838058: 0.14427355015329846, 5.3983946820220501: 0.51319796560976771}

keys = np.array(Samples.keys())
vals = np.array(Samples.values())

或者,如果you want to iterate over it

import numpy as np

Samples = {5.207403005022627: 0.69973543384229719, 6.8970222167794759: 0.080782939731898179, 7.8338517407140973: 0.10308033284258854, 8.5301143255505334: 0.018640838362318335, 10.418899728838058: 0.14427355015329846, 5.3983946820220501: 0.51319796560976771}

keys = vals = []

for k, v in Samples.items():
    keys.append(k)
    vals.append(v)

keys = np.array(keys)
vals = np.array(vals)

答案 3 :(得分:1)

在Python 3.7中:

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/linearlayoutweblist"
        android:layout_above="@+id/gettitle"
        android:orientation="vertical">

        <ListView
            android:id="@+id/listView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight=".53"
            android:choiceMode="singleChoice"
            android:listSelector="#a2aed3"
            android:layout_above="@+id/progressBar" />

        <Button
            android:layout_width="48dp"
            android:layout_height="wrap_content"
            android:maxHeight="5dp"
            android:minWidth="5dp"
            android:visibility="visible"
            android:background="@drawable/ic_highlight_off_black_24dp"
            android:id="@+id/closebtn"
            android:layout_gravity="right|end"
            android:gravity="right|end" />

        <ProgressBar
            android:id="@+id/progressBar"
            android:layout_above="@+id/webviewlay"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:indeterminate="true"
            android:layout_marginTop="-7dp"
            android:layout_marginBottom="-7dp"
            android:visibility="visible"
            style="@style/Widget.AppCompat.ProgressBar.Horizontal"
            />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/webviewlay"
            android:visibility="visible"
            android:paddingTop="1dp"
            android:layout_weight=".47"
            android:paddingBottom="2dp"
            android:background="@drawable/topline"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_above="@+id/gettitle">

            <WebView
                android:id="@+id/arabicfont"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                />

        </LinearLayout>


    </LinearLayout>



注意:重要的一点是,在此Python版本import numpy as np Samples = {5.207403005022627: 0.69973543384229719, 6.8970222167794759: 0.080782939731898179, 7.8338517407140973: 0.10308033284258854, 8.5301143255505334: 0.018640838362318335, 10.418899728838058: 0.14427355015329846, 5.3983946820220501: 0.51319796560976771} keys = np.array(list(Samples.keys())) vals = np.array(list(Samples.values())) dict.keys()中,它们分别返回类型为dict.values()dict_keys的对象。

答案 4 :(得分:0)

keys = np.array(dictionary.keys())
values = np.array(dictionary.values())

答案 5 :(得分:0)

如果您关心速度(Python 3.7)

rnd = np.random.RandomState(10)

for i in [10,100,1000,10000,100000]:
    test_dict = {j:j for j in rnd.uniform(-100,100,i)}
    assert len(test_dict) == i
    print(f"\nFor {i} keys\n-----------")
    
    %timeit keys = np.fromiter(test_dict.keys(), dtype=float)
    
    %timeit keys = np.array(list(test_dict.keys()))

np.fromiter快5-7倍

For 10 keys
-----------
712 ns ± 4.77 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
1.65 µs ± 9.15 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

For 100 keys
-----------
1.87 µs ± 13.7 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
8.02 µs ± 22.3 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

For 1000 keys
-----------
13.7 µs ± 27.7 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
70.5 µs ± 251 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

For 10000 keys
-----------
128 µs ± 70.6 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
698 µs ± 455 ns per loop (mean ± std. dev. of 7 runs, 1000 loops each)

For 100000 keys
-----------
1.45 ms ± 374 ns per loop (mean ± std. dev. of 7 runs, 1000 loops each)
7.14 ms ± 6.1 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)