So I have an array with some daily price variations (VarP), and a initial price value (p_0 = 500), and I want to get a new array with prices according to these variations.
For example:
p_0 = 500
p_1 = p_0*VarP[0]
p_2 = p_1*VarP[1]
.
.
.
p_n = p_(n-1)*VarP[n-1]
How can I do this with a function in Python?
Please try this code
newArr = [None] * len(VarP);
newArr[0] = 500;
for i in range(1, len(VarP)):
newArr[i] = VarP[i-1] * newArr[i-1]
text = "hellovision hey creator yoyo b creator great publisher"
I want to extract creator's name and publisher's name from text.
The result will be,
creator = hellovision hey, yoyo
publisher = great
How can I get text using regular expression?
Do I need to use span()..?
This is my code.
def preprocess2(text):
text_list = test.split(' ')
lyricist = []
composer = []
music_arranger = []
temp = []
lyricist.clear()
composer.clear()
music_arranger.clear()
for i in range(0, len(text_list)):
if text_list[i] == 'creator':
print(len(text_list))
for a in range(0, i-1):
temp.append(text_list[a])
lyricist.append(''.join(temp))
temp.clear()
for b in range(0, i+1):
print(b)
text_list.pop(b)
print(len(text_list))
break
elif text_list[i] == 'pulisher':
for a in range(0, i-1):
temp.append(text_list[a])
composer.append(''.join(temp))
temp.clear()
for b in range(0, i+1):
text_list.pop(b)
break
i = i +1
return text_list
If you split your array using regex with a capture group, the value that you split on will also be passed into the output array.
You can then loop through looking for 'creator' or 'publisher' and in each case, pass the previous entry into the proper collection.
const text = "hellovision hey creator yoyo b creator great publisher"
const splitArr = text.split(/(creator|publisher)/)
const creators = [], publishers = []
let i = -1, len = splitArr.length
while(++i < len){
if(splitArr[i] == "creator") creators.push(splitArr[i-1].trim())
else if(splitArr[i] == "publisher") publishers.push(splitArr[i-1].trim())
}
console.log("creators: ", creators)
console.log("publishers: ", publishers)
I’m using Rails 4.2.3. I’m having trouble passing a variable number of search criteria to my Rails finder method. I have
user = current_user
search_criteria = ["my_objects.user_id = ?"]
search_values = [user.id]
start_date = params[:start_date]
if start_date.present?
start_date_obj = Date.strptime(start_date, "%m/%d/%Y")
search_criteria.push("my_objects.start_date >= ?")
search_values.push(start_date_obj)
else
start_date = my_object.find_user_first_my_object_date(user)
#default_start_date = start_date.to_time.strftime("%m/%d/%Y") if start_date.present?
end
end_date = params[:end_date]
if end_date.present?
end_date_obj = Date.strptime(end_date, "%m/%d/%Y")
search_criteria.push("my_objects.end_date <= ?")
search_values.push(end_date_obj)
else
end_date = my_object.find_user_last_my_object_date(user)
#default_end_date = end_date.to_time.strftime("%m/%d/%Y") if end_date.present?
end
distance = params[:distance]
if distance.present?
distance_parts = distance.split(" ")
search_criteria.push("my_objects.distance = ?")
search_criteria.push("my_objects.distance_unit_id = ?")
search_values.push("#{distance_parts[0]}")
search_values.push("#{distance_parts[1]}")
end
#user_my_objects = MyObjectTime.joins(:my_object).where(search_criteria.join(" AND "), search_values)
.order("my_objects.day")
But this results in the error
wrong number of bind variables (1 for 5) in: my_objects.user_id = ? AND my_objects.start_date >= ? AND my_objects.end_date <= ? AND my_objects.distance = ? AND my_objects.distance_unit_id = ?
I think Rails is treating my “search_values” array as a single value but I want it to pass each value of the array as an argument into the search condition. How do I fix the above?
If I've read the question right, this boils down to
values = ['a', 'b', 'c']
SomeModel.where("foo = ? AND bar = ? AND baz = ?", values)
Throwing an error about an incorrect number of bind variables. To unpack an array and use its values as individual arguments you use the splat operator:
SomeModel.where("foo = ? AND bar = ? AND baz = ?", *values)
I have been asked to take a barcode symbol sequence, and replace each number substring with the actual number representation. In other words, a number '1' in the barcode sequence is represented by '...::'. Each number [0-9] is represented by a 5 character symbol. Each barcode is made up of 10 digits, so I want to create a number string that is equivalent to the symbolic barcode string.
ex.-
'...:::.:.....::::....:..::.:..:.:..:.:..:..:.::...' would be equivalent to 1910499980
What I am trying to accomplish is to capture each 5 character sequence, replace it with its numeric equivalent, then move on to the next 5 character sequence. The code I have so far is below, but it does not function correctly, it replaces some of the symbols, but not all. It also substitutes some of the numbers incorrectly. I forgot to mention that the first character of each line is to be omitted, so a portion of my code has already taken care of that.
Any help you can provide would be great,
Thanks,
Dan
barcodes = {':...:::.:.....::::....:..::.:..:.:..:.:..:..:.::...';
':::...:..:...::...::.::.....:.:.:..:..::...:.:..:.:';
'::.:...:..:.::..::...:...:::.....::...::.::...:..:.';
'::...:.:..:::...:...::..:...:.:..::.:..:..:..::...:';
':::....::...:.:...:.:::...:.:..:.:..::...::...:.:..';
':.:..:..::...:.:...::::......:::...:.:..:.::..:...:';
'::.:..:..:....::..:.:..:.:.:..:..::..:.:.:..:.:..:.';
':.::.....:::...:.::.....::..::..:..:.:..:.:..:.:..:';
'::...::..:..::...::...::..:...:.:.:.:...:.:.:...::.';
'::..:...::...:.:::......:::..:...::..:..:..:.::.:..';
'::..:...:.:::...:...:...::.:..:.:..:.:..:.:..:.::..';
':.:..::..:.:..:...:.:.:..::.:..:.:..:.:..:..:.:.:..';
':..::...:.:..::.::....::....::..:..:..:.:.:.:...:.:';
':..::..::.....::::....:..:.:.:.:.:..:..:...::....::';
':.::...::...:.:.::....::....:.:::.....:.:..::.:...:';
'::..:..:..:..::...:.:..:.:.:..:..::.:.:...:..:...::';
':.:.:.:...:::...::...:...:..::..:..:..:.:.:..::..:.';
':...::..:.:..:.:.:..:.::....::.:..:.:..:.:..:.:..:.';
':..:.:..::.:..:.::....::..:...:.:..:..::...:.::...:';
':.:..::...::..:.::...:.:....::.:..:...:.:...:::..:.';
'::..:..:.:.::...::....:..:.:..:..::..:.:..:..::...:';
'::..:.::....:.:...:.:..::.:..:..:..:..::...:.:.:.:.';
':.:.:..::..::...::......:::..:..:..:.:.:..:.:..::..';
':..::.:...:..:.:::...:.:.....::..:.:...::..::...:.:';
':.:..:::....::..::......::.::...::...:.:..:..::..:.';
':..:.::...:.::..:.:...:.:..:..:..::...:.::.:....::.';
':.::...:.:.:..:.:.:..:...:.:..::.:....::...:.::...:';
':..::.::.....::.::.....::.:..:..:..::...:.:..::..:.';
':::.....:.:...::..::.:.:..::...::...::...::....:.:.';
':::...:..:..:.:..:..:::....:..:.:.:..:.:...::.:...:';
':::.....:.::.:..::.....::..:..:..::...:.::.:..:..:.';
'::..:.:.:..::...::.....:.:.::..::....::..::...:.:..';
':.:.:.::...::......::...::..::..:..:.:..:..:.:::...';
':.:.:...::.:...:::....::..:.:....::...:.:...::.:..:'};
pat1 = '...::';
pat2 = '..:.:';
pat3 = '..::.';
pat4 = '.:..:';
pat5 = '.:.:.';
pat6 = '.::..';
pat7 = ':...:';
pat8 = ':..:.';
pat9 = ':.:..';
pat0 = '::...';
num1 = '1';
num2 = '2';
num3 = '3';
num4 = '4';
num5 = '5';
num6 = '6';
num7 = '7';
num8 = '8';
num9 = '9';
num0 = '0';
patterns = [pat1;pat2;pat3;pat4;pat5;pat6;pat7;pat8;pat9;pat0];
numbers = [num1;num2;num3;num4;num5;num6;num7;num8;num9;num0];
eachString = [];
barcodeMinusFirst = [];
for i = 1:length(barcodes);
eachString = [eachString; cellstr(barcodes(i))];
end
barcodeStrings = char(eachString);
barcodeStrings = barcodeStrings(:,2:end);
barcodeStrings = cellstr(barcodeStrings);
patternCell = cellstr(patterns);
numberCell = cellstr(numbers);
for i = 1:length(barcodes)
newBarcodes = regexprep(barcodeStrings, patternCell, numberCell);
end
The containters.Map class would serve this purpose:
>> nums = [1:9 0];
>> patterns = {'...::';'..:.:';'..::.';'.:..:';'.:.:.';...
'.::..';':...:';':..:.';':.:..';'::...'};
>> map = containers.Map(patterns,vals)
map =
Map with properties:
Count: 10
KeyType: char
ValueType: double
Use the values method to look up a value based on an input pattern or patterns:
>> codeTest = patterns([5 4 7 1]);
>> map.values(codeTest)'
ans =
[5] [4] [7] [1]
Segment your strings into cells and now you can translate all your barcode values:
bc = cell2mat(barcodes); bc = bc(:,2:end);
codes5 = mat2cell(bc,ones(size(bc,1),1),5*ones(1,size(bc,2)/5));
codesNumeric = cell2mat(map.values(codes5))
Try this -
Function
function output_string = barcode_seq(input_string)
%%// Params
pat1 = '...::';
pat2 = '..:.:';
pat3 = '..::.';
pat4 = '.:..:';
pat5 = '.:.:.';
pat6 = '.::..';
pat7 = ':...:';
pat8 = ':..:.';
pat9 = ':.:..';
pat0 = '::...';
patterns = [pat1;pat2;pat3;pat4;pat5;pat6;pat7;pat8;pat9;pat0];
barcode_len = size(patterns,2);
s1 = reshape(input_string,barcode_len,[])';
comp_exp_mat = bsxfun(#eq,patterns,permute(s1,[3 2 1]));
[x,y] = find(squeeze(sum(comp_exp_mat,2))==barcode_len);
x(x==10)=0;
output_string = num2str(x)';
Run
>> barcode_seq('...:::.:.....::::....:..::.:..:.:..:.:..:..:.::...')
ans =
1910499980
Using the initial barcodes variable, patterns variable, and numbers variable, this code will read through each line in barcodes. It will then convert the cell array to a character array, remove the first character, create a new cell array, split the cell array into 5 symbol segments, and replace each segment with its numeric counterpart. The final step is to combine the new numeric strings into a matrix similar to the original barcodes array.
barcodes = {':...:::.:.....::::....:..::.:..:.:..:.:..:..:.::...';
':::...:..:...::...::.::.....:.:.:..:..::...:.:..:.:';
'::.:...:..:.::..::...:...:::.....::...::.::...:..:.';
'::...:.:..:::...:...::..:...:.:..::.:..:..:..::...:';
':::....::...:.:...:.:::...:.:..:.:..::...::...:.:..';
':.:..:..::...:.:...::::......:::...:.:..:.::..:...:';
'::.:..:..:....::..:.:..:.:.:..:..::..:.:.:..:.:..:.';
':.::.....:::...:.::.....::..::..:..:.:..:.:..:.:..:';
'::...::..:..::...::...::..:...:.:.:.:...:.:.:...::.';
'::..:...::...:.:::......:::..:...::..:..:..:.::.:..';
'::..:...:.:::...:...:...::.:..:.:..:.:..:.:..:.::..';
':.:..::..:.:..:...:.:.:..::.:..:.:..:.:..:..:.:.:..';
':..::...:.:..::.::....::....::..:..:..:.:.:.:...:.:';
':..::..::.....::::....:..:.:.:.:.:..:..:...::....::';
':.::...::...:.:.::....::....:.:::.....:.:..::.:...:';
'::..:..:..:..::...:.:..:.:.:..:..::.:.:...:..:...::';
':.:.:.:...:::...::...:...:..::..:..:..:.:.:..::..:.';
':...::..:.:..:.:.:..:.::....::.:..:.:..:.:..:.:..:.';
':..:.:..::.:..:.::....::..:...:.:..:..::...:.::...:';
':.:..::...::..:.::...:.:....::.:..:...:.:...:::..:.';
'::..:..:.:.::...::....:..:.:..:..::..:.:..:..::...:';
'::..:.::....:.:...:.:..::.:..:..:..:..::...:.:.:.:.';
':.:.:..::..::...::......:::..:..:..:.:.:..:.:..::..';
':..::.:...:..:.:::...:.:.....::..:.:...::..::...:.:';
':.:..:::....::..::......::.::...::...:.:..:..::..:.';
':..:.::...:.::..:.:...:.:..:..:..::...:.::.:....::.';
':.::...:.:.:..:.:.:..:...:.:..::.:....::...:.::...:';
':..::.::.....::.::.....::.:..:..:..::...:.:..::..:.';
':::.....:.:...::..::.:.:..::...::...::...::....:.:.';
':::...:..:..:.:..:..:::....:..:.:.:..:.:...::.:...:';
':::.....:.::.:..::.....::..:..:..::...:.::.:..:..:.';
'::..:.:.:..::...::.....:.:.::..::....::..::...:.:..';
':.:.:.::...::......::...::..::..:..:.:..:..:.:::...';
':.:.:...::.:...:::....::..:.:....::...:.:...::.:..:'};
pat1 = '...::';%patterns corresponding to each barcode sequence
pat2 = '..:.:';
pat3 = '..::.';
pat4 = '.:..:';
pat5 = '.:.:.';
pat6 = '.::..';
pat7 = ':...:';
pat8 = ':..:.';
pat9 = ':.:..';
pat0 = '::...';
num1 = '1'; % variables for each number
num2 = '2';
num3 = '3';
num4 = '4';
num5 = '5';
num6 = '6';
num7 = '7';
num8 = '8';
num9 = '9';
num0 = '0';
split = [];
patterns = [pat1;pat2;pat3;pat4;pat5;pat6;pat7;pat8;pat9;pat0]; %character matrix for comparison
numbers = [num1;num2;num3;num4;num5;num6;num7;num8;num9;num0]; %character matrix for replacement
patternCell = cellstr(patterns); %convert patterns to cell array
numberCell = cellstr(numbers); %convert numbers to cell array
barcodeStrings = char(barcodes); %convert to character array
barcodeStrings = barcodeStrings(:,2:end);
barcodeStrings = cellstr(barcodeStrings);
for i = 1:length(barcodes)
newBarcodes = regexp(barcodeStrings, '\W{1,5}', 'match');
end
for j = 1:length(newBarcodes)
split = [split; regexprep(newBarcodes{j}, patternCell, numberCell)];
end
numbMat = cell2mat(split);
output:
disp(numbMat)
1910499980
0833024322
9460703308
7407823847
0652099009
4321017467
9812243588
6176134444
7866675753
8320183429
8207144446
4882499989
3230634252
3610459831
6650620237
8432243941
5700734248
1224638888
2380674327
4780938218
8500443547
8052384325
5600184556
3720912132
4060166548
2769543293
6589749327
3030384748
0213900005
0854045537
0290343298
8900260609
5001134420
5370693214
Reshape your string so that each group of five symbols is on a different row, and use ismember (with the 'rows' option) to find which pattern is matched by each row:
str = '...:::.:.....::::....:..::.:..:.:..:.:..:..:.::...'; %// example string
%// Variables "patterns" and "numbers" are defined in your code
[~, ind] = ismember(reshape(str,5,[]).', patterns, 'rows');
result = numbers(ind).';
which gives
result =
1910499980
I want to remove some words from a list of words. I have a list with a recurring word and I want to get rid of it and I have no idea. I don't know whether I need to use a whole loop or regex.
from xlrd import open_workbook,error_text_from_code
book = open_workbook(inp)
sheet0 = book.sheet_by_index(0)
x = 0
y = 0
countr = sheet0.nrows
countc = sheet0.ncols
names = ''
variables = []
"different variables-----------------"
while x < countr -1:
x = x+1
y = y+1
cell = sheet0.cell(y,0)
names = names+ str(cell)
cell = sheet0.cell(y,1)
variables.append(cell)
country_text = names
countries = ', '.join(re.findall("('.*?')", country_text))
countries = countries.split()
print (variables)
print (countries)
What I get :
[number:150000.0, number:140000.0, number:300000.0]
and I need
[150000, 140000, 300000]
If you use a loop you can access to the value of a cell using this function:
sheet0.cell_value(curr_row, curr_cell)