mercredi 28 juillet 2021

How can I summarize splits and regex into less lines in Python?

if len(re.findall('\\bim\\b', message.lower())) > 0:
    new_list = message.lower()
    x = new_list.split("im ")
elif len(re.findall("\\bi'm\\b", message.lower())) > 0:
    new_list = message.lower()
    x = new_list.split("i'm ")

I found this code of mine from ages ago and I'm awful in Regex, I know that you can use the (a|b) filter probably though, it's obviously very inefficient and I'm sure it can be tidied up a lot.

Filtering Rows with duplicate column values

I was cleaning a dataset for class. I noticed there were some negative values. Some rows with this condition also have the same id name in two columns 2 and 3.

I'm stumped. I'm trying to draft out a code, but unsure where should I start. I would love to get advice. I couldn't find anything similar.

Below is a sample table similar to the table I have.

df <- data.frame(A=c(1,2,4,7,8), B=c(2,2,4,9,9), C=c(0,1,5,3,4))

Do I use the ifelse () nested within a filter()? Basically, I want to filter a data table without rows 2 and 3. I'm reading up on pipes currently. But what I'm learning so far doesn't seem to be a solution.

compare two columns in excel to find if column 2 contains the text string of column1, if so copy the string & paste against column2

Sheet 1 column with village names

Sheet 2 column with full addresses

if sheet 2 column contains the string of sheet1 column, that matching string must be copied against respective sheet 2 column(Where that string found). kindly help to write code in vba to use in excel. Thanks in advance

How to manage the login and logout in golang?

In this code, I made a function called logout in which I wrote struct to check if the Logout variable is called then it should return true. I called this function in logout.html to check the if statement.

What I want to do is that if I am not logged in then it should give me the message that Please login first and if I am logged in then after clicking on logout button it should give me the else statement message.

But when visit the localhost:4000/logout even if I am logged in or not, it gives me the same message of if-statment every time.

handler.go

func logout(w http.ResponseWriter, r *http.Request) {
    d := struct{ Logout bool }{Logout: true}
    logoutTmpl.Execute(w, d)
}

logout.html


    <h1>Please login first</h1> 

    <h1>You been successfully logout</h1>

IF Then Else Statement in eclipse in expression builder

enter image description here

In the output there are two language NLD and FRA. There is an if statement that i have been trying to make that if the language "bpp_langue_code" is FRA the results of the "bpa_ad_ville2" should be "Brussel" else the "bpa_ad_ville2" should be "Bruxelles".

But it's not working and showing no syntax error.

Below is my code:

if (row["BPP_LANGUE_CODE"] == "FRA") {
  (row["bpa_ad_ville2"] == "Bruxelles");
} else {
  (row["bpa_ad_ville2"] == "Brussel");
}

Any ideas? Thanks!

How do I refactor multiple if/elif/else statements when I have complex conditions?

I have something like this below which is repeated in many places in the codebase. Is there a way to consolidate it? I had a look at this, and it is essentially the same question, but with more complex conditions.

if something < value1:
    my_fun1()
elif value1 <= something < value2 and something_else == another_value:
    my_fun2()
elif value2 <= something <= value3 and something_else == another_value:
    my_fun3()
...
else:
    fallback_fun()

How code python/pyspark if statement (medium/hard complexity)?

I'm working in Pyspark with two datasets and am stuck on how to code something. I'm sure this could involve if statements, various joins and/or groupBy() functions.

The two dataframes have information essentially like below. In practice the dfs are huge and with many instances of the example given.

df1:

Name1 Name2 Key
A Z 1
A Y 1
B X 1
B W 1
C V 1

df2:

Name1 Name2 Key
A Z 2
B U 2

In df1, sometimes 'name1' will match to multiple 'name2'.

In df2, 'name1' is always unique (ie always one instance of each 'name1').

I would like: if 'name1' matches to multiple 'name2' in df1, and one of those name1/name2 pairs is in df2 (ie A-Z row in the example), then drop all other rows in df1 with that name1 (ie drop A-Y row) (so B-X, B-W, C-V remain).

Any help would be much appreciated!