concatenate/join list of numbers to get a string - j

I want to join a list of integers in their order to obtain a final string, e.g.
a=.11 22 33 44
And I want to get '11223344'. And also, sometimes, I need to put a separator in between '11 22 33 44' like a space in this case. What is the simplest way?

Use format ": to covnert the list of numbers to a string:
":a
11 22 33 44
There are many ways to remove the space between them or to separate them with another string.
' ' -.~ ":a NB. remove spaces (' ') from ":a
11223344
(":a) rplc ' ';', ' NB. replace spaces with ', ' (or anything else, even empty)
11, 22, 33, 44
,('-',~":)"0 a NB. append '-' to every element of a; convert to string
11-22-33-44-
etc

Another solution that can make it easier to deal with some situations.
Use the foreign 8!:0 to convert the numbers to boxed strings (this handles conversion of negative signs from _ to -).
8!:0 a
┌──┬──┬──┬──┐
│11│22│33│44│
└──┴──┴──┴──┘
Use the joinstring verb from the strings script/addon to join the boxed strings as required. For versions prior to J7 you will need to require 'strings' to ensure that the joinstring verb is available.
' ' joinstring 8!:0 a
11 22 33 44
'' joinstring 8!:0 a
11223344
'","' joinstring 8!:0 a
11","22","33","44

Related

How do i get the values in different rows in excel using formuale?

I have a string of numbers which are spaced randomly in a single cell like this -:
"8 29 30 36 32 16 16 33 36 30 1 32 1 2 0 4 21 11 35 17 419 47 14 13 30 36 35 23 27 14 35 20 36 28 44 16 4 4 32 22 0 28 16 27 27 27 30 12 10 34 8 17 19 1 24 17 8"
I want this to put individual numbers in different rows starting from 1 to n (total numbers) using a generalized formuale but not able to write the formulae. Could anyone help me please?
Any help would really be appreciated as I am not able to do this!
Thanks and Regards,
There is no need to use VBA at all, the following formula will work.
Assuming the string you mention is in A1, put the following formula into E4:
=FILTERXML("<x><y>"&SUBSTITUTE(SUBSTITUTE(A1," "," ")," ","</y><y>")&"</y></x>","//y")
It's a bit of a hack, but it works. There are two elements. Firstly, I notice that you have some numbers separated by double-spaces and I assume that's just padding. Those are removed using the SUBSTITUTE(A1," "," "). Secondly, the result is then altered, using SUBSTITUTE - changing each space into XML tags which are then parsed using FILTERXML into an array.
As Ed points out, you can use Excel's TRIM function which removes multiple spaces more robustly and elegantly:
=FILTERXML("<x><y>"&SUBSTITUTE(TRIM(A1)," ","</y><y>")&"</y></x>","//y")
If you're using Office365, then you could use the purpose-built TEXTSPLIT function instead.
You can do this by selecting the cell with the numbers, navigating to the Data tab, and selecting "text to columns". Select "Delimited" in the window that pops up and choose next. then select "spaces". that should work!
Alternative to FILTERXML presented by CLR
Lets assume your string of numbers is sitting in cell D2
Standardize the spacing
Use the TRIM function to eliminate all excess spacing.
TRIM($D$2)
Add a starting and ending space
Make is so all the numbers are the same in the sense that they all have a single space before and after them. Regular patterns are nicer to deal with as you do not need to make special cases. ie first number or last number in the list. Note this is done after the TRIM function performed.
=" "&TRIM($D$2)&" "
Add start and end identifiers
While potentially not necessarily required, it may make life easier. Lets use S for start and F for finish. This can be done with the SUBSTITUTE function. The last optional entry of the substitute function allows you to declare which instance of the search item you which to replace. This will be convenient for us as we can use a counter to jump are way along the list to the appropriate spaces. The formula for just adding the S looks like
=SUBSTITUTE(" "&TRIM($D$2)&" "," ","S",ROW(A1))
ROW(A1) is our counter so in this case the first space will be replaced by S. Now to put in the F we want to put it in the next space after that S substitution as been made. Since there is now one less space in the text, the same occurrence number gets used. The formula becomes:
=SUBSTITUTE(SUBSTITUTE(" "&TRIM($D$2)&" "," ","S",ROW(A1))," ","F",ROW(A1))
Determine number length
This can be done by finding the position of F and S in the string and taking the difference of there position and subtracting 1. The formula for doing this is:
=FIND("F",SUBSTITUTE(SUBSTITUTE(" "&TRIM($D$2)&" "," ","S",ROW(A1))," ","F",ROW(A1)))-FIND("S",SUBSTITUTE(SUBSTITUTE(" "&TRIM($D$2)&" "," ","S",ROW(A1))," ","F",ROW(A1)))-1
Pull the number
Since we now know where the number start due to the S and we know how many digits there are, you can use the MID function to pull the number as a string. The formula will now look like:
=MID(SUBSTITUTE(SUBSTITUTE(" "&TRIM($D$2)&" "," ","S",ROW(A1))," ","D",ROW(A1)),FIND("S",SUBSTITUTE(SUBSTITUTE(" "&TRIM($D$2)&" "," ","S",ROW(A1))," ","F",ROW(A1)))+1,FIND("F",SUBSTITUTE(SUBSTITUTE(" "&TRIM($D$2)&" "," ","S",ROW(A1))," ","F",ROW(A1)))-FIND("S",SUBSTITUTE(SUBSTITUTE(" "&TRIM($D$2)&" "," ","S",ROW(A1))," ","F",ROW(A1)))-1)
End of list
You can copy the last formula down as far as you like and it will generate a list. However when you copy it down further than the number of numbers you have it will start to through #VALUE! error. If you do not want to see this and would prefer a blank cell, then you can wrap the above formula in an IFERROR function that can generate "", 0, or "Custom Message".
=IFERROR(MID(SUBSTITUTE(SUBSTITUTE(" "&TRIM($D$2)&" "," ","S",ROW(A1))," ","D",ROW(A1)),FIND("S",SUBSTITUTE(SUBSTITUTE(" "&TRIM($D$2)&" "," ","S",ROW(A1))," ","F",ROW(A1)))+1,FIND("F",SUBSTITUTE(SUBSTITUTE(" "&TRIM($D$2)&" "," ","S",ROW(A1))," ","F",ROW(A1)))-FIND("S",SUBSTITUTE(SUBSTITUTE(" "&TRIM($D$2)&" "," ","S",ROW(A1))," ","F",ROW(A1)))-1),"")
Text or Number
As it currently stands, the formula above is all string manipulation so your output will be digits stored as text or a string. If you want to have the digits as a number instead of text then you either have to wrap the MID function in the VALUE function or send the resulting text from the MID function through a math operation that will not change its value. (ie. *1, /1, +0, -0) Alternatively you can simply place a double minus (--) in from of the MID function. The alternative approach is shown below:
=IFERROR(--MID(SUBSTITUTE(SUBSTITUTE(" "&TRIM($D$2)&" "," ","S",ROW(A1))," ","D",ROW(A1)),FIND("S",SUBSTITUTE(SUBSTITUTE(" "&TRIM($D$2)&" "," ","S",ROW(A1))," ","F",ROW(A1)))+1,FIND("F",SUBSTITUTE(SUBSTITUTE(" "&TRIM($D$2)&" "," ","S",ROW(A1))," ","F",ROW(A1)))-FIND("S",SUBSTITUTE(SUBSTITUTE(" "&TRIM($D$2)&" "," ","S",ROW(A1))," ","F",ROW(A1)))-1),"")

python - remove space in the end of range loop (int)

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=",")

A function "join" and "split" must show word [5:6] and [10]

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

Trying to remove only the characters F or C following numbers

I'm trying to find a regex (that will work in node.js) to remove the Fahrenheit and Celsius letters and replace them with " degrees" from the below weather forecast string.
"Clear skies. Low 46F. NNW winds shifting to ENE at 10 to 15 mph."
I want the above string to read as below:
"Clear skies. Low 46 degrees. NNW winds shifting to ENE at 10 to 15 mph."
There could be more than one instance of a temperature in the string.
NOTE: I only want to remove the F or C if it's immediately following a number with no space in-between. If "Florida" were in the above string, I'd want the letter "F" left untouched.
I've tried the below regex, but it finds the entire 46F. I just want it changed to 46 degrees.
/\d+[FC]/g
Thanks.
That is because the lack of the capturing group (parentheses):
Use this:
/(\d+)[FC]/g
$1 means the first capturing group. The \d+ in this case.
speechOutput = speechOutput.replace(/(\d+)[FC]/g, '$1 degrees');

read long txt file into r without delimiter and identify 17 char string where char 16 and 17 are identical to 14 and 15

I have a txt file approx 40k long. there are no delimiters or structure to the txt file. I need to identify a text string within this long string 15 characters long, but I know that char 16 and 17 equals char 14 and 15 respectively. Any idea how whis can be done using r?
If i understand correctly, here's a sample string.
a<-"aaabbbcccddddfffeee"
And let's say you want to extract a 6 character string where the 5th and 6th characters match the next two characters after the string. You can find that with a regular expression like
regmatches(a, regexpr("(.{4}(.{2}))\\2", a))
However that does also grab the two extra character after the match. You can trim off the the last two characters, or you could use captured matches. I've written a helper function call regcapturedmatches which makes this a bit easier. You could do
regcapturedmatches(a, regexpr("(.{4}(.{2}))\\2", a, perl=T))[[1]][,1]
to get just the match.

Resources