can anybody try to help me to retrieve numbers in Python and each number to an array:
I have done the following code, it does the job but it reads 10 as two numbers:
with open("test.dat") as infile:
for i, line in enumerate(infile):
if i == 0:
for x in range(0, len(line)):
if(line[x] == ' ' or line[x] == " "):
continue
else:
print(x, " " , line[x], ", ")
initial_state.append(line[x])
---Results:
(0, ' ', '1', ', ')
(2, ' ', '2', ', ')
(4, ' ', '3', ', ')
(6, ' ', '4', ', ')
(8, ' ', '5', ', ')
(10, ' ', '6', ', ')
(12, ' ', '7', ', ')
(14, ' ', '8', ', ')
(16, ' ', '9', ', ')
(18, ' ', '1', ', ')
(19, ' ', '0', ', ')
(21, ' ', '1', ', ')
(22, ' ', '1', ', ')
(24, ' ', '1', ', ')
(25, ' ', '2', ', ')
(27, ' ', '1', ', ')
(28, ' ', '3', ', ')
(30, ' ', '1', ', ')
(31, ' ', '4', ', ')
(33, ' ', '1', ', ')
(34, ' ', '5', ', ')
(36, ' ', '0', ', ')
(37, ' ', '\n', ', ')
index include spaces, please see the line of numbers im trying to add to array
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0
Use .split() to split all fields by looping through, please see the following code, it should do it
with open("test.dat") as infile:
for i, line in enumerate(infile):
if i == 0: # if first line
field = [field.split(" ") for field in line.split(" ")]
for x in range(0, len(field)):
initial_state_arr.append(field[x])
If you are sure each number is separated by a single space why not just split the line and print each element as an array:
with open("test.dat") as infile:
content = infile.read().split()
for index, number in enumerate(content):
print ((index*2, number))
And what is your exact input and expected result? Does the file have multiple spaces between numbers?
I am parsing out /proc/PID/stat of a process. The file has input of:
25473 (firefox) S 25468 25465 25465 0 -1 4194304 149151169 108282 32 15 2791321 436115 846 86 20 0 84 0 9648305 2937786368 209665 18446744073709551615 93875088982016 93875089099888 140722931705632 140722931699424 140660842079373 0 0 4102 33572009 0 0 0 17 1 0 0 175 0 0 93875089107104 93875089109128 93875116752896 140722931707410 140722931707418 140722931707418 140722931707879 0
I came up with:
import re
def get_stats(pid):
with open('/proc/{}/stat'.format(pid)) as fh:
stats_raw = fh.read()
stat_pattern = '(\d+\s)(\(.+\)\s)(\w+\s)(-?\d+\s?)'
return re.findall(stat_pattern, stats_raw)
This will match the first three groups but only return one field for the last group of (-?\d+\s?):
[('25473 ', '(firefox) ', 'S ', '25468 ')]
I was looking for a way to match only set number for the last group:
'(\d+\s)(\(.+\)\s)(\w+\s)(-?\d+\s?){49}'
You cannot access each repeated capture with re regex. You may capture the whole rest of the string into Group 4 and then split with whitespace:
import re
s = r'25473 (firefox) S 25468 25465 25465 0 -1 4194304 149151169 108282 32 15 2791321 436115 846 86 20 0 84 0 9648305 2937786368 209665 18446744073709551615 93875088982016 93875089099888 140722931705632 140722931699424 140660842079373 0 0 4102 33572009 0 0 0 17 1 0 0 175 0 0 93875089107104 93875089109128 93875116752896 140722931707410 140722931707418 140722931707418 140722931707879 0'
stat_pattern = r'(\d+)\s+(\([^)]+\))\s+(\w+)\s*(.*)'
res = []
for m in re.finditer(stat_pattern, s):
res.append(m.group(1))
res.append(m.group(2))
res.append(m.group(3))
res.extend(m.group(4).split())
print(res)
Output:
['25473', '(firefox)', 'S', '25468', '25465', '25465', '0', '-1', '4194304', '149151169', '108282', '32', '15', '2791321', '436115', '846', '86', '20', '0', '84', '0', '9648305', '2937786368', '209665', '18446744073709551615', '93875088982016', '93875089099888', '140722931705632', '140722931699424', '140660842079373', '0', '0', '4102', '33572009', '0', '0', '0', '17', '1', '0', '0', '175', '0', '0', '93875089107104', '93875089109128', '93875116752896', '140722931707410', '140722931707418', '140722931707418', '140722931707879', '0']
If you literally need to only get 49 numbers into Group 4, use
r'(\d+)\s+(\([^)]+\))\s+(\w+)\s*((?:-?\d+\s?){49})'
^^^^^^^^^^^^^^^^^^
With PyPi regex module, you may use r'(?P<o>\d+)\s+(?P<o>\([^)]+\))\s+(?P<o>\w+)\s+(?P<o>-?\d+\s?){49}' and after running a regex.search(pattern, s) access .captures("o") stack with the values you need.
>>> import regex
>>> s = '25473 (firefox) S 25468 25465 25465 0 -1 4194304 149151169 108282 32 15 2791321 436115 846 86 20 0 84 0 9648305 2937786368 209665 18446744073709551615 93875088982016 93875089099888 140722931705632 140722931699424 140660842079373 0 0 4102 33572009 0 0 0 17 1 0 0 175 0 0 93875089107104 93875089109128 93875116752896 140722931707410 140722931707418 140722931707418 140722931707879 0'
>>> stat_pattern = r'(?P<o>\d+)\s+(?P<o>\([^)]+\))\s+(?P<o>\w+)\s+(?P<o>-?\d+\s?){49}'
>>> m = regex.search(stat_pattern, s)
>>> if m:
print(m.captures("o"))
Output:
['25473', '(firefox)', 'S', '25468 ', '25465 ', '25465 ', '0 ', '-1 ', '4194304 ', '149151169 ', '108282 ', '32 ', '15 ', '2791321 ', '436115 ', '846 ', '86 ', '20 ', '0 ', '84 ', '0 ', '9648305 ', '2937786368 ', '209665 ', '18446744073709551615 ', '93875088982016 ', '93875089099888 ', '140722931705632 ', '140722931699424 ', '140660842079373 ', '0 ', '0 ', '4102 ', '33572009 ', '0 ', '0 ', '0 ', '17 ', '1 ', '0 ', '0 ', '175 ', '0 ', '0 ', '93875089107104 ', '93875089109128 ', '93875116752896 ', '140722931707410 ', '140722931707418 ', '140722931707418 ', '140722931707879 ', '0']
I am creating a maze traverser in Python. Initially I read the maze txt file as a list, but I am unable to print the maze line by line. We are given the number of rows and columns, the row and column of the entrance, and row and column of the exit.
What my ouput is:
[['5', ' ', '5', ' ', '4', ' ', '1', ' ', '0', ' ', '1'], ['#', ' ', '#', '#', '#'], ['#', ' ', '#', ' ', '#'], ['#', ' ', '#', ' ', '#'], ['#', ' ', ' ', ' ', '#'], ['#', ' ', '#', '#', '#']]
what I am looking for:
5 5 4 1 0 1
# ###
# # #
# # #
# #
# ###
My test code to print out the maze:
#read MAZE and print
def readMaze(maze, filename):
mazeFile = open(filename, "r")
columns = mazeFile.readlines()
for column in columns:
column = column.strip()
row = [i for i in column]
maze.append(row)
maze =[]
readMaze(maze, "maze01.txt")
print maze
If your maze list is like this:
maze = [['5', ' ', '5', ' ', '4', ' ', '1', ' ', '0', ' ', '1'], ['#', ' ', '#', '#', '#'], ['#', ' ', '#', ' ', '#'], ['#', ' ', '#', ' ', '#'], ['#', ' ', ' ', ' ', '#'], ['#', ' ', '#', '#', '#']]
You could print it and have your desired print output using join and a for loop like this example:
for i in maze:
print("".join(i))
Output:
5 5 4 1 0 1
# ###
# # #
# # #
# #
# ###
You're simply printing the whole list without ever iterating over it to print the characters how you want them. You need to use a for loop just like you have in your readMaze function to iterate over the top-level list, and on each element (which is a list of characters), use join to concatenate the characters into one string, print it, then move onto the next line
# your input list has multiple nested sub-lists
l = [
['5', ' ', '5', ' ', '4', ' ', '1', ' ', '0', ' ', '1'],
['#', ' ', '#', '#', '#'],
['#', ' ', '#', ' ', '#'],
['#', ' ', '#', ' ', '#'],
['#', ' ', ' ', ' ', '#'],
['#', ' ', '#', '#', '#']
]
# so we iterate over them...
for sublist in l:
print(''.join(sublist)) # ...and concatenate them together before printing
Output:
5 5 4 1 0 1
# ###
# # #
# # #
# #
# ###
I am starting a Sudoku program and am experimenting with efficiently copying an unsigned int[] into a char[] while viewing the resulting AT&T x86-64 assembly objdump -d result. The point of what I am doing is to produce an output like this (which my code currently does) for learning purposes:
------- ------- -------
| | 2 7 | |
| 1 5 | | 7 8 |
| 6 | 4 1 | 3 |
------- ------- -------
| 4 | | 9 |
| 1 9 | 3 | 2 5 |
| | | |
------- ------- -------
| 8 | 9 | 1 |
| 2 | 6 8 | 3 |
| 6 | | 4 |
------- ------- -------
I am using an unsigned int[] to represent the board and have a static char[] within the printBoard function for the purpose of displaying the board when I call printBoard(const unsigned int* ac_board). What I was hoping for was having the static char[] be initialized with the formatting needed and have 81 copies from the unsigned int[] into the static char[] each time I call printBoard
At the moment I have not been able to achieve this, I have 81 * 2 copies occurring in my code following the pattern (I am compiling as g++ -std=c++14 -O3 "$1" -o "$2"):
...
100000adc: 88 05 ba 05 00 00 mov %al,0x5ba(%rip) # 10000109c <__ZZ10printBoardPKjE10s_printBuf+0x1c>
100000ae2: 8b 47 04 mov 0x4(%rdi),%eax
100000ae5: 88 05 b3 05 00 00 mov %al,0x5b3(%rip) # 10000109e <__ZZ10printBoardPKjE10s_printBuf+0x1e>
100000aeb: 8b 47 08 mov 0x8(%rdi),%eax
100000aee: 88 05 ac 05 00 00 mov %al,0x5ac(%rip) # 1000010a0 <__ZZ10printBoardPKjE10s_printBuf+0x20>
100000af4: 8b 47 0c mov 0xc(%rdi),%eax
100000af7: 88 05 a7 05 00 00 mov %al,0x5a7(%rip) # 1000010a4 <__ZZ10printBoardPKjE10s_printBuf+0x24>
100000afd: 8b 47 10 mov 0x10(%rdi),%eax
100000b00: 88 05 a0 05 00 00 mov %al,0x5a0(%rip) # 1000010a6 <__ZZ10printBoardPKjE10s_printBuf+0x26>
100000b06: 8b 47 14 mov 0x14(%rdi),%eax
...
Is there a way to actually achieve 81 mov instructions rather than 81 * 2?
Below is my main.cpp:
#include <ctime>
#include "print.hpp"
// -----------------------------------------------------------------------------
unsigned int g_board[81] = {
0x20, 0x20, 0x20, 0x32, 0x20, 0x37, 0x20, 0x20, 0x20,
0x20, 0x31, 0x35, 0x20, 0x20, 0x20, 0x37, 0x38, 0x20,
0x20, 0x36, 0x20, 0x34, 0x20, 0x31, 0x20, 0x33, 0x20,
0x20, 0x20, 0x34, 0x20, 0x20, 0x20, 0x39, 0x20, 0x20,
0x31, 0x39, 0x20, 0x20, 0x33, 0x20, 0x20, 0x32, 0x35,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x38, 0x20, 0x20, 0x39, 0x20, 0x20, 0x31, 0x20,
0x20, 0x20, 0x32, 0x36, 0x20, 0x38, 0x33, 0x20, 0x20,
0x36, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x34
};
// -----------------------------------------------------------------------------
int main() {
::std::clock_t begin = clock();
printBoard(g_board);
::std::clock_t end = clock();
::std::cout << (end - begin) << ::std::endl;
return 0;
}
Here is my current best implementation of print.hpp:
#include <iostream>
// -----------------------------------------------------------------------------
void printBoard(const unsigned int* ac_board) {
static char s_printBuf[] = {
' ', '-', '-', '-', '-', '-', '-', '-', ' ', '-', '-', '-', '-',
'-', '-', '-', ' ', '-', '-', '-', '-', '-', '-', '-', ' ', '\n',
'|', ' ', 'x', ' ', 'x', ' ', 'x', ' ', '|', ' ', 'x', ' ', 'x',
' ', 'x', ' ', '|', ' ', 'x', ' ', 'x', ' ', 'x', ' ', '|', '\n',
'|', ' ', 'x', ' ', 'x', ' ', 'x', ' ', '|', ' ', 'x', ' ', 'x',
' ', 'x', ' ', '|', ' ', 'x', ' ', 'x', ' ', 'x', ' ', '|', '\n',
'|', ' ', 'x', ' ', 'x', ' ', 'x', ' ', '|', ' ', 'x', ' ', 'x',
' ', 'x', ' ', '|', ' ', 'x', ' ', 'x', ' ', 'x', ' ', '|', '\n',
' ', '-', '-', '-', '-', '-', '-', '-', ' ', '-', '-', '-', '-',
'-', '-', '-', ' ', '-', '-', '-', '-', '-', '-', '-', ' ', '\n',
'|', ' ', 'x', ' ', 'x', ' ', 'x', ' ', '|', ' ', 'x', ' ', 'x',
' ', 'x', ' ', '|', ' ', 'x', ' ', 'x', ' ', 'x', ' ', '|', '\n',
'|', ' ', 'x', ' ', 'x', ' ', 'x', ' ', '|', ' ', 'x', ' ', 'x',
' ', 'x', ' ', '|', ' ', 'x', ' ', 'x', ' ', 'x', ' ', '|', '\n',
'|', ' ', 'x', ' ', 'x', ' ', 'x', ' ', '|', ' ', 'x', ' ', 'x',
' ', 'x', ' ', '|', ' ', 'x', ' ', 'x', ' ', 'x', ' ', '|', '\n',
' ', '-', '-', '-', '-', '-', '-', '-', ' ', '-', '-', '-', '-',
'-', '-', '-', ' ', '-', '-', '-', '-', '-', '-', '-', ' ', '\n',
'|', ' ', 'x', ' ', 'x', ' ', 'x', ' ', '|', ' ', 'x', ' ', 'x',
' ', 'x', ' ', '|', ' ', 'x', ' ', 'x', ' ', 'x', ' ', '|', '\n',
'|', ' ', 'x', ' ', 'x', ' ', 'x', ' ', '|', ' ', 'x', ' ', 'x',
' ', 'x', ' ', '|', ' ', 'x', ' ', 'x', ' ', 'x', ' ', '|', '\n',
'|', ' ', 'x', ' ', 'x', ' ', 'x', ' ', '|', ' ', 'x', ' ', 'x',
' ', 'x', ' ', '|', ' ', 'x', ' ', 'x', ' ', 'x', ' ', '|', '\n',
' ', '-', '-', '-', '-', '-', '-', '-', ' ', '-', '-', '-', '-',
'-', '-', '-', ' ', '-', '-', '-', '-', '-', '-', '-', ' ', '\n',
0
};
s_printBuf[28] = ac_board[0];
s_printBuf[30] = ac_board[1];
s_printBuf[32] = ac_board[2];
s_printBuf[36] = ac_board[3];
s_printBuf[38] = ac_board[4];
s_printBuf[40] = ac_board[5];
s_printBuf[44] = ac_board[6];
s_printBuf[46] = ac_board[7];
s_printBuf[48] = ac_board[8];
s_printBuf[54] = ac_board[9];
s_printBuf[56] = ac_board[10];
s_printBuf[58] = ac_board[11];
s_printBuf[62] = ac_board[12];
s_printBuf[64] = ac_board[13];
s_printBuf[66] = ac_board[14];
s_printBuf[70] = ac_board[15];
s_printBuf[72] = ac_board[16];
s_printBuf[74] = ac_board[17];
s_printBuf[80] = ac_board[18];
s_printBuf[82] = ac_board[19];
s_printBuf[84] = ac_board[20];
s_printBuf[88] = ac_board[21];
s_printBuf[90] = ac_board[22];
s_printBuf[92] = ac_board[23];
s_printBuf[96] = ac_board[24];
s_printBuf[98] = ac_board[25];
s_printBuf[100] = ac_board[26];
s_printBuf[132] = ac_board[27];
s_printBuf[134] = ac_board[28];
s_printBuf[136] = ac_board[29];
s_printBuf[140] = ac_board[30];
s_printBuf[142] = ac_board[31];
s_printBuf[144] = ac_board[32];
s_printBuf[148] = ac_board[33];
s_printBuf[150] = ac_board[34];
s_printBuf[152] = ac_board[35];
s_printBuf[158] = ac_board[36];
s_printBuf[160] = ac_board[37];
s_printBuf[162] = ac_board[38];
s_printBuf[166] = ac_board[39];
s_printBuf[168] = ac_board[40];
s_printBuf[170] = ac_board[41];
s_printBuf[174] = ac_board[42];
s_printBuf[176] = ac_board[43];
s_printBuf[178] = ac_board[44];
s_printBuf[184] = ac_board[45];
s_printBuf[186] = ac_board[46];
s_printBuf[188] = ac_board[47];
s_printBuf[192] = ac_board[48];
s_printBuf[194] = ac_board[49];
s_printBuf[196] = ac_board[50];
s_printBuf[200] = ac_board[51];
s_printBuf[202] = ac_board[52];
s_printBuf[204] = ac_board[53];
s_printBuf[236] = ac_board[54];
s_printBuf[238] = ac_board[55];
s_printBuf[240] = ac_board[56];
s_printBuf[244] = ac_board[57];
s_printBuf[246] = ac_board[58];
s_printBuf[248] = ac_board[59];
s_printBuf[252] = ac_board[60];
s_printBuf[254] = ac_board[61];
s_printBuf[256] = ac_board[62];
s_printBuf[262] = ac_board[63];
s_printBuf[264] = ac_board[64];
s_printBuf[266] = ac_board[65];
s_printBuf[270] = ac_board[66];
s_printBuf[272] = ac_board[67];
s_printBuf[274] = ac_board[68];
s_printBuf[278] = ac_board[69];
s_printBuf[280] = ac_board[70];
s_printBuf[282] = ac_board[71];
s_printBuf[288] = ac_board[72];
s_printBuf[290] = ac_board[73];
s_printBuf[292] = ac_board[74];
s_printBuf[296] = ac_board[75];
s_printBuf[298] = ac_board[76];
s_printBuf[300] = ac_board[77];
s_printBuf[304] = ac_board[78];
s_printBuf[306] = ac_board[79];
s_printBuf[308] = ac_board[80];
::std::cout << s_printBuf;
}