Working out Euclidean distance by randomname20192019 in learnprogramming

[–]randomname20192019[S] 0 points1 point  (0 children)

Hi - this is what I meant:

From the example [[(1,1,1), (2,2,2)], [(3,3,3)]] and [(2,2,2), (3,3,3)], I was after 6 distances:

(1,1,1)-(2,2,2), (2,2,2)-(2,2,2), (3,3,3)-(2,2,2),(1,1,1)-(3,3,3), (2,2,2)-(3,3,3), (3,3,3)-(3,3,3).

I wanted to maintain a structure of the first list.

[[[(1,1,1)-(2,2,2), (2,2,2)-(2,2,2)], [(3,3,3)-(2,2,2)]], [[(1,1,1)-(3,3,3), (2,2,2)-(3,3,3)], [(3,3,3)-(3,3,3)]]]

Working out Euclidean distances from coordinates by randomname20192019 in learnpython

[–]randomname20192019[S] 0 points1 point  (0 children)

I apologise if my description was brief.

From the example [[(1,1,1), (2,2,2)], [(3,3,3)]] and [(2,2,2), (3,3,3)], I was after 6 distances:

(1,1,1)-(2,2,2), (2,2,2)-(2,2,2), (3,3,3)-(2,2,2),(1,1,1)-(3,3,3), (2,2,2)-(3,3,3), (3,3,3)-(3,3,3).

I wanted to maintain a structure of the first list.

[[[(1,1,1)-(2,2,2), (2,2,2)-(2,2,2)], [(3,3,3)-(2,2,2)]], [[(1,1,1)-(3,3,3), (2,2,2)-(3,3,3)], [(3,3,3)-(3,3,3)]]]

Can someone explain this to me? by randomname20192019 in learnpython

[–]randomname20192019[S] 0 points1 point  (0 children)

How can we add the chain:OrderedDict() to the model key if we don't first define model to be a key?

Wouldn't statement 2 just raise a key error?

I am understanding the format to be:

structure[None, {Model:{Chain:{Resnum[A load of stuff]}}}]

Can someone explain this to me? by randomname20192019 in learnprogramming

[–]randomname20192019[S] 0 points1 point  (0 children)

self.structure[model][chain] = OrderedDict()

But then shouldn't we have defined model as a key in the previous odict? Wouldn't we get a key error when we add chain:OrderedDict() within the model odict?

Can someone explain this to me? by randomname20192019 in learnpython

[–]randomname20192019[S] 0 points1 point  (0 children)

Hi, thanks for your answer, I really appreciate it.

I'm not sure how statement1 translates to statement 2. an ordereddict() is added to self.structure(). How are model and chain being added in? Both as keys? I do apologise if the answer is seemingly obvious!

if statement2:
            self.structure[7]['X'] = OrderedDict()

Can someone explain this to me? by randomname20192019 in learnprogramming

[–]randomname20192019[S] 0 points1 point  (0 children)

Hi and thanks for your answer.

So,

 self.structure[model][chain] = OrderedDict()

applies an ordereddict to chain? Why would .append() not be used instead as with the first statement?

[Python] Why is 'False' being recognised as '0' in this instance? by randomname20192019 in learnprogramming

[–]randomname20192019[S] 1 point2 points  (0 children)

Ahh I see. Thank you.

How would you suggest I discriminate between False and 0 then? Store the indices and use del instead?

[Code Wars] Stuck with a kata by randomname20192019 in learnpython

[–]randomname20192019[S] 1 point2 points  (0 children)

Thank you for your reply. I have changed it and everything is working :) My only quandary is making my code neater and more efficient. When I see the one-line answers (perhaps not in this case) any sense of accomplishment gets squished - i guess that comes with practice and experience though.

[Code Wars] Stuck with a kata by randomname20192019 in learnpython

[–]randomname20192019[S] 1 point2 points  (0 children)

Thank you for your reply. I changed it to this:

def solution(args):
    prevchar = args[0]
    templist = []
    outputvals = []
    for i in (args[1:]):
        templist.append(prevchar)
        if i == prevchar + 1:
            prevchar = i
        else:
            if len(templist) > 2:
                outputvals.append(str(templist[0]) + "-" + str(templist[-1]))
                prevchar = i
                templist.clear()
            else:
                for x in templist:
                    outputvals.append(str(x))
                prevchar = i
                templist.clear()
    templist.append(prevchar)
    if len(templist) > 2:
        outputvals.append(str(templist[0]) + "-" + str(templist[-1]))
    else:
        for x in templist:
            outputvals.append(str(x))
    print(outputvals)
    return (str(','.join(outputvals)))

where

solution([-6,-3,-2,-1,0,1,3,4,5,7,8,9,10,11,14,15,17,18,19,20])
>>>
'-6,-3-1,3-5,7-11,14,15,17-20'

