samedi 27 novembre 2021

Using list comprehension as condition for if else statement

I have the following code which works well

list = ["age", "test=53345", "anotherentry", "abc"]

val = [s for s in list if "test" in s]
if val != " ":
    print(val)

But what I'm trying to do is using the list comprehension as condition for an if else statement as I need to proof more than one word for occurence. Knowing it will not work, I'm searching for something like this:

PSEUDOCODE
if (is True = [s for s in list if "test" in s])
print(s)
elif (is True = [l for l in list if "anotherentry" in l])
print(l)
else:
print("None of the searched words found")

Why does cycle 'if' delete not all odd numbers from the list (only each second odd number)?

There is a code:

list = [1, 2]

while list[-1]+list[-2] <= 4000000:
    list.append(list[-1] + list[-2])
for i in list:
    if i % 2 == 1:
        print(i)
        list.remove(i)
print(list)
print(sum(list))

Question regarding post decrement in loop jointed with if

What is the difference between the following to code? The result is different. b is some String and int j = b.length() - 1

The if branch is in a loop and runs many times if this makes any difference.

if (j >=0 && b.charAt(j) == '1') {
    j--;
    carry ++;
}

vs.

if (j >=0 && b.charAt(j--) == '1') {
    carry ++;
}

Google Sheets - IF with ISBLANK

I am working in google sheets and I would like to ask you one thing. I want to check if cell is blank and if it is it should return 0, if it is not it should calculate the formula, which I have used befere. https://i.stack.imgur.com/Qh4dc.png (can not post image...) But I am still getting this #ERROR! which says error when analyzing formula. I have tried it in Microsoft Excel and exact formula works...

Did somebody already solved this? Thanks a lot! 👌

elif not behaving as expected - beginner/ noob problems

I have a half dozen if and elif statements and the second elif won't work. It has something to do with the less than sign because the greater than sign works Why?

elif temperature < 10:
   print("Don't bother getting out of bed")

Complete source:

temperature = 3

if temperature > 30:
    print("Its a hot day, drink plenty of water")
if temperature < 15:
    print("No need to drink any water today")
elif temperature > 39:
    print("Its really hot so stay indoors")
elif temperature < 10:
    print("Don't bother getting out of bed")
else:
    print("Its a lovely day today")

# this returns:
# No need to drink any water today
# Process finished with exit code 0

https://github.com/Destria-black/Learning.git

IF THEN STATEMENT IN R - REPLACING BASED ON VALUE

I have a very large file where I have merged two customer databases. The key is the ID. Where the customer name did not match it shows an NA. I need to accomplish a simple if/then statement where if there is "NA" in column NAME_1 the DESIRED OUTCOME NAME is what is in NAME_2, else use what is in NAME_1

I attempted the following code but get errors

df <- df %>% if (df$NAME_1 == "NA") rename(df$NAME_1 == df$NAME_2)

enter image description here

Batch: Why does the parsing behavior of a variable of a FOR change within an IF?

Why is this batch not working:

IF "%videoFile%"=="" (
    SET videoExt=mp4
    FOR /F "delims=*" %%a IN ('dir /b *.%videoExt%') DO (SET videoFile=%%~nxa)
)

While this works:

IF "%videoFile%"=="" (
    FOR /F "delims=*" %%a IN ('dir /b *.mp4') DO (SET videoFile=%%~nxa)
)

And as a smart guess this works too:

IF "%videoFile%"=="" (
    SET videoExt=mp4
    FOR /F "delims=*" %%a IN ('dir /b *.%%videoExt%%') DO (SET videoFile=%%~nxa)
)

Is there maybe another way possible?