【问题标题】:How can I change string into float list and 'n' of n dimensional list?如何将字符串更改为浮点列表和 n 维列表的“n”?
【发布时间】:2019-10-06 02:38:37
【问题描述】:

我有一个类似这个列表的代码,

['11,2019','12,2019']

我尝试使用 numpy 并希望得到结果

array([[[11],[2019]],[[12],[2019]]])

我尝试在 numpy 中重塑代码,

arr1= arr.reshpae(-1,1), 

但它只显示了这个结果

array([['11,2019'],['12,2019']])

不改变类型,字符串为浮点数或整数。

在列表步骤中,我尝试在开始 numpy 步骤之前更改类型。

import numpy as np

arr1=int(arr)

但它向我展示了 TypeError : int() 参数必须是字符串、类似字节的对象或数字,而不是“列表”

我希望通过将numpy中的每个元素分开来将一维列表变为二维列表

['11,2019','12,2019']  ->  array([[[11],[2019]],[[12],[2019]]])

【问题讨论】:

  • int 是一个 python 函数来创建一个整数。你需要用逗号分割这些字符串。

标签: python string list numpy


【解决方案1】:

我为 OP 考虑了一个评论相当多的解决方案。基本上,您将splitstr 项目用逗号分隔,map 将其添加到int,将其附加到list,然后将其转换为numpy 数组。

input_list = [ '11,2019', '12,2019' ]

# initialize variable for storing the result
output_list = []

# loop through every item in the input list
for item in input_list:

    # split each item of list by comma
    split_str = item.split( ',' )

    # convert split list of strings to int
    split_int = list( map( int, split_item ) )

    # store result to variable
    output_list.append( split_int )

# convert list to numpy (optional)
output_array = numpy.array( output_list )

【讨论】:

  • 漂亮又适合新手。但是考虑放弃不必要的复杂的 int 转换。让 numpy 更容易做到这一点:np.array(output_list, dtype=int) 将尽可能转换为 int。
【解决方案2】:

您可以只遍历初始列表的元素并在每个元素上使用 split 函数,以便将它们变成一维列表并将它们添加到输出二维列表:

import numpy as np

arr1d = ['11,2019','12,2019']
arr2d = [] #create an output list
for el in arr1d:
    #for each element in the 1d list
    #split that element into a 1d array and add to the 2d array
    arr2d.append(el.split(','))
#convert into a numpy array if that's necessary
output = np.array(arr2d)

【讨论】:

    【解决方案3】:

    试试这个。

    a=['11,2019','12,2019']
    d=np.array(list((map(lambda y:[[int(y.split(',')[0])],[int(y.split(',')[1])]],a))))
    print(d)
    

    输出:

    [[[  11]
      [2019]]
    
     [[  12]
      [2019]]]
    

    【讨论】:

      【解决方案4】:

      这里有三个选项(假设您真的是指二维数组)。

      inp = ['11,2019','12,2019']
      
      1. 最短,但最终会停止工作,一旦从numpy 中删除matrix 类:
      np.array(np.matrix(';'.join(inp)))
      # array([[  11, 2019],
      #        [  12, 2019]])
      

      2。这是你应该使用的:

      np.array([l.split(',') for l in inp], int)
      # array([[  11, 2019],
      #        [  12, 2019]])
      

      我们使用列表推导在逗号处拆分每个字符串。 np.array 的第二个参数是 dtype;我们用它来告诉 numpy 输入元素(字符串)应该被解释为整数。

      3. 这让 python 将字符串解析为整数元组。工作,但有点过度设计。
      import ast
      np.array([ast.literal_eval(l) for l in inp])
      # array([[  11, 2019],
      #        [  12, 2019]])
      

      PS:如果你真的想要你输入的内容,你必须在上面附加[..., None],例如:

      np.array([l.split(',') for l in inp], int)[..., None]
      # array([[[  11],
      #         [2019]],
      # 
      #        [[  12],
      #         [2019]]])
      

      【讨论】:

      • 你错过了一对 :) np.genfromtxt(inp, delimiter=',', dtype=int); np.stack(np.char.split(inp, ',')).astype(int)
      • @hpaulj 哎哟!第二个太恶心了!但是为什么不发布第一个呢?我实际上认为这可以说是“正确”的做法。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-11
      • 2015-10-28
      • 2021-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多