I want to write a line of code that checks if the iteration i==someValue and the next iteration i==anotherValue then write a line of code in the loop.
E.g
for line in file:
if line[1]=='A' and if next(line[1]) == 'B'
print("do something here")
How can I go about achieving this?
Thanks!
One solution is to store the value that you want for the first line as a control value. If the next line does not have the correct value - the control value is cleared. For example like:
file = ['A', 'A', 'B', 'C', 'A', 'C', 'B']
control_value = ''
for line in file:
if line == 'A':
control_value = line
continue
if line == 'B' and control_value == 'A':
print('Do something here')
control_value = ''
Any values except 'A' will reset the control_value.
Related
Firstly, Play JSON is not an option unfortunately.
I know how to remove & add elements in an ArrayBuffer, e.g. the below removes elements "b" & "c"...
val x = ArrayBuffer('a', 'b', 'c', 'd', 'e')
x -= ('b', 'c')
Giving
x = ArrayBuffer('a', 'd', 'e')
However, I have a JSON Array, something like below, where I want to remove "c" & "d" that exists in each dictionary within the list of dictionaries of "z".
ArrayBuffer({"x":1,"y":2,"z":[{"a":0.5,"b":"North","c":[{"c1":1,"c2":195.00,"c3":null},{"c1":2,"c2":229.00,"c3":null}],"d":{"d1":"N","d2":null}},{"a":0.5,"b":"North","c":[{"c1":1,"c2":195.00,"c3":null},{"c1":2,"c2":229.00,"c3":null}],"d":{"d1":"N","d2":null}},{"a":0.5,"b":"North","c":[{"c1":1,"c2":195.00,"c3":null},{"c1":2,"c2":229.00,"c3":null}],"d":{"d1":"N","d2":null}}]
Is this possible?
If so could you point me in the right direction - would be very grateful!
Thank you
I have been working on this problem on leetcode https://leetcode.com/problems/string-compression/
Given an array of characters, compress it in-place.
The length after compression must always be smaller than or equal to the original array.
Every element of the array should be a character (not int) of length 1.
After you are done modifying the input array in-place, return the new length of the array.
I almost have a solution, but I can't seem to count the last character in the string and I also am not sure how to make it so if there is only an amount of one of a character that I do not show 1 in the array.
I feel like I'm pretty close and I'd like to try and keep the solution that I have without altering it too much if possible.
This is what I have so far. chars is a list of characters
def compress(chars):
char = 0
curr = 0
count = 0
while curr < len(chars):
if chars[char] == chars[curr]:
count += 1
else:
# if count == 1:
# break
# else:
chars[char-1] = count
char = curr
count = 0
curr += 1
chars[char-1] += 1
return chars
print(compress(["a", "a", "b", "b", "c", "c", "c"]))
I wasn't quite able to format your code to get the answer you were seeking. Based on your answer, I was able to put together code and explanation that could help you:
def compress(chars):
count = 1
current_position = 0
# if it's a single character, just return a
# a basic array with count
if len(chars) == 1:
chars.append("1")
return chars
# loop till the 2nd last character is analyzed
while current_position < len(chars) - 1:
# assume that we haven't reached the 2nd last character
# if next character is the same as the current one, delete
# the current one and increase our count
while current_position < len(chars) - 1 and \
chars[current_position] == chars[current_position + 1]:
del chars[current_position]
count += 1
# if next character isn't the same, time to add the count to
# the list. Split the number into
# character list (e.g. 12 will become ["1", "2"]
# insert those numbers behind the character and increment position
for x in str(count):
chars.insert(current_position + 1, str(x))
current_position += 1
# great, on to the next character
current_position += 1
# if we are now at the last character, it's a lonely character
# give it a counter of 1 and exit the looping
if current_position == len(chars) - 1:
chars.append("1")
break
count = 1
return chars
mylist = ["a","b","b","b","b","b","b","b","b","b","b","b","b"]
print(compress(mylist))
Results
mylist = ["a","b","b","b","b","b","b","b","b","b","b","b","b"]
['a', '1', 'b', '1', '2']
mylist = ["a","a","a","a","a","a","a","a","a","a","b","b","b","b","b","b","b","b","b","b","b","b"]
['a', '1', '0', 'b', '1', '2']
mylist = ["a"]
['a', '1']
mylist = ["a","b"]
['a', '1', 'b', '1']
mylist = ["a","a","b","b","c","c","c"]
['a', '2', 'b', '2', 'c', '3']
Text input example:
"The exponent of [i]*[i]*[i]*0,5 is [i*i*i*0.5]"|%d,%f,%d,%e
Text output must be:
"The exponent of [%d]*[%f]*[%d]*0,5 is [%e]"|i,i,i,i*i*i*0.5
What I did:
I tried first to put them in two arrays
array_text ['i', 'i', 'i', 'i*i*i*0.5']
array_parameters ['%d', '%f', '%d', '%e']
But I don't know how to switch array_text[0] in the text with array_parameters[0],
array_text[1] in the text with array_parameters[1] and so on with the other array items.
How can I swap them in order to realize my text output as indicated above?
Edit:
I tried to resolve it like this:
let end = 0
for i in range(0,len(array_text)-1)
let idx = match(textstring, '\[\zs'.array_text[i], end)
let end = matchend(textstring, array_text[i], idx)
if end > idx
let text = substitute(text, strpart(text, idx, end-idx), array_parameters[i], '')
endif
endfor
That seems to work but the substitute/strpart command doesn't work.
I have a set of inputs and one output declared in a cell array like that:
A = {'a', 'f', 'c', 'b';
'b', 'f', 'c', 'a';
'a', 'f', 'b', 'c';
'c', 'f', 'b', 'a';
'c', 'f', 'a', 'b';
'b', 'f', 'a', 'c' }
where the first column is an output, and the rest are the inputs used, for each output.
I need to compare the values to reduce the calculation time.
So, the thing is, for equals outputs, I wanna know if the inputs are the same, a important remark.. the order of values desn't metter, so, when comparing f c b with f b c it is the same.
I need this because, acttualy, my data set is a 5040 x 7 cell array and I need to put them into a intorpolation function.
I thought in something like
if the value of the output column is equal to the another value of the same column, check if the value of inputs are all the same, using, ismember function.
But I can not arrive to a code that works.
Any help, please?
First, since you don't care about the order of the inputs, I would sort each of the rows:
[T, N] = size(A);
for t = 1:T
Asorted(t,1) = A(t,1);
Asorted(t,2:N) = sort(A(t,2:N));
end
Now you want to find all of the duplicate rows. A simple way to do this is first to convert to a character array, and use the unique function --
B = cell2mat(Asorted);
[C, ii, jj] = unique(B,'rows');
Now C contains the unique rows of B, ii contains the indexes of the unique rows, and jj labels each of the rows of B depending on which unique value it has.
If you wanted to filter out all of the duplicate rows from A, you can now do
Afiltered = A(ii, :);
This results in:
Afiltered =
'a' 'f' 'b' 'c'
'b' 'f' 'a' 'c'
'c' 'f' 'a' 'b'
I need to find only unique cell arrays within a set of cell arrays. For example, if this is my input:
I = {{'a' 'b' 'c' 'd' 'e'} ...
{'a' 'b' 'c'} ...
{'d' 'e'} ...
{'a' 'b' 'c' 'd' 'e'} ...
{'a' 'b' 'c' 'd' 'e'} ...
{'a' 'c' 'e'}};
Then I would want my output to look like this:
I_unique = {{'a' 'b' 'c' 'd' 'e'} ...
{'a' 'b' 'c'} ...
{'d' 'e'} ...
{'a' 'c' 'e'}};
Do you have any idea how to do this? The order of elements in the output doesn't matter, but efficiency does since the cell array I could be very large.
If your cells contain only sorted single characters then you can retain just the unique sequences using:
>> I = {{'a' 'b' 'c' 'd' 'e'} {'a' 'b' 'c'} {'d' 'e'} {'a' 'b' 'c' 'd' 'e'} {'a' 'b' 'c' 'd' 'e'} {'a' 'c' 'e'}};
>> I_unique = cellfun(#char, I, 'uniformoutput', 0);
>> I_unique = cellfun(#transpose, I_unique, 'uniformoutput', 0);
>> I_unique = unique(I_unique)
I_unique =
'abc' 'abcde' 'ace' 'de'
You can then split the resulting cells into single characters again:
>> I_unique = cellfun(#transpose, I_unique, 'uniformoutput', 0);
>> I_unique = cellfun(#cellstr, I_unique, 'uniformoutput', 0);
>> I_unique = cellfun(#transpose, I_unique, 'uniformoutput', 0);
>> I_unique{:}
ans =
'a' 'b' 'c'
ans =
'a' 'b' 'c' 'd' 'e'
ans =
'a' 'c' 'e'
ans =
'd' 'e'
EDIT: Updated to use a more efficient algorithm.
If efficiency is tantamount due to a large number of sets in I, then your best option is probably to roll your own optimized loops. This problem bears some similarity to a previous question about how to efficiently remove sets that are subsets of or equal to another. The difference here is that you are not concerned with removing subsets, just duplicates, so the code in my answer to the other question can be modified to further reduce the number of comparisons made.
First we can recognize that there's no point in comparing sets that have different numbers of elements, since they can't possibly match in that case. So, the first step is to count the number of strings in each set, then loop over each group of sets that have the same number of strings.
For each of these groups, we will have two nested loops: an outer loop over each set starting at the end of the sets, and an inner loop over every set preceding that one. If/When the first match is found, we can mark that set as "not unique" and break the inner loop to avoid extra comparisons. Starting the outer loop at the end of the sets gives us the added bonus that sets in I_unique will maintain the original order of appearance in I.
And here is the resulting code:
I = {{'a' 'b' 'c' 'd' 'e'} ... %# The sample cell array of cell arrays of
{'a' 'b' 'c'} ... %# strings from the question
{'d' 'e'} ...
{'a' 'b' 'c' 'd' 'e'} ...
{'a' 'b' 'c' 'd' 'e'} ...
{'a' 'c' 'e'}};
nSets = numel(I); %# The number of sets
nStrings = cellfun('prodofsize',I); %# The number of strings per set
uniqueIndex = true(1,nSets); %# A logical index of unique elements
for currentSize = unique(nStrings) %# Loop over each unique number of strings
subIndex = find(nStrings == currentSize); %# Get the subset of I with the
subSet = I(subIndex); %# given number of strings
for currentIndex = numel(subSet):-1:2 %# Outer loop
for compareIndex = 1:currentIndex-1 %# Inner loop
if isequal(subSet{currentIndex},subSet{compareIndex}) %# Check equality
uniqueIndex(subIndex(currentIndex)) = false; %# Mark as "not unique"
break %# Break the inner loop
end
end
end
end
I_unique = I(uniqueIndex); %# Get the unique values