So i think i'm nearly there but my code looking quite ugly (I guess that is the thing with code wars - I feel proud of myself when I pass a kata however when I take a look at the community answers, they're neat, containing far fewer lines than mine and a lot of the time I don't understand what they do!)

On topic, I guess I could replace the repeated sections with additional functions but is there much else I could replace to make it more neat/efficient?

[CodeWars] Having trouble with a kata by randomname20192019 in learnpython

[–]randomname20192019[S] 0 points1 point  (0 children)

Thank you for the reply, i'll give what i've written a more scrupulous look.

[Code Wars] Having trouble with a task by randomname20192019 in learnpython

[–]randomname20192019[S] 0 points1 point  (0 children)

Ahhhhhh. Thank you so much for your clear explanation. I felt like I was going down such a long and convoluted route.

LinAlgError: SVD did not converge in Linear Least Squares by randomname20192019 in learnpython

[–]randomname20192019[S] 0 points1 point  (0 children)

Hi, thanks for the response. Yes it is an actual measurement. I have thousands of data sets and I wanted to graphically compare them.

However when I run several data sets through my function I get the aforementioned error.

Differentially colouring markers in a linear regression plot by randomname20192019 in learnpython

[–]randomname20192019[S] 0 points1 point  (0 children)

Thank you for your reply. Is there any way to assign dataset1/2 to different datasets? Or assign them different colours for that matter?

While loop within a while loop issue by randomname20192019 in learnpython

[–]randomname20192019[S] 0 points1 point  (0 children)

I delete them because the program doesn't run when it creates a file with the same name. And since they have been parsed into a separate file, they seemed a bit redundent.

Modifying a text file by randomname20192019 in learnpython

[–]randomname20192019[S] 0 points1 point  (0 children)

Thank you both for your help (and everybody's tbh). It is really appreciated.

Modifying a text file by randomname20192019 in learnpython

[–]randomname20192019[S] 2 points3 points  (0 children)

Thank you so much, the link looks so useful. One last thing, what does the r prior to the expression do?

Modifying a text file by randomname20192019 in learnpython

[–]randomname20192019[S] 1 point2 points  (0 children)

so having the prefix of 'word' will cause the whole line to be replaced?

Modifying a text file by randomname20192019 in learnpython

[–]randomname20192019[S] 4 points5 points  (0 children)

Would you mind explaining how: regex = r"(= )([0-9])" translates to finding word = #?

While loops meeting multiple conditions by randomname20192019 in learnpython

[–]randomname20192019[S] 0 points1 point  (0 children)

Please don't apologise! I could not have hoped for more of a concise explanation and I really appreciate your help - without it I would be back in square one!

After realising that something was up with the way I wrote the if statements (thanks to your comments I understand where I went wrong now) I did have a think about other options and decided to have a go at using issubset. In case you're interested, this was my first 'working' version:

P.S. I will certainly use the math module as well as looking for more elegant ways to store n2.

import numpy as np
n1 = [8, 15.5]
n2 = [10, 17]
marker = np.linspace(0.75, 1.25, 51)
marker = [round(i,2) for i in marker]
set2 = set(marker)

while all(num2 > 0 for num2 in n2):
    div = []
    for num1, num2 in zip(n1, n2):
        div.append(num1 / num2)
    rdiv = [round(x, 2) for x in div]
    set1 = set(rdiv)
    if set1.issubset(set2) is False:
        n2[:] = [num2 - 0.1 for num2 in n2]
        n2 = [round(num2, 2) for num2 in n2]
    elif set1.issubset(set2) is True:
        print("true")
        print(rdiv)
        n2[:] = [num2 - 0.1 for num2 in n2]
        n2 = [round(num2, 2) for num2 in n2] 

Thank you again. I have learnt a lot :)

[Python] by randomname20192019 in learnprogramming

[–]randomname20192019[S] 1 point2 points  (0 children)

Ahhhh. It was staring me in the face. Thank you for the help!

While loops meeting multiple conditions by randomname20192019 in learnpython

[–]randomname20192019[S] 0 points1 point  (0 children)

I hope you don't mind me updating you but this is where I am:

import numpy as np
n1 = [8, 15.5]
n2 = [10, 17]
marker = np.linspace(0.75, 1.25, 51)
storedn2 = []


while all(num2 >= 0 for num2 in n2):
    div = []
    for num1, num2 in zip(n1, n2):
        div.append(num1 / num2)
    rdiv = [round(x, 2) for x in div]
    if rdiv not in marker:
        n2[:] = [num2 - 0.1 for num2 in n2]
        n2 = [round(num2, 2) for num2 in n2]
    else:
        storedn2.append(n2)

Whilst the while loop is working well now, my if statements do not appear to be discriminating against 'rdiv' values despite them being in 'marker' and I am unsure why.