Related
i have this list that contains an empty element:
list = ['Caramanico Terme', ' ', 'Castellafiume', ' ', 'Castelvecchio Subequo', ' ', 'Falesia di ovindoli', ' ', 'Fara San Martino', ' ', "L'Aquila - Madonna d'Appari", ' ', 'La Palma Pazza (Bisegna AQ)', ' ', 'Liscia Palazzo', ' ', 'Luco dei marsi', ' ', 'Montebello di Bertona', ' ', 'Monticchio', ' ', 'Palena', ' ', 'Pennadomo', ' ', 'Pennapiedimonte', ' ', 'Pescomarrino', ' ', 'Petrella', ' ', 'Pianezza', ' ', 'Pietrasecca', ' ', ' ', 'PietrePiane', ' ', 'Pizzi di Lettopalena (loc. Fonte della Noce)', ' ', 'Placche di Bini', ' ', 'Roccamorice', ' ', 'Sasso di Lucoli', ' ', 'Villetta Barrea', ' ']
how i can remove this '' empty element?
I have try in this way:
[x for x in list if all(x)]
but the elements are not delete
Any help?
Thanks
First of all. Make sure to not call your list list. That's a built-in type and will cause problems later. I renamed it to lst. Then you can filter the list the following way:
lst = ['Caramanico Terme', ' ', 'Castellafiume', ' ', 'Castelvecchio Subequo', ' ', 'Falesia di ovindoli', ' ', 'Fara San Martino', ' ', "L'Aquila - Madonna d'Appari", ' ', 'La Palma Pazza (Bisegna AQ)', ' ', 'Liscia Palazzo', ' ', 'Luco dei marsi', ' ', 'Montebello di Bertona', ' ', 'Monticchio', ' ', 'Palena', ' ', 'Pennadomo', ' ', 'Pennapiedimonte', ' ', 'Pescomarrino', ' ', 'Petrella', ' ', 'Pianezza', ' ', 'Pietrasecca', ' ', ' ', 'PietrePiane', ' ', 'Pizzi di Lettopalena (loc. Fonte della Noce)', ' ', 'Placche di Bini', ' ', 'Roccamorice', ' ', 'Sasso di Lucoli', ' ', 'Villetta Barrea', ' ']
filtered = [x for x in lst if len(x.strip()) > 0]
This will remove all kinds of whitepace elements like ' ' or ' ' etc.
EDIT:
As corn3lius pointed out, this would work too:
filtered = [x for x in lst if x.strip()]
You can add a condition in comprehension list:
l = ['Caramanico Terme', ' ', 'Castellafiume', ' ', 'Castelvecchio Subequo', ' ', 'Falesia di ovindoli', ' ', 'Fara San Martino', ' ', "L'Aquila - Madonna d'Appari", ' ', 'La Palma Pazza (Bisegna AQ)', ' ', 'Liscia Palazzo', ' ', 'Luco dei marsi', ' ', 'Montebello di Bertona', ' ', 'Monticchio', ' ', 'Palena', ' ', 'Pennadomo', ' ', 'Pennapiedimonte', ' ', 'Pescomarrino', ' ', 'Petrella', ' ', 'Pianezza', ' ', 'Pietrasecca', ' ', ' ', 'PietrePiane', ' ', 'Pizzi di Lettopalena (loc. Fonte della Noce)', ' ', 'Placche di Bini', ' ', 'Roccamorice', ' ', 'Sasso di Lucoli', ' ', 'Villetta Barrea', ' ']
print([l for l in list if l != ' '])
Removing all items that not is ' ' i.e. the empty string is the same thing as building a set with all elements from the first set that has length > 0. This one liner takes care of that:
a = ['', 'apple', '', 'peach']
b = [i for i in a if i != '']
Removing empty items from list. Here empty items might be in single space or multiple space within quotes. So, use strip() function in list comprehension.
Ex:
temp_str = ' || 0X0C || 0X00000 || 0X00094 || 0X00E8C || IN_OPER || 000000e8cff7e000 || '
temp_str.split('||')
# result: [' ', ' 0X0C ', ' 0X00000 ', ' 0X00094 ', ' 0X00E8C ', ' IN_OPER ', ' 000000e8cff7e000 ', ' ']
temp_list = [ x for x in temp_str.split('||') if x]
temp_list
# result: [' ', ' 0X0C ', ' 0X00000 ', ' 0X00094 ', ' 0X00E8C ', ' IN_OPER ', ' 000000e8cff7e000 ', ' ']
temp_list = [ x for x in temp_str.split('||') if x.strip()]
temp_list
# result: [' 0X0C ', ' 0X00000 ', ' 0X00094 ', ' 0X00E8C ', ' IN_OPER ', ' 000000e8cff7e000 ']
temp_list = [ x.strip() for x in temp_str.split('||') if x.strip()]
temp_list
# result: ['0X0C', '0X00000', '0X00094', '0X00E8C', 'IN_OPER', '000000e8cff7e000']
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
# ###
# # #
# # #
# #
# ###
my python dict looks like
{'1, ': (' name', '10G')}
{'2, ': (' name', '10G')}
{'3, ': (' name2', '40G')}
{'4, ': (' name2', '40G')}
Keys are 1 to 4
and values are
name* , *G
result I want to get using python :
no of 10G entries = 2 and
no of 40G entries = 2
What will be the python code?
You can simply use Counter
>>> a = {
'1, ': (' name', '10G'),
'2, ': (' name', '10G'),
'3, ': (' name2', '40G'),
'4, ': (' name2', '40G')
}
>>> from collections import Counter
>>> c = Counter(a.values())
>>> c
Counter({(' name2', '40G'): 2, (' name', '10G'): 2})
>>> list(c.iteritems())
[((' name2', '40G'), 2), ((' name', '10G'), 2)]
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;
}
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