Related
I am a beginner in python.
The second loop only run for once, the first time only, but when the turn comes to the first loop and when e = e+1 - python skips the second loop!
Why?
The print order only work for once.
items = [['.', '.', '.', '.', '.', '.'],
['.', 'O', 'O', '.', '.', '.'],
['O', 'O', 'O', 'O', '.', '.'],
['O', 'O', 'O', 'O', 'O', '.'],
['.', 'O', 'O', 'O', 'O', 'O'],
['O', 'O', 'O', 'O', 'O', '.'],
['O', 'O', 'O', 'O', '.', '.'],
['.', 'O', 'O', '.', '.', '.'],
['.', '.', '.', '.', '.', '.']]
i=0
e=0
while e < 6 :
while i < 9 : #python run this loop only once, and never come back when e=e+1
print items[i][e]
i=i+1
e=e+1
After the 'i' loop runs once, i will be set to 9 and will stay as 9 until you reset.
so you can try to set it to 0 after e = e+1.
A useful technique you can try is also printing the values of 'e' and 'i' to see where the loops gone wrong
items = [['.', '.', '.', '.', '.', '.'],
['.', 'O', 'O', '.', '.', '.'],
['O', 'O', 'O', 'O', '.', '.'],
['O', 'O', 'O', 'O', 'O', '.'],
['.', 'O', 'O', 'O', 'O', 'O'],
['O', 'O', 'O', 'O', 'O', '.'],
['O', 'O', 'O', 'O', '.', '.'],
['.', 'O', 'O', '.', '.', '.'],
['.', '.', '.', '.', '.', '.']]
i=0
e=0
while e <6 :
while i <9 :
print items[i][e]
print 'loop: i'+str(i)+'e'+str(e)
i=i+1
e=e+1
i=0
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'm trying to read a sudoku and put it on a list,
i have something like this.
0,0,0,0,7,0,2,6,0
0,6,0,8,0,2,0,3,5
0,0,5,3,0,0,0,7,0
0,7,6,0,0,0,0,2,0
0,8,9,6,0,0,0,4,0
0,3,0,5,4,0,0,8,0
0,0,0,2,8,0,0,0,0
0,2,0,4,0,0,0,0,3
0,0,8,7,0,3,6,0,0
i need convert it on a list like this
board = [['0', '0', '0', '0', '7', '0', '2', '6', '0'], ['0', '6', '0', '8',
'0', '2', '0', '3', '5'], ['0', '0', '5', '3', '0', '0', '0', '7', '0'],
['0','7', '6', '0', '0', '0', '0', '2', '0'], ['0', '8', '9', '6', '0',
'0', '0','4', '0'], ['0', '3', '0', '5', '4', '0', '0', '8', '0'],
['0', '0', '0', '2','8', '0', '0', '0', '0'], ['0', '2', '0', '4', '0',
'0', '0', '0', '3'], ['0','0', '8', '7', '0', '3', '6', '0', '0']]
I'm using this code but have a problem
tablero = open('sd1.txt', 'r')
board = [line.split(',') for line in tablero.readlines()]
The result is:
board = [['0', '0', '0', '0', '7', '0', '2', '6', '0\n'], ['0', '6', '0',
'8', '0', '2', '0', '3', '5\n'], ['0', '0', '5', '3', '0', '0', '0', '7',
'0\n'], ['0', '7', '6', '0', '0', '0', '0', '2', '0\n'], ['0', '8', '9',
'6', '0', '0', '0', '4', '0\n'], ['0', '3', '0', '5', '4', '0', '0', '8',
'0\n'], ['0', '0', '0', '2', '8', '0', '0', '0', '0\n'], ['0', '2', '0',
'4', '0', '0', '0', '0', '3\n'], ['0', '0', '8', '7', '0', '3', '6', '0',
'0\n']]
Use .strip() to remove leading and trailing whitespace (including the trailing newline that is causing your trouble):
board = [line.strip().split(',') for line in tablero.readlines()]
in case you have the problem at the end of line, you can do a right strip as same Jez but only on the right part..basically..it does the same but only the right of the string .
board = [line.rstrip().split(',') for line in tablero.readlines()]
I guess you need to remove the '\n' by using line.strip('\n\r').
Or you could also use line[:-1].split(','), which also removes the last newline character.
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
# ###
# # #
# # #
# #
# ###
char initialMaze[ SIZEY+1][ SIZEX+1] //local array to store the maze structure
= { {'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X'},
{'X', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
{'X', '#', '#', '#', '#', '#', ' ', ' ', ' ', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
{'X', '#', '#', '#', '#', '#', ' ', ' ', ' ', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
{'X', '#', '#', '#', '#', '#', ' ', ' ', ' ', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
{'X', '#', '#', '#', ' ', ' ', ' ', ' ', ' ', ' ', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
{'X', '#', '#', '#', ' ', '#', ' ', '#', '#', ' ', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
{'X', '#', ' ', ' ', ' ', '#', ' ', '#', '#', ' ', '#', '#', '#', '#', '#', ' ', ' ', '+', '+', '#'},
{'X', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '+', '+', '#'},
{'X', '#', '#', '#', '#', '#', ' ', '#', '#', '#', ' ', '#', ' ', '#', '#', ' ', ' ', '+', '+', '#'},
{'X', '#', '#', '#', '#', '#', ' ', ' ', ' ', ' ', ' ', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
{'X', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'}, };
Hi,
I'm trying to create some code that will load this maze layout from a .txt file and put it into a 2d array. Currently it just puts the maze layout straight into the 2d array without having it stored in a .txt file.
Does anyone know how I will do this?
Step 1: Create a text file which looks like:
XXXXXXXXXXXXXXXXXXXX
X###################
X##### ###########
X##### ###########
X##### ###########
X### ##########
X### # ## ##########
X# # ## ##### ++#
X# ++#
X##### ### # ## ++#
X##### #########
X###################
Step 2: write the code for reading the maze text file
char initialMaze[SIZEY+1][SIZEX+1];
int row = 0;
ifstream fstrm("filename.txt");
while(fstrm.getline(initialMaze[row], SIZEX+1)) {
++row;
}
If the text representing the maze is in a text file called maze.txt,
Then the following code might be sufficient,
char initialMaze[SIZEY+1][ SIZEX+1];
string temp;
int i=0;
ifstream var("maze.txt");
if (myfile.is_open())
{
while(getline(var,temp) )
{
strcpy(initialMaze[i],temp.c_str());
i++;
}
myfile.close();
}
maze.txt:
XXXXXXXXXXXXXXXXXXXX
X###################
X##### ###########
X##### ###########
X##### ###########
X### ##########
X### # ## ##########
X# # ## ##### ++#
X# ++#
X##### ### # ## ++#
X##### #########
X###################
maze.cpp:
#include <fstream>
#include <string>
#include <vector>
#include <cstdlib>
int main() {
std::ifstream fin("maze.txt");
if (!fin) return EXIT_FAILURE;
std::vector<std::string> maze;
std::string line;
while (std::getline(fin, line)) {
maze.push_back(line);
}
}
This gets the data into maze, where you can access any cell using maze[row][column]. The maze can be practically any size, and rows don't even have to all be the same length. (Just be sure when you access an element that the row and column are inside the maze:
if (0 <= row && row < maze.size() && 0 <= column && column < maze[row].size()) {
maze[row][column] ...
}
text.txt
1880 1 0 67.50 10.50 -1.00 -1.00
1880 1 4 66.50 11.50 -1.00 -1.00
1880 1 8 66.50 11.50 -1.00 -1.00
1880 1 12 65.50 11.50 -1.00 -1.00
1880 1 16 64.50 11.50 -1.00 -1.00
1880 1 20 63.50 12.50 -1.00 -1.00
1880 2 0 63.50 12.50 -1.00 -1.00
1880 2 4 62.50 12.50 -1.00 -1.00
1880 2 8 62.50 12.50 -1.00 -1.00
text.cpp
#include <vector>
#include <string>
#include <sstream>
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
vector<vector<double> > data;
ifstream file("D:\\test.txt");// file path
string line;
while (getline(file, line))
{
data.push_back(vector<double>());
istringstream ss(line);
double value;
while (ss >> value)
{
data.back().push_back(value);
}
}
for (int y = 0; y < data.size(); y++)
{
for (int x = 0; x < data[y].size(); x++)
{
cout<<data[y][x]<< " ";
}
cout << endl;
}
return 0;
}
run result:
1880 1 0 67.5 10.5 -1 -1
1880 1 4 66.5 11.5 -1 -1
1880 1 8 66.5 11.5 -1 -1
1880 1 12 65.5 11.5 -1 -1
1880 1 16 64.5 11.5 -1 -1
1880 1 20 63.5 12.5 -1 -1
1880 2 0 63.5 12.5 -1 -1
1880 2 4 62.5 12.5 -1 -1
1880 2 8 62.5 12.5 -1 -1
Press any key to continue