Its quite simple but I got stuck. I have two files who need to be identical(even spaces)
file #1 is the output from :
for i in range(0, 19):
print(i)
and the other is the same just without space after the 18
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
how do I remove the space after the 18 in order to get identical files?? ( only by changing file#1)
thank you!
the example
You can call print with newline as the separator and an empty string as the end marker instead:
print(*range(19), sep='\n', end='')
Try building the whole string first and then printing it:
m = " \n".join(map(str, range(19)))
print(m)
I recommend you use join because you can specify a character that will only be inserted between the letters (in this case the separator is " \n") and not after the last one.
The answer can be “0,1,2,3,4,5,6,7,8,9.....18”, it should also include “try this end = " ," ”.
print(i, end=",")
Related
I have the following join in my code
(' '.join(s.split()[10:14])
But i also want to print word [16], i have tried "and", "+" etc. but no luck
Hope somebody can help me :-)
You can use multiple slice objects with the operator.itemgetter method, and use itertools.chain.from_iterable to join the slices, so that you would not have to split the same string twice or store the result of the split in a temporary variable:
from operator import itemgetter
from itertools import chain
print(' '.join(chain.from_iterable(itemgetter(slice(10, 15), slice(16, 17))(s.split()))))
Here's one way to do that:
>>> s = 'But i also want to print word [16], i have tried "and", "+" etc. but no luck Hope somebody can help me :-)'
>>> a = s.split()
>>> print(' '.join([*a[10:14],a[16]]))
tried "and", "+" etc. luck
You can do it by joining both strings after extracting them individually.
Sample string:
s = '1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19'
' '.join(s.split()[10:14]) + ' ' + s.split()[15]
Also, as pointed out by #blhsing , the second join might not be needed here and only the 16th element of split can be added as a string. Also added a whitespace between the two strings.
Output:
Out[19]: '11 12 13 14 16
I have an input of one string(get it with std::getline(cin, line) where line is std:string line)
8 9 + 12 43 - *
And I need to get numbers and symbols from this string.
8
9
+
12
43
-
*
I'm sorry for such a trivial question. I wrote a lot of bad code and want to know a simple way to do it.
Thank you.
A string can be separated by whitespace using splitting.
Check this out:
Split a string in C++?
I prompt the user to input a variable 'year'.
I need to get the first 2 digits, and the last 2 digits of the 4-digit variable year for the Gaussian algorithm and store them in two separate variables.
What I need help on is getting the first 2 and last 2 numbers from 'year'.
Try this:
local n = 1234
local first = math.floor(n / 100)
local last = n % 100
print(first, last)
which print:
12 34
To add to Bart's answer, if you're keeping it in string form you can just use sub to grab the parts you want:
year = '1969'
firsttwo = year:sub(1,2)
secondtwo = year:sub(3,4)
I'm in a basic MATLAB college course, and need some help with my code.
theres an external .txt file with names in it, with corresponding numbers assigned to each name. my goal is to place all the first names, last names, and numbers into arrays, find the lowest number in the 'number' array, get the corresponding indexer number, and print the first and last name related with that number.
the text file reads 25 different names and numbers
(i.e.:
Bob
Smith
17
Jane
Doe
23
Bill
Johnson
13
...etc...)
here is my general code so far:
1 clear
2
3 clc
4
5
6 fid1=fopen('facedata.txt','rt');
7
8 for index = 1:1:25
9 firstn(index) = fgetl(fid1);
10 lastn(index) = fgetl(fid1);
11 number(index) = fscanf(fid1,'%f');
12 end
13
14 [distmin,I] = min(dist);
15 fprintf('%5.4f %10s %10.0f', distmin, firstn(I), I);
My hope is for the code to run through, get matlab to recognize '13' as the lowest number, and print 'bill johnson' to the screen, but if I run the code, matlab says there are errors
Subscripted assignment dimension mismatch.' # line 9.
and
Index exceeds matrix dimensions.' # the firstn**(I)** in line 15.
any ideas?? i know this is crazy long, but any help would be appreciated! :]
The command fgetl means read a line from the text file. Therefore your code is reading 2x25 = 50 lines of text. How do you know that your file has this many lines in it? You should read a new line, process it, and repeat until you reach the end of the file:
fid = fopen('fgetl.m');
tline = fgetl(fid);
while ischar(tline)
disp(tline)
tline = fgetl(fid);
end
fclose(fid);
However, this would not do what you want. You should rather use fscanf to read data in the format you want. You want to read two consecutive strings (first name, last name) and an integer number. So you can use
A = fscanf(fid, '%s %s %d', [3 inf]);
to read three items at a time and repeat until the end of the file.
I answered my own question earlier today, but here's what I found if anyone is interested:
you have to index a line of string by using curly brackets instead of straight ones.
i.e.:
for index = 1:1:25
firstname{index} = fgetl(fid1);
end
fprintf('%10s', firstn{index});
fprintf will print whichever number index is supplied.
thanks anyway kavka :]
I would like to read a .txt file into Matlab.
One of the columns contains both letters and numbers.
(So I guess one way is to read this column is as string.)
The problem is I also need to find out numbers which are larger than 5 within that column.
e.g. The .txt looks like
12 1
21 2
32 7
11 a
03 b
22 4
13 5
31 6
i.e. Ultimately, I would like to get
32 7
31 6
How can I get it?? Any experts, please help!
You can read the contents of the file into a cell array of strings using TEXTSCAN, convert the strings to numeric values using CELLFUN and STR2NUM (characters like 'a' and 'b' will result in the empty matrix []), remove rows of the cell array that have any empty cells in them, then convert the remaining data into an N-by-2 matrix using CELL2MAT:
fid = fopen('junk.txt','r'); %# Open the file
data = textscan(fid,'%s %s','CollectOutput',true); %# Read the data as strings
fclose(fid); %# Close the file
data = cellfun(#str2num,data{1},'UniformOutput',false); %# Convert to numbers
data(any(cellfun('isempty',data),2),:) = []; %# Remove empty cells
data = cell2mat(data); %# Convert to N-by-2 array
The matrix data will now look like this, given your sample file in the question:
>> data
data =
12 1
21 2
32 7
22 4
13 5
31 6
And you can get the rows that have a value greater than 5 in the second column like so:
>> data(data(:,2) > 5,:)
ans =
32 7
31 6
fid = fopen('txt.txt','r');
Aout = [];
while(1)
[a1,count1] = fscanf(fid,'%s',1);
[a2,count2] = fscanf(fid,'%s',1);
if(count1 < 1 | count2 < 1)
break;
end
if(~isempty(str2num(a2)) & str2num(a2) > 5 & (~isempty(str2num(a1))) )
Aout = [ Aout ; str2num(a1) str2num(a2) ];
end
end
fclose(fid);
Violates the unspoken rule of growing a Matlab variable during a loop, but it's text processing anyway so you probably won't notice the slowness.
Edit: Had too many errors in previous version, had to start fresh.