数组中的变量数

时间:2018-11-18 22:42:04

标签: python variable-length-array

我有一个密码;

x = np.linspace(0, 12, 200, False, True, None)

当我打印x时,我得到了;

(array([ 0.  ,  0.06,  0.12,  0.18,  0.24,  0.3 ,  0.36,  0.42,  0.48,
        0.54,  0.6 ,  0.66,  0.72,  0.78,  0.84,  0.9 ,  0.96,  1.02,
        1.08,  1.14,  1.2 ,  1.26,  1.32,  1.38,  1.44,  1.5 ,  1.56,
        1.62,  1.68,  1.74,  1.8 ,  1.86,  1.92,  1.98,  2.04,  2.1 ,
        2.16,  2.22,  2.28,  2.34,  2.4 ,  2.46,  2.52,  2.58,  2.64,
        2.7 ,  2.76,  2.82,  2.88,  2.94,  3.  ,  3.06,  3.12,  3.18,
        3.24,  3.3 ,  3.36,  3.42,  3.48,  3.54,  3.6 ,  3.66,  3.72,
        3.78,  3.84,  3.9 ,  3.96,  4.02,  4.08,  4.14,  4.2 ,  4.26,
        4.32,  4.38,  4.44,  4.5 ,  4.56,  4.62,  4.68,  4.74,  4.8 ,
        4.86,  4.92,  4.98,  5.04,  5.1 ,  5.16,  5.22,  5.28,  5.34,
        5.4 ,  5.46,  5.52,  5.58,  5.64,  5.7 ,  5.76,  5.82,  5.88,
        5.94,  6.  ,  6.06,  6.12,  6.18,  6.24,  6.3 ,  6.36,  6.42,
        6.48,  6.54,  6.6 ,  6.66,  6.72,  6.78,  6.84,  6.9 ,  6.96,
        7.02,  7.08,  7.14,  7.2 ,  7.26,  7.32,  7.38,  7.44,  7.5 ,
        7.56,  7.62,  7.68,  7.74,  7.8 ,  7.86,  7.92,  7.98,  8.04,
        8.1 ,  8.16,  8.22,  8.28,  8.34,  8.4 ,  8.46,  8.52,  8.58,
        8.64,  8.7 ,  8.76,  8.82,  8.88,  8.94,  9.  ,  9.06,  9.12,
        9.18,  9.24,  9.3 ,  9.36,  9.42,  9.48,  9.54,  9.6 ,  9.66,
        9.72,  9.78,  9.84,  9.9 ,  9.96, 10.02, 10.08, 10.14, 10.2 ,
       10.26, 10.32, 10.38, 10.44, 10.5 , 10.56, 10.62, 10.68, 10.74,
       10.8 , 10.86, 10.92, 10.98, 11.04, 11.1 , 11.16, 11.22, 11.28,
       11.34, 11.4 , 11.46, 11.52, 11.58, 11.64, 11.7 , 11.76, 11.82,
       11.88, 11.94]), 0.06)

我想找到返回了多少个变量。 为此,我已经尝试过

print (len(x))

我得到2。

这不正确,对吗?是否只有两个变量,其中一个是[]中的False,然后是True。还是我在某个地方犯了一个错误?如果是的话,我该怎么做才能获取返回的变量数量。

3 个答案:

答案 0 :(得分:1)

您已将.find参数设置为const obj = { "users": { "2211392761": { "username": "user1" }, "14300995184": { "username": "user2" }, "2781554712": { "username": "user3" }, "3554341": { "username": "user4" }, "202611": { "username": "user5" }, "17754300653761": { "username": "user6" } } } const findEntry = usernameToFind => { const foundEntry = Object.entries(obj.users) .find(([, { username }]) => username === usernameToFind); if (foundEntry) return foundEntry[0]; }; console.log(findEntry('user5')); console.log(findEntry('userthatdoesntexist'));,因此您将获得一个由数组和步长组成的元组。您可以通过retstep访问数组,并可以通过True访问数组的长度。但是,我希望执行以下操作:

