Array of arrays (Python/NumPy)

前端 未结 4 1491
北荒
北荒 2021-01-01 12:56

I am using Python/NumPy, and I have two arrays like the following:

array1 = [1 2 3]
array2 = [4 5 6]

And I would like to create a new array

4条回答
  •  情话喂你
    2021-01-01 13:17

    If the file is only numerical values separated by tabs, try using the csv library: http://docs.python.org/library/csv.html (you can set the delimiter to '\t')

    If you have a textual file in which every line represents a row in a matrix and has integers separated by spaces\tabs, wrapped by a 'arrayname = [...]' syntax, you should do something like:

    import re
    f = open("your-filename", 'rb')
    result_matrix = []
    for line in f.readlines():
        match = re.match(r'\s*\w+\s+\=\s+\[(.*?)\]\s*', line)
        if match is None:
            pass # line syntax is wrong - ignore the line
        values_as_strings = match.group(1).split()
        result_matrix.append(map(int, values_as_strings))
    

提交回复
热议问题