efficient way to get string between the two words - arrays

I have a list of strings as:
strings= ['stackasdf:5;iwantthis123jasdoverflow','iwantthistoo','asdf:5;python123jasd']
Now, I want to print strings both in between two substrings(start=asdf:5; end=123jasd) and which are not.
output expected:
iwantthis
iwantthistoo
python
What I've done is:
import re
start = 'asdf:5;'
end = '123jasd'
for i in strings:
t=i[len(start):-len(end)]
if t:
print (t)
else:
print (i)
df:5;iwantthis123jasdo
iwantthistoo
python
I'm not getting the desired output as expected.

Using a regex approach we can try:
strings = ['stackasdf:5;iwantthis123jasdoverflow','iwantthistoo','asdf:5;python123jasd']
output = [re.findall(r'asdf:5;(.*?)123jasd', x)[0] if re.search(r'asdf:5(.*?)123jasd', x) else x for x in strings]
print(output) # ['iwantthis', 'iwantthistoo', 'python']

You can just use a simple for each loop and if statement to print out the correct substring of each element in the array:
strings= ['stackasdf:5;iwantthis123jasdoverflow','iwantthistoo','asdf:5;python123jasd']
for word in strings:
if "asdf:5;" in word and "123jasd" in word:
ind1 = word.index("asdf:5;")
ind2 = word.index("123jasd")
print(word[ind1+7:ind2])
else:
print(word)
output:
iwantthis
iwantthistoo
python

Use replace function & split?
strings = ['stackasdf:5;iwantthis123jasdoverflow',
'iwantthistoo',
'asdf:5;python123jasd']
for i in strings:
c = '\n' # c may be any character that is not present in your strings
i = i.replace('asdf:5;', c).replace('123jasd', c).split(c)
print(i[0] if len(i) == 1 else i[1])
print(f'{strings = }') # to show that I am not changing the original list.
Output:
iwantthis
iwantthistoo
python
strings = ['stackasdf:5;iwantthis123jasdoverflow', 'iwantthistoo', 'asdf:5;python123jasd']

Related

Why is mkString not working in Scala?

