I need help understanding a piece code - python-2.7

I would really appreciate If someone could help me understand it thanks p.s. Im new to code
sentence = "I like my dog I buy my dog toys"
s = sentence.split()
positions = [s.index(x)+1 for x in s]
print(sentence)
print(positions)
I would really appreciate If someone could help me understand it thanks p.s. Im new to code

Jean is correct. Have you done any online python tutorials?
Here goes.
The first line assigns the string "I like my dog I buy my dog toys" to a variable named sentence.
the next line
s = sentence.split()
breaks up the string into an array of substrings and assigns that array to variable s
>>> print(s)
['I', 'like', 'my', 'dog', 'I', 'buy', 'my', 'dog', 'toys']
the next line
positions = [s.index(x)+1 for x in s]
looks for the occurrence of each of each array value and logs its position to the array position
>>> print(positions)
[1, 2, 3, 4, 1, 6, 3, 4, 9]
EDIT
Allow me to elaborate on some key points. First, the split function. Many languages have a split function. They all take a delimiter, the character upon which the string will be split. In Python, the split() function can be called with no delimiter. In this case the function will use a single space character (" "). Thus when we call sentence.split(), it takes the value of the sentence variable and breaks it apart using the single space and returns an array of the various substrings, or pieces. In this case the individual words.
Next, let's look at the line
positions = [s.index(x)+1 for x in s]
Let's consider the following for a moment
for x in s
i = s.index(x)
this is a basic loop that takes each item in array s and places it in variable x. The first pass through this loop takes "I" and assigns it to x. Then we look for the position of "I" in the array of s. Since s contains the words od the sentence in order, the first position, array item 0 contains the value "I". So, the value of variable i becomes 0. The loop continues matching each item in array s and finds the value's corresponding position within the array.
Taking this one step further, we instantiate another array, in this case position. As the loop iterates over the array s finding the corresponding indices of each value, those positions are then placed in the new array position.
Now most people do not necessarily think in terms of zero based lists. Therefore, we take an extra step and add 1 to each position as it is found. So position 0 becomes position 1, and so on.
So what is different about the for loop I used to demonstrate above and the single line of code used in the example of this question? Nothing really. this line
positions = [s.index(x)+1 for x in s]
is simply a condensed form of the for loop. In Python, this is known as List Comprehension.
At this point, this answer is becoming more of a small instructional on Python. I really need to suggest that you seek out and find some tutorials on Python, starting with the one on Pythons documentation site. Another one may be here on TutorialPoint, or Learn Python. There are also great resources on Pluralsite and Cousera as well.
Good luck

Related

