Link 2 variables together - python-2.7

I wanted to know how to match a string between the column. For ex:
A. B. C. D. E. F.
1. 2. 3. 4. 5. 6
In above example let Alphabet be column name and digits are considered as values in the column.
So, i want to match Column A , Value 1 and Column E , Value 5. Values in Column A and E is different.
I'm using something like (A + '.*' + E)
To get something like - 1. 5.
Any help is really appreciated.
Thanks, :)

Why not just use dictionaries?
>>>dic = {'A': 1, 'B': 2}
>>>print(dic[A])
1
I hope this helps.

Related

How to match a key and a value using regular expressions if the key and value can be swapped?

Let's say I have the following keys: a, b, c and I can have text like this:
a 1 b 2 c 3
or
1 a 2 b 3 c
How can I write a regular expression that matches b 2 in the first text and matches 2 b in the second text? The idea is to return matches as keys and values no matter the order on which they appear in the text.
The main problem I've come so far is that when trying to look for a match in one order I will get false positives for the other order, so how can I solve this?
Thank you all!
Could be as simple as this:
(b 2|2 b)
Link with example: https://regex101.com/r/ZnW96W/1

Convert letter to numeric number formatting in Excel using RegExp

I have an Excel sheet with given data in a cell. Want to convert letter numbering (e.g. a, c) or b)) to numeric number format(e.g. 1, 3) or 2))
Input Cell Data
Introduction and Basic wording
More lines here
a,c) A steps 1 and 3
- A sub-step) 1 of 1.
- A sub-step 2 of 1
...
b) A step 2.
- A sub-step 1 of 2
d-g) A step 4 and 6
h) A step 5
Note:
Inside cell there are few character in given format 'p)'
Output Cell Data
Introduction and Basic wording
More lines here
1,3) A steps 1 and 3
- A sub-step) 1 of 1.
- A sub-step 2 of 1
...
2) A step 2.
- A sub-step 1 of 2
4-7) A step 4 and 6
8) A step 5
I used below RegExp to get the proper matching of letter numbering
(^|\n)((\w(?=\)|\,|\-)(\,|\-)){0,3}\w?\))
To replace it to numeric numbering format, I am using below code
Set colMataches = RE.Execute(strRegExp)
For Each match In colMataches
WScript.Echo "Found:"&match.Value
Dim mt : mt = match.Value 'Output result: a,c), b)etc.
Select Case mt
...
Case "b)"
sProc = RE.Replace(strRegExp, 2)
...
Case Else
WScript.Echo "Not Found"
End Select
As per my understanding:Replace(expression , find , replacewith[ , start[ , count[ , compare]]])
where find is the substring you want to replace & replacewith is the substring you want to replace with.
This usually work good when we are trying to find global matches for a specific value.e.g. want to replace a character like 'a' or non character like '-' globally with a single & specific value like alphabet('b') or Numeric('1') or anything other than alphanumeric('#').
But i am failing to replace(a->1, b->2, c->3, d->4 etc) by use of Select Case statement. Is there a better way to do proper replacement?

INDEX and MATCH

Hi im looking to for help!
IF column A matches column B AND at the same time if column C matches column D; Then return the difference from column E to F and place number in column G
can anyone please help?
Please try in G1:
=IF(AND(A1=B1,C1=D1),F1-E1,"")
copied down to suit.

How to create a column that is a repetition of a column's contents by a specified number of times mentioned in another column?

I want to use columns 'A' and 'B' to create column 'Result' which is content of A repeated B number of times
A B Result
z 3 zzz
az 2 azaz
Tried using Result=repeat(A,B) which didn't work out. Is there something I missed while using the repeat statement?
The REPEAT function returns a character value consisting of the first argument repeated n times, Thus, the first argument appears n + 1 times in the result.
So, you have to subtract 1 from B to get the result that you want.
Try
Result=repeat(A,int(B)-1)
It is simple in R! . Sorry I did not look for the tag, but here is how R does it
Try the function makeNstr() from package Hmisc
>require(Hmisc)
>df <- data.frame(A = c("a","az"), B = c(3,2))
>Result <- makeNstr(df$A,df$B)
>df <- cbind(df,Result)
>df
A B Result
1 a 3 aaa
2 az 2 azaz
Hope you find it useful