I can't get an array to print like a string normally in Scala
val a = Array("woot","yeah","ok then").sorted
for (i <- a.length-1 to 0 by -1)
println(s"$i: ${a(i)}")
val ab = ArrayBuffer(for (e <- a if e != null) yield e*3)
println(ab.mkString(" "))
For some reason, this prints:
2: yeah
1: woot
0: ok then
ArrayBuffer([Ljava.lang.String;#5034c75a)
And I was expecting it to print "yeahyeahyeah wootwootwoot ok thenok thenok then", that is, the items in the array (as strings) separated by a space. Why isn't it working and what am I doing wrong?
EDIT: ok, it was showing that because I was initializing ab to be a one-element ArrayBuffer with that array as the element instead of the elements of that inner array being separate elements of the array buffer.
If you want to print each element of the array, concatenated three times, with spaces between the entries, then it's just:
println((for (e <- a) yield e * 3).mkString(" "))
it gives:
ok thenok thenok then wootwootwoot yeahyeahyeah
(and this is the right order, because you wanted it sorted alphabetically, and o < w < y)
If you want to reverse the array before printing, you can initialize it to
val a = Array("woot","yeah","ok then").sorted.reverse
I think what you meant was
val ab = ArrayBuffer((for (e <- a if e != null) yield e*3): _*)
Some shorter answer using more functional approach:
val a = Array("woot","yeah","ok then").sorted.reverse
a.map(_ * 3).map(elem => print(elem + " ")
Edit:
If you want to have a result in some new variable you can do that:
val string = a.map(_ * 3).mkString(" ")

Ruby merging items in array conditionally

I have an array containing capital and small letters. I am trying to concatenate capital letters with the following small letters in a new array. For example, I have the following array
first_array = ["A","b","C","d","e"]
and I want to obtain the following array
["Ab","Cde"] #new array
I am trying to iterate through the first array with a code that looks like this:
new_array = []
first_array.each_with_index do |a,index|
if (a!~/^[a-z].*$/)
new_array = new_array.push "#{a}"
else
new_array[-1] = first_array[index-1] + "#{a}" #the idea is to concatenate the small letter with the previous capital letter and replace the last item in the new array
end
but it does not work. I am not sure I am tackling this issue efficiently which is why I can't resolve it. Could somebody suggest some options?
If you join as a string you can then scan to get all the matches:
first_array.join.scan(/[A-Z][a-z]*/)
=> ["Ab", "Cde"]
While I prefer #Paul's answer, you could do the following.
first_array.slice_before { |s| s.upcase == s }.map(&:join)
#=> ["Ab", "Cde"]
So, you want to split your original array when next char is uppercase, and then make strings of those subarrays? There's a method in standard lib that can help you here:
first_array = ["A","b","C","d","e"]
result = first_array.slice_when do |a, b|
a_lower = a.downcase == a
b_upper = b.upcase == b
a_lower && b_upper
end.map(&:join)
result # => ["Ab", "Cde"]
I like Sergio's answer a lot, here's what I brewed up while trying it out:
def append_if_present(lowercase, letters)
lowercase << letters.join if letters.size > 0
end
first_array = ["A","b","C","d","e"]
capitals = []
lowercase = []
letters = []
first_array.each_with_index do |l, i|
if l =~ /[A-Z]/
capitals << l
append_if_present(lowercase, letters)
letters = []
else
letters << l
end
end
append_if_present(lowercase, letters)
p capitals.zip(lowercase).map(&:join)
Here's a method that uses Enumerable#slice_when :
first_array.slice_when{ |a, b| b.upcase == b && a.downcase == a }.map(&:join)

Failing to use numeric characters within Ruby array as indices for a string

I am trying to use the numeric charecters from the array held within the positions argument as indices to access the characters of the string inside the string argument to subsequently print a new string. I have an idea of what I need to do to get it to work, but I am hung up.
Total code thus far:
def scramble_string(string, positions)
str = string
pos = positions.join
newstr = []
i = 0
while i < pos.length do
return newstr.push(str[pos[i]])
i += 1
end
end
scramble_string("hello", [2, 3, 4, 5])
I suspect my problem lies within this part of the code...
return newstr.push(str[pos[i]])
If I understand you, you can use the following to get a given substring of a string, using a range:
'this is a string'[5..8]
=> "is a"
A simple way would be:
str = 'this is a string'
positions = [2,3,6,9,10]
new_str = positions.map {|p| str[p]}.join
=> "iss s"
str = 'this is a string'
positions = [2,3,6,9,1]
str.split('').values_at(*positions).join
#=> "iss h"
Another way, one that does not use join:
positions.each_with_object('') { |i,s| s << str[i] }

Python Array JSON (dumps)

Google App Engine, Python27
I am trying to encode a python array of strings to a JSON string to send to the client. I created a working solution, which requires me to manually create a string version of the array and call dumps on it from python, but I don't believe it should be necessary:
def get_json(cls):
name_query = cls.query()
array_string = "["
index = 1
for name in name_query:
array_string += '"' + name.key.id() + '"'
if index < name_query.count():
array_string += ", "
index += 1
array_string += "]"
return json.dumps(array_string)
>> "[\"Billy\", \"Bob\"]"
For my second attempt, I tried calling "dumps" on a python array, which I believe should be a working solution; instead, I have to call dumps twice:
# (bad output)
def get_json(cls):
name_query = cls.query()
name_array = []
for name in name_query:
name_array.append(name.key.id())
return json.dumps(name_array)
>> ["Billy", "Bob"]
# (working output)
def get_json(cls):
name_query = cls.query()
name_array = []
for name in name_query:
name_array.append(name.key.id())
return json.dumps(json.dumps(name_array)))
>> "[\"Billy\", \"Bob\"]"
Even though I have a working solution, is there a better way and why does calling dumps on a python array not give the correct output?
The first function is working fine; it's outputting a valid JSON string. I think the confusion for you is that it's not surrounded in double-quotes, but that's just because you're printing the output of the function.
It's returning a string containing a JSON list, not a Python list object:
>>> def get_json():
... x = ["Billy", "Bob"]
... return json.dumps(x)
...
>>> print get_json()
["Billy", "Bob"]
>>> print repr(get_json())
'["Billy", "Bob"]' # It's a string, not a list
>>> type(get_json())
<type 'str'> # See? Type is str
When you call json.dumps twice, you're just wrapping the Python string returned by the first json.dumps call in double-quotes, and the double-quotes that were inside the array get escaped, because they're embedded in one big JSON string now, instead of being used as quotes around JSON strings inside a JSON array.
>>> print repr(json.dumps(get_json()))
'"[\\"Billy\\", \\"Bob\\"]"'

How to find a substring in an array of strings in matlab?

I have a string 'ADSL'. I want to find this string in an array of strings char('PSTN,ADSL','ADSL,VDSL','FTTH,VDSL')
when i run this command
strmatch('ADSL',char('PSTN,ADSL','ADSL,VDSL','FTTH,VDSL'));
the output is 2
But I expect the output as [1 2]
strmatch only gives positive result if the search string appears at the begining of row.
How can I find the search string if it occurs anywhere in the row?
Given the following input:
array = {'PSTN,ADSL', 'ADSL,VDSL', 'FTTH,VDSL'};
str = 'ADSL';
We find the starting position of each string match using:
>> pos = strfind(array, str)
pos =
[6] [1] []
or
>> pos = regexp(array, str)
pos =
[6] [1] []
We can then find the indices of matching strings using:
>> matches = find(~cellfun(#isempty,pos))
matches =
1 2
For an array of strings, it's better to use a cell array. That way strings can be of differnet lengths (and regexp can be applied on all cells at once):
cellArray = {'PSTN,ADSL','ADSL,VDSL','FTTH,VDSL'};
str = 'ADSL';
Then:
result = find(~cellfun('isempty', regexp(cellArray, str)));
will give what you want.
If you really have a char array as in your example,
array = char('PSTN,ADSL','ADSL,VDSL','FTTH,VDSL');
you can convert to a cell array (with cellstr) and apply the above:
result = find(~cellfun('isempty', regexp(cellstr(array), str)));
i would use strfind
a=strfind(cellstr(char('PSTN,ADSL','ADSL,VDSL','FTTH,VDSL')),'ADSL');
in this case will be a three by one cell array containing the index where you string starts at in the corresponding string

Resources