How to I shorten this code to comply with the DRY (Don't Repeat Yourself) principle?

I want to take words a user provides, store them in a list, and then modify those words so that every other letter is capitalized. I have working code but it is repetitive. I cannot for the life of me figure out how to get all the words ran through in one function and not have it output one long string with the spaces removed. Any help is appreciated.
This is my current code:
def sarcastic_caps(lis1):
list=[]
index=0
for ltr in lis1[0]:
if index % 2 == 0:
list.append(ltr.upper())
else:
list.append(ltr.lower())
index=index+1
return ''.join(list)
final_list.append(sarcastic_caps(lis1))
Imagine 4 more iterations of this ^. I would like to do it all in one function if possible?
I have tried expanding the list index but that returns all of the letters smashed together, not individual words. That is because of the .join but I need that to get all of the letters back together after running them through the .upper/.lower.
I am trying to go from ['hat', 'cat', 'fat'] to ['HaT', 'CaT', 'FaT'].

PythonQuestion on Longest Common Substring(LCS) algorithm

I'm pretty new to Python, it's my first programming language, and I've wanted to work on some manual data structure manipulation and playing around.
I've recently been learning the basic algorithm for solving the LCS problem, and I understand how it works besides one line of code that I for some weird reason can't seem to convince myself I am grasping entirely.
this is the code I've been using to learn from after I couldn't get it down myself quite right.
EDIT 2: Anyway to make this work with an input of two lists of integers?**I figured out that I was understanding my original question correctly, but would anyone know how I could make this work with a **list of integers? I tried converting S and T to a string of comma separated values, which worked in matching some of the characters, but even then it rarely worked in most test-cases. I'm not sure why it wouldn't, as it is still just two strings being compared, but with commas.
def lcs(S,T):
m = len(S)
n = len(T)
counter = [[0]*(n+1) for x in range(m+1)]
longest = 0
lcs_set = set()
for i in range(m):
for j in range(n):
if S[i] == T[j]:
c = counter[i][j] + 1
counter[i+1][j+1] = c
if c > longest:
lcs_set = set()
longest = c
lcs_set.add(S[i-c+1:i+1])
elif c == longest:
lcs_set.add(S[i-c+1:i+1])
return lcs_set
Now my issue is understanding is the line : lcs_set.add(S[i-c+1:i-1])
I understand that the counter is incremented when a match is found, to give longest the length of the substring. So, to make it easy, if S = Crow and T = Crown, when you reach w, the last match, the counter is incremented to 4, and i is at index 3 of S.
Does this mean I am to read this as: i (index3 on S, the W) - c (4), so 3-4 = -1, so 3-4+1 = 0 (at C) and for the right side of the slice: i(3) + 1 = 4(N, but will not be included, obviously), meaning we end with S[0:4], Crow, to LCS_Set?
If that is the case, I guess I am confused as to why we are adding the whole substring to the set, and not just the newest matched character?
If I understand right, it is updating LCS_set with the entire slice of the current matched substring, so if it were on the second match, R, the counter would be at 2, i would be at 1, and it would be saying S[1-2+1:i(1)+1], so 1-2 = -1, -1 + 1 = 0(C) up to i(1)+1 = 2 (leaving us with S[0:2], or CR), so each time around, the set is updated with the entire substring, and not just the current index.
It's not really a problem, I just want to make sure I'm understanding this correctly.
I would really appreciate any input, or any tips anyone might see with my current logic!!
EDIT:
I just realized I was totally forgetting that the position at C is the current counter number, therefore it obviously wouldn't be updating the LCS_set with the current max match number, and it can't update it with just the current matched letter, so it has to take the slice of the substring in order to update the LCS_Set.
Thanks in advance!

How do I split a word into individual letters in Python 2.7

I have a number that I have converted into a string. I now want to assign each of the digits of this number to a new variable that I want to use later. How do I do it?
For example if
input = "98912817271"
How do I assign the one's digit, 1, to a variable 1 and so on?
I've tried searching for a solution but couldn't find any on StackOverflow.Any help would be much appreciated.
in python, words are already lists, this means, they already have positions asigned,
try: print input[0] and see
if you want to assign a variable the value of any position in your string, just select the position as if it was a list:
foo = input[#]
Try this:
def string_spliter(s):
result = []
for element in s:
result.append(element)
return result
print(string_spliter(string))

Python re find index position of first search match

I have a series of strings, most of which contain 4 digits in a row. I want to slice the string at the end of that fourth digit, using Python. Sometimes the string contains more than one such pattern. What I want is the index position of the FIRST match of my regular expression. What I have been able to get is the LAST match.
myString = 'Today is June 14, 2019. I sometimes like to think back when I was a child in 1730.'
theYear = re.compile("\d{4}")
[(m.start(0), m.end(0)) for m in re.finditer(theYear, myString)]
print m.span(0)
The result is (77, 81), which is the index position for the second date, not the first one. I know the problem is my loop, which will iterate through all of the matches, leaving me with the last one. But I havn't been able to figure out how to access those index positions without looping.
Thanks for any help.
print theYear.search(myString).span()

How to find number of words in a phrase with spaces removed by checking v. dictionary

I have a word, for example "ilikesamsung", and a dictionary of words, for example:
{"i","like","the","king","sam","sung","samsung"}
I want to calculate number of spaces in this word, if we break it into the dictionary words. In the above example, the string is broken into "i like samsung", and there are 2 spaces.
How can it be done?
Elaboration:
Problem has 2 parts-
Output Yes if this word can be broken.
Calculate number of spaces
What I have tried:
I have solved First part by Dynamic Programming.,
For the second part:
I have extended DP method to solve part2 . I took an array and I stored index at which word ends. arr[]={1,0,0,0,1,0,0,1,0,0,0,1}
this gives answer 3.
I want answer 2, for this array should be {1,0,0,0,1,0,0,0,0,0,0,1}.
Any advises?
You can use Dynamic Programming for this task:
f(0) = 0
f(i) = MIN { f(i-j) + (Dictionary.contais(s.substring(i-j,i)?1:INFINITY } for each j=1,...,i
The above finds the number of words in your string, so the final answer is f(#characters)-1
The idea is to do an "exhaustive search" - for a given new character - try to connect it to a word, and recursively invoke on what's left from the string.
This can be done pretty efficiently with DP techniques.