简体   繁体   English

将列表元素转换成数组

[英]Convert list elements into array

I have a list tsv file which I am parsing and want to convert it into an array. 我有一个正在解析的列表tsv文件,想要将其转换为数组。

Here is the file format - 这是文件格式-

jobname1 queue maphours reducehours
jobname2 queue maphours reducehours

code

with open(file.tsv) as tsv:
    line = [elem.strip().split('\t') for elem in tsv]
    vals = np.asarray(line)
    print vals[0]
    print vals[4]

Vals currently returns the following output - Vals当前返回以下输出-

['job1', 'queue', '1.0', '0.0\n']
['job2', 'queue', '1.0', '0.0\n']

I want to convert each element in a row in the entire file to an array object - 我想将整个文件中一行中的每个元素转换为一个数组对象-

vals[0] = job1 vals[1] = queue vals[2] = 1.0 vals[3] = 0.0 

How do i achieve this? 我该如何实现?

From what I understand you would like to create 2D array in numpy where each row of the file is a row corresponds to the created array, and column in a file is a column in the array. 据我了解,您想在numpy中创建2D数组,其中文件的每一行是一行,对应于所创建的数组,文件中的列是数组中的一列。 If so, you could do this as follows: 如果是这样,您可以按照以下步骤进行操作:

For example, if your data file is: 例如,如果您的数据文件是:

jobname1    queue   1   3
jobname2    queue   2   4
jobname41   queue   1   1
jobname32   queue   2   2
jobname21   queue   3   4
jobname12   queue   1   6

The following code: 如下代码:

with open(file) as tsv:
    line = [elem.strip().split('\t') for elem in tsv]

vals = np.asarray(line) 

will result in the following vals array: 将导致以下vals数组:

[['jobname1' 'queue' '1' '3']
 ['jobname2' 'queue' '2' '4']
 ['jobname41' 'queue' '1' '1']
 ['jobname32' 'queue' '2' '2']
 ['jobname21' 'queue' '3' '4']
 ['jobname12' 'queue' '1' '6']]

The get the job names you can do: 获取您可以执行的工作名称:

print(vals[:,0])
% gives ['jobname1' 'jobname2' 'jobname41' 'jobname32' 'jobname21' 'jobname12']

Or if you want rows containing some job, you can do: 或者,如果您想要包含某些作业的行,则可以执行以下操作:

print(vals[np.apply_along_axis(lambda row: row[0] == 'jobname1', 1, vals)])

Are you sure you need an array? 您确定需要阵列吗? @Marcin's answer is more complete if you want a Numpy array. 如果您想要一个Numpy数组,@ Marcin的答案会更完整。

Python doesn't have an array data structure (there's a list of Python data structures here ). Python没有一个数组(有Python数据结构的列表在这里 )。 There is a "thin wrapper around the C array" . 有一个“ C数组周围的薄包装器” In order to use the wrapper around the C array, you have to specify a type that the array will hold ( here you'll find a list of typecodes, at the top, and examples at the bottom): 为了在C数组周围使用包装器,您必须指定数组将要保存的类型( 在这里,您会在顶部找到类型代码列表,在底部找到示例):

If you want to use a numpy array, this should work: 如果要使用numpy数组,则应该可以使用:

import numpy as np
myarray = np.asarray(yourList)

adopted from here . 这里收养。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM