lundi 1 février 2021

Control flow for returning list values

I'm trying to return a list of values given this function:

Pass_or_fail <- function(y){
 x = round(runif(y, 0, 100), 1)
  if(x>50){
    print("Passed")
  }else if(x < 50){
    print("Failed")
  }
}

When I pass a value into y for example 20 this should return a list of either Passed or Failed characters. However, the function above only returns a single vector. I wish for it to return 20 values relative to the condition provided above, mainly in list format.

I was thinking of creating a variable likeso:

t_list <- list(1:20)
#then
Map(Pass_or_fail(20), t_list)

Output:

 "Passed" "Failed" "Passed" "Failed" "Passed" "Failed" "Passed" "Failed" "Passed" "Failed" "Passed" "Failed" "Passed" "Failed" "Passed" "Failed" "Passed" "Failed" "Passed" "Failed"

I cannot seem to figure it out yet.

Python: get n next elements in list if they match condition

I have a list that contains lists of strings :

[['Far', 'far', 'away', ',', 'behind', 'the', 'word', 'mountains', ',', 'far', 'from', 'the', 'countries', 'Vokalia', 'and', 'Consonantia', ',', 'there', 'live', 'the', 'blind', 'texts', '.'], ['Separated', 'they', 'live', 'in', 'Bookmarksgrove', 'right', 'at', 'the', 'coast', 'of', 'the', 'Semantics', ',', 'a', 'large', 'language', 'ocean', '.'], ['A', 'small', 'river', 'named', 'Duden', 'flows', 'by', 'their', 'place', 'and', 'supplies', 'it', 'with', 'the', 'necessary', 'regelialia', '.'], ['It', 'is', 'a', 'paradisematic', 'country', ',', 'in', 'which', 'roasted', 'parts', 'of', 'sentences', 'fly', 'into', 'your', 'mouth', '20', '2', '.'], ['Even', 'the', 'all', 'powerful', 'Pointing', 'has', 'no', 'control', 'about', 'the', 'blind', 'texts', 'it', 'is', 'an', 'almost', 'unorthographic', 'life', '1', 'day', 'however', 'a', 'small', 'line', 'of', 'blind', 'text', 'by', 'the', 'name', 'of', 'Lorem', 'Ipsum', 'decided', 'to', 'leave', 'for', 'the', 'far', 'World', 'of', 'Grammar', '.'], ['The', 'Big', 'Oxmox', 'advised', 'her', 'not', 'to', 'do', 'so', ',', 'because', 'there', 'were', 'thousands', 'of', 'bad', 'Commas', ',', 'wild', 'Question', 'Marks', 'and', 'devious', 'Semikoli', ',', 'but', 'the', 'Little', 'Blind', 'Text', 'didn', '’', 't', 'listen', '.'], ['She', 'packed', 'her', '7', 'versalia', ',', 'put', 'her', 'initial', 'into', 'the', 'belt', 'and', 'made', 'herself', '40', '4', 'on', 'the', 'way', '.'], ['When', 'she', 'reached', 'the', 'first', 'hills', 'of', 'the', 'Italic', 'Mountains', ',', 'she', 'had', 'a', 'last', 'view', '3', '00', 'and', '3', 'back', 'on', 'the', 'skyline', 'of', 'her', 'hometown', 'Bookmarksgrove', ',', '2', '000', 'and', '20', '1', 'the', 'headline', 'of', 'Alphabet', 'Village', 'and', 'the', '2', '000', ',', '20', '1', 'subline', 'of', 'her', 'own', 'road', ',', 'the', 'Line', 'Lane', '.'], ['Pityful', 'a', 'rethoric', 'question', 'ran', 'over', 'her', 'cheek', ',', 'then']]

I want to loop through every sublist and capture every sequence of digits in a list. so I want to capture, in groups:

['20', '2'],[1],['3', '00', 'and', '3],['20', '1],['2', '000', ',', '20', '1']

So far this is what I managed to do:

for sentence in listToks :
    temp = []
    print(sentence)
    for i in range(len(sentence)-1) :
        if sentence[i].isdigit() :
            if not sentence[i+1].isdigit() and not sentence[i+1] in ["and",","]:
                temp.append(sentence[i])
            else :
                temp.append(sentence[i:i+2])
    print("==>",temp)
    print('\n')

the output is:

['It', 'is', 'a', 'paradisematic', 'country', ',', 'in', 'which', 'roasted', 'parts', 'of', 'sentences', 'fly', 'into', 'your', 'mouth', '20', '2', '.']
==> [['20', '2'], '2']

['She', 'packed', 'her', '7', 'versalia', ',', 'put', 'her', 'initial', 'into', 'the', 'belt', 'and', 'made', 'herself', '40', '4', 'on', 'the', 'way', '.']
==> ['7', ['40', '4'], '4']

['When', 'she', 'reached', 'the', 'first', 'hills', 'of', 'the', 'Italic', 'Mountains', ',', 'she', 'had', 'a', 'last', 'view', '3', '00', 'and', '3', 'back', 'on', 'the', 'skyline', 'of', 'her', 'hometown', 'Bookmarksgrove', ',', '2', '000', 'and', '20', '1', 'the', 'headline', 'of', 'Alphabet', 'Village', 'and', 'the', '2', '000', ',', '20', '1', 'subline', 'of', 'her', 'own', 'road', ',', 'the', 'Line', 'Lane', '.']
==> [['3', '00'], ['00', 'and'], '3', ['2', '000'], ['000', 'and'], ['20', '1'], '1', ['2', '000'], ['000', ','], ['20', '1'], '1']

I also tried:

for sentence in listToks :
    temp = []
    print(sentence)
    for i in range(len(sentence)-1) :
        if sentence[i].isdigit() :
            if not sentence[i+1].isdigit() and not sentence[i+1] in ["and",","]:
                temp.append(sentence[i])
            else :
                while True :
                    temp2 =[]
                    temp2.append(sentence[i])
                    i+=1
                    if not sentence[i+1].isdigit() and not sentence[i+1] in ["and",","]:
                        break
                temp.append(temp2)         
    print("==>",temp)
    print('\n')

and the output is:

['It', 'is', 'a', 'paradisematic', 'country', ',', 'in', 'which', 'roasted', 'parts', 'of', 'sentences', 'fly', 'into', 'your', 'mouth', '20', '2', '.']
==> [['20'], '2']


['Even', 'the', 'all', 'powerful', 'Pointing', 'has', 'no', 'control', 'about', 'the', 'blind', 'texts', 'it', 'is', 'an', 'almost', 'unorthographic', 'life', '1', 'day', 'however', 'a', 'small', 'line', 'of', 'blind', 'text', 'by', 'the', 'name', 'of', 'Lorem', 'Ipsum', 'decided', 'to', 'leave', 'for', 'the', 'far', 'World', 'of', 'Grammar', '.']
==> ['1']

['She', 'packed', 'her', '7', 'versalia', ',', 'put', 'her', 'initial', 'into', 'the', 'belt', 'and', 'made', 'herself', '40', '4', 'on', 'the', 'way', '.']
==> ['7', ['40'], '4']

['When', 'she', 'reached', 'the', 'first', 'hills', 'of', 'the', 'Italic', 'Mountains', ',', 'she', 'had', 'a', 'last', 'view', '3', '00', 'and', '3', 'back', 'on', 'the', 'skyline', 'of', 'her', 'hometown', 'Bookmarksgrove', ',', '2', '000', 'and', '20', '1', 'the', 'headline', 'of', 'Alphabet', 'Village', 'and', 'the', '2', '000', ',', '20', '1', 'subline', 'of', 'her', 'own', 'road', ',', 'the', 'Line', 'Lane', '.']
==> [['and'], ['and'], '3', ['20'], ['20'], ['20'], '1', ['20'], ['20'], ['20'], '1']

What I want is something like:

[['7'], ['40', '4']]
[['3', '00', 'and', '3'],['2', '000', 'and', '20', '1']]
etc.

The idea is to recreate the numbers: 20+1 = 21, 2+000=2000, 21+2000= 2021
Thank you

Can anyone figure out why my loop counter isn't working during my while statement (Itterating) . Loop should equal nine when running

Can anyone figure out why my loop counter isn't working during my while statement (Iterating) . Loop should equal nine when running . I Also should see skipping - 5 when running because it is less than 0

values = [ 3, 53, 19, -5, 60, 41, "test", 5, 2, 9, 22]

#create a loop using a for or while

for i in values:

    # Use one or more if-elif-else conditional statements within the loop

    if i == 9:
      # Use a break statement
      break
    elif isinstance(i, str):
      # Use a continue statement
      continue
    else:
      print(i)

while loop example

total = 0
loop = 0
while total < 200:
   if isinstance(values[loop], str):
       loop +-1
       continue
   elif values[loop] > 0:
       total += values[loop]
   else:
    print(f'skipping {values[loop]}')

    if loop == len(values)-1:
      break
    loop += 1

print(f'sum: {total}, loop: {loop}')

Terminal After Running Code

3
53
19
-5
60
41
5
2
sum: 201, loop: 0

R - ifelse statement with two conditions

I'm new to R and want to understand why "ifelse" fails in my case.

I have a vector of longitudes and radii. If the lons are < 0 then the code should do lons - (zrad/dxy), if lons are >= 0 then lons + (zrad/dxy) should be calculated.

Some example data:

lons <- c(-80,4)
zrad <- c(747,576)
dxy  <- 222
test <- ifelse(lons < 0 | lons >= 0, lons - (zrad/dxy), lons + (zrad/dxy))
View(test)

This gives me

1   -83.364865
2   1.405405

However, lons - (zrad/dxy) is

-83.364865  1.405405 

and lons + (zrad/dxy) is

-76.635135  6.594595

But the right result should be

-83.364865 6.594595

Do I need to use a for-loop so that "test" includes the result from the first and second calculation? Or is there an easier way to differentiate the two cases(conditions)?

How to create a variable based on specific condition?

I would like to create a variable based on specific condition. I have a text variable description and I tried to create a loop based on this variable. What I'm trying to do is :

If word1 and word2 in description ont the first row, then append classe with word1 and word2 .

If word3 is contained alone for the second row, then append classe with word3 and so on. And if there none of the three word then append with Others.

Here's the loop I did so far :

   container = ("word1", "word2", "word3")
    
    classe = []
    
    for i in range(0, df.shape[0]):
        for n in container:
            if [df['description'].str.contains(n).any()]:
                      classe.append(n)
                     
        else :
           
            classe.append('Other')

And I would like something like that :

outputdesired

But I got this weird result instead, not at all what I want :

classe

['word1',
 'word2'
 'word3',
 'Others',
 'word1',
 'word2'
 'word3',
 'Others',
 'word1',
 'word2'
 'word3',
 'Others',
 ...]

I obtained a shape for classe of 3040 or df.shape[0] is equal to 608, so there is a trouble because I need to have the same. I need a shape of 608 for classe. Something's wrong.

Any idead how to fix that ? Or if you have another solution, I'm also interested :)

Thanks.

How can I keep file pointer in the If clause

Could anybody advice me if there is any way to keep file pointer in the nested IF clause.

I have to parse the file, and based on its content, different code blocks should process the file. I came up with nested IF loops.

enter code here import re

with open('smartctl.txt', 'r') as file: line = file.readlines()

for x in line:
    matchIP = re.search('Checking', x)
    if matchIP:
        print(x)
    Match_Micron55 = re.search('Micron_5100', x)
    Match_Intel = re.search('INTEL', x)
    Match_Micron600 = re.search('Micron_M600', x)
    Any_Micron = re.search('Micron', x)
    if Match_Micron55:
        print("here we have Micron55")

    elif Match_Intel:
        print("here we have Intel")

    elif Match_Micron600:
        print('here we have Micron 600')
        mline = line
        print("file is open")
        check = ""
        for y in mline:
            if y == x:
                check == True
                continue
            if y.startswith('  1 Raw_Read_Error_Rate') and check == True:
                print(y)
                continue
            if y.startswith('  5 Reallocated_Sector_Ct') and check == True:
                print(y)
                continue
            if y.startswith('173 Unknown_Attribute') and check == True:
                print(y)
                break
    elif Any_Micron:
        print('Here we have unsorted values')
enter code here

As you can see I read the file as line. Then I go with variable X through the file. Then, when I fall in IF condition, I have to CONTINUE reading the file: that's keep the file pointer and continue reading exactly from the place I went into the IF loop. I c

The method seems to be non working. Apart from creating Y variable I have also tryed ussing X in the nested IF clause but was not succeed. The x (line) vatiable seems to not keep its value on the second IF entry.

How to disable or ignore code if input is null in scss

I have code in my _dip.scss file like that :

#g-dip {
    @if $dip-background-style == 'bg-color' {
        background: $dip-bg-color;
    }
}

it's make background color with selected color from color input field if color option enabled. If no color selected from color input field in color mode, then it's make css code like that :

background: false;

How to disable

background: $dip-bg-color;

code if color mode enabled but no color selected from color input field?