0

I have the following two strings:

request= "1/1/1.3.45.3.6/3/4
reply= "1/2/3"

I want to return a new string, which concatenates the 1.3.45.3.6 of request with 2 of reply to return a list which contains the string.

Output should be a list:

result= ["1.3.45.3.6.2"]

I have the following method, that doesn't work. The inputs to the method are both strings.

def concatentatestring(request, response):
    oidStatusValueList = [f"{i.split('/')[2]}.{j.split('/')[2]}" \
       for i in request for j in response \
           if (i.split('/')[1] == j.split('/')[1]) ]
5
  • Updated the code in the question. It was a typo. Commented Aug 7, 2019 at 19:23
  • No problem, but since you're always guaranteed a single element list (if I'm reading this correctly), why bother making it a list? print(f"{request.split('/')[2]}.{response.split('/')[1]}") gives your desired output, no need to iterate. Commented Aug 7, 2019 at 19:23
  • 1
    How do you decide which parts of the strings should be merged? Commented Aug 7, 2019 at 19:27
  • I want to use a list Commented Aug 7, 2019 at 19:28
  • @LauraSmith see my answer. Commented Aug 7, 2019 at 19:42

4 Answers 4

1

Dont use a list comprehension as you are not iterating over request and response

>> request= "1/1/1.3.45.3.6/3/4"
>>> reply= "1/2/3"
>>> [f"{request.split('/')[2]}.{reply.split('/')[1]}"]
['1.3.45.3.6.2']

So your concatentatestring function would look like

>>> def concatentatestring(request, response):
...     return [f"{request.split('/')[2]}.{response.split('/')[1]}"]
... 
>>> 
>>> concatentatestring(request, reply)
['1.3.45.3.6.2']
Sign up to request clarification or add additional context in comments.

Comments

1

Adding two strings can be done with the + operator or using an f-string (I don't believe theres much if any performance difference)

You can split a string with the split method as you used and then if the format of the request and the response will always be the same you know to get the 2 element of the split request and the 1 element of the split response

def addstring(request, response):
    return [request.split('/')[2] + '.' + response.split('/')[1]]

Comments

0

Your code was not quite doing it as you needed to properly iterate through the strings. You also should split them before the list comprehension. To use both the index and the value at that index, you can use enumerate and, lastly, you should check to make sure you have not reached the end of the strings or it could cause errors.

This is what you are looking for:

request = "1/1/1.3.45.3.6/3/4"
reply = "1/2/3"

def concatenateStrings(request, response):
    reqs = request.split("/")
    resps = response.split("/")
    oidStatusValueList = [f"{reqs[i+1]}.{resps[j+1]}" \
       for i, req in enumerate(reqs) for j, resp in enumerate(resps) \
           if (req == resp and i<len(reqs)-1 and j<len(resps)-1) ]
    return oidStatusValueList

result = concatenateStrings(request, reply)
print(result)

Your expected output is also incorrect considering what you are inputting and your criteria for joining the strings. This code results in:

['1.2', '1.3.45.3.6.2']

Comments

0

you are iterating through every letter in the strings in the for loops you wrote.

I believe this can do what you are asking for:

request= "1/1/1.3.45.3.6/3/4"
reply= "1/2/3"

def concatentatestring(request, response):
    request_splited=request.split('/') # =["1","1","1.3.45.3.6","3","4"]
    response_splited=response.split('/') # =["1","2","3"]
    if request_splited[1] == response_splited[1]:
        return [f'{request_splited[2]}.{response_splited[1]}']
    else:
        ...

2 Comments

For example, I also need to check if the second index of the request and reply string are equal before concatenating them together. How would I do that?
and if they are not equal? I edited the response to check that but without a case for if they are not equal

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.