x[0]

然后您将数组真正放在len(x[0])中,因此x, stepsize = np.linspace(0, 12, 200, False, True, None) 将按预期工作。

答案 1 :(得分:0)

在numpy中,shape允许您获取数组的维数。例如,此处的x.shape或x [0] .shape。问题在于您还返回了生成值的步骤,因此拥有值的数组和该步骤,因此为什么len(x)给您2,因为x包含两个对象。还要注意,您不包含对linspace()的调用中的最后一个值,这意味着您在生成的数组中未达到12。

答案 2 :(得分:0)

您获得的输出由具有两个元素的tuple组成。 x的第一个元素是长度为200的numpy数组,x的第二个元素是数字(0.06)。这是您可以找到的方式:

import numpy as np

x = np.linspace(0, 12, 200, False, True, None)

print x

>>> (np.array([ 0.  ,  0.06,  0.12,  0.18,  0.24,  0.3 ,  0.36,  0.42,  0.48,
    0.54,  0.6 ,  0.66,  0.72,  0.78,  0.84,  0.9 ,  0.96,  1.02,
    1.08,  1.14,  1.2 ,  1.26,  1.32,  1.38,  1.44,  1.5 ,  1.56,
    1.62,  1.68,  1.74,  1.8 ,  1.86,  1.92,  1.98,  2.04,  2.1 ,
    2.16,  2.22,  2.28,  2.34,  2.4 ,  2.46,  2.52,  2.58,  2.64,
    2.7 ,  2.76,  2.82,  2.88,  2.94,  3.  ,  3.06,  3.12,  3.18,
    3.24,  3.3 ,  3.36,  3.42,  3.48,  3.54,  3.6 ,  3.66,  3.72,
    3.78,  3.84,  3.9 ,  3.96,  4.02,  4.08,  4.14,  4.2 ,  4.26,
    4.32,  4.38,  4.44,  4.5 ,  4.56,  4.62,  4.68,  4.74,  4.8 ,
    4.86,  4.92,  4.98,  5.04,  5.1 ,  5.16,  5.22,  5.28,  5.34,
    5.4 ,  5.46,  5.52,  5.58,  5.64,  5.7 ,  5.76,  5.82,  5.88,
    5.94,  6.  ,  6.06,  6.12,  6.18,  6.24,  6.3 ,  6.36,  6.42,
    6.48,  6.54,  6.6 ,  6.66,  6.72,  6.78,  6.84,  6.9 ,  6.96,
    7.02,  7.08,  7.14,  7.2 ,  7.26,  7.32,  7.38,  7.44,  7.5 ,
    7.56,  7.62,  7.68,  7.74,  7.8 ,  7.86,  7.92,  7.98,  8.04,
    8.1 ,  8.16,  8.22,  8.28,  8.34,  8.4 ,  8.46,  8.52,  8.58,
    8.64,  8.7 ,  8.76,  8.82,  8.88,  8.94,  9.  ,  9.06,  9.12,
    9.18,  9.24,  9.3 ,  9.36,  9.42,  9.48,  9.54,  9.6 ,  9.66,
    9.72,  9.78,  9.84,  9.9 ,  9.96, 10.02, 10.08, 10.14, 10.2 ,
   10.26, 10.32, 10.38, 10.44, 10.5 , 10.56, 10.62, 10.68, 10.74,
   10.8 , 10.86, 10.92, 10.98, 11.04, 11.1 , 11.16, 11.22, 11.28,
   11.34, 11.4 , 11.46, 11.52, 11.58, 11.64, 11.7 , 11.76, 11.82,
   11.88, 11.94]), 0.06)

print type(x) 
>>> <type 'tuple'>

print len(x[0])  #This is the length of the first element of x (x[0]).
>>> 200 

print len(x)返回2的原因是,x是具有2元素的元组。同时x[0]x的第一个元素)是包含numpy个元素的200数组。 xx[1])的第二个元素是数字0.06