The Art of Computer Programming exercise question: Chapter 1, Question 8

I'm doing the exercises to TAOCP Volume 1 Edition 3 and have trouble understanding the syntax used in the answer to the following exercise.
Chapter 1 Exercise 8
Computing the greatest common divisor of positive integers m & n by specifying Tj,sj,aj,bj
Let your input be represented by the string ambn (m a's followed by n b's)
Answer:
Let A = {a,b,c}, N=5. The algorithm will terminate with the string agcd(m,n)
j Tj sj bj aj
0 ab (empty) 1 2 Remove one a and one b, or go to 2.
1 (empty) c 0 0 Add c at extreme left, go back to 0.
2 a b 2 3 Change all a's to b's
3 c a 3 4 Change all c's to a's
4 b b 0 5 if b's remain, repeat
The part that I have trouble understanding is simply how to interpret this table.
Also, when Knuth says this will terminate with the string agcd(m,n) -- why the superscript for gcd(m,n) ?
Thanks for any help!
Edited with more questions:
What is Tj -- note that T = Theta
What is sj -- note that s = phi
How do you interpret columns bj and aj?
Why does Knuth switch a new notation in the solution to an example that he doesn't explain in the text? Just frustrating. Thanks!!!
Here's an implementation of that exercise answer. Perhaps it helps.
By the way, the table seems to describe a Markov algorithm.
As far as I understand so far, you start with the first command set, j = 0. Replace any occurencies of Tj with sj and jump to the next command line depending on if you replaced anything (in that case jump to bj, if nothing has been replaced, jump to aj).
EDIT: New answers:
A = {a,b,c} seems to be the character set you can operate with. c comes in during the algorithm (added to the left and later replaced by a's again).
Theta and phi could be some greek character you usually use for something like "original" and "replacement", although I wouldn't know they are.
bj and aj are the table lines to be next executed. This matches with the human-readable descriptions in the last column.
The only thing I can't answer is why Knuth uses this notation without any explanations. I browsed the first chapters and the solutions in the book again and he doesn't mention it anywhere.
EDIT2: Example for gdc(2,2) = 2
Input string: aabb
Line 0: Remove one a and one b, or go to 2.
=> ab => go to 1
Line 1: Add c at extreme left, go back to 0.
=> cab => go to 0
Line 0: Remove one a and one b, or go to 2.
=> c => go to 1
Line 1: Add c at extreme left, go back to 0.
=> cc => go to 0
Line 0: Remove one a and one b, or go to 2.
No ab found, so go to 2
Line 2: Change all a's to b's
No a's found, so go to 3
Line 3: Change all c's to a's
=> aa
Line 4: if b's remain, repeat
No b's found, so go to 5 (end).
=> Answer is "aa" => gdc(2,2) = 2
By the way, I think description to line 1 should be "Remove one "ab", or go to 2." This makes things a bit clearer.
The superscript for gcd(m,n) is due to how numbers are being represented in this table.
For example: m => a^m
n => b^n
gcd(m,n) => a^gcd(m,n)
It looks to be like Euclids algorithm is being implemented.
i.e.
gcd(m,n):
if n==0:
return m
return gcd(n,m%n)
The numbers are represented as powers so as to be able to do the modulo operation m%n.
For example, 4 % 3, will be computed as follows:
4 'a's (a^4) mod 3 'b's (b^3), which will leave 1 'a' (a^1).
the notion of am is probably a notion of input string in the state machine context.
Such notion is used to refer to m instances of consecutive a, i.e.:
a4 = aaaa
b7 = bbbbbbb
a4b7a3 = aaaabbbbbbbaaa
And what agcd(m,n) means is that after running the (solution) state machine, the resulting string should be gcd(m,n) instances of a
In other words, the number of a's in the result should be equal to the result of gcd(m,n)
And I agree with #schnaader in that it's probably a table describing Markov algorithm usages.