loops

Can I call a function inside a Lambda expression in python

情到浓时终转凉″ 提交于 2021-02-10 15:52:10
问题 I have a function with including if, else condition and for loop. I want to write this function inside a lambda expression. I tried from many ways to create this lambda function. But still I couldn't do it. This is my function with another rules. negation ='no,not,never'.split(',') list2 = 'miss,loss,gone,give up,lost'.split(',') def f(sentence): s = sentence.split() l = [s.index(word) for word in s if word in list2] # Will returns list of indices (of sentence) where word is in list2 if len(l

SAS Macro Do LOOP

家住魔仙堡 提交于 2021-02-10 15:19:46
问题 I want to have a sas macro loop by even years, so the do loop would jump from 2006 to 2008 to 2010...all the way to 2018 and not from 2006 to 2007. When I do a %by = 2, SAS doesn't work and gives me an error message. What is the best solution? I have the following Code: %macro import; %do I = 2006 %to 2018; data BTI&I; set edited.Bti_&I; year=&I; run; %end; %mend import; %import; 回答1: Add the %by 2 keyword to increment intervals of 2. I would also recommend passing the start and end years as

Computing points for a football team

别说谁变了你拦得住时间么 提交于 2021-02-10 14:59:47
问题 everyone. I am trying to calculate football team points. I have my dataframe "df.work" with rownames = teamnames: rownames(df.work) <- c(as.vector(teams$HomeTeam)) I have imported all played matches from another dataframe. And for every team I try to execute if loop based on subset of team matches. First I filter all home matches. So my code is for (Team1 in row.names.data.frame(df.work)){ a1 <- filter(df.clean, HomeTeam == Team1) if (a1$home.goals > a1$away.goals) { df.work[Team1,"Points"] <

Computing points for a football team

亡梦爱人 提交于 2021-02-10 14:59:30
问题 everyone. I am trying to calculate football team points. I have my dataframe "df.work" with rownames = teamnames: rownames(df.work) <- c(as.vector(teams$HomeTeam)) I have imported all played matches from another dataframe. And for every team I try to execute if loop based on subset of team matches. First I filter all home matches. So my code is for (Team1 in row.names.data.frame(df.work)){ a1 <- filter(df.clean, HomeTeam == Team1) if (a1$home.goals > a1$away.goals) { df.work[Team1,"Points"] <

Logical operators in if-else in R

霸气de小男生 提交于 2021-02-10 06:48:39
问题 I have the following table (5 columns and 3 rows) called mat : AC CA RES 1 0 2 2 1 3 0 0 0 1 The operation being performed is mat[1]/mat[1]+mat[2] I am testing for the following : 1) If both columns of a row are zero, the result should be NA. 2) If only one column of a row is zero and the other has a non-zero number, proceed with calculation. (Even if the numerator is 0, that's okay) I am currently using this if-else structure within a for-loop going row-wise: if(mat[b,1]>=0|mat[b,2]>=0){mat

Loops and Collatz Conjecture

北城余情 提交于 2021-02-10 06:14:34
问题 I have a problem with loops and declaring variables. currently I am making a program about Collatz Conjecture, the program should check what is the biggest steps to reach one from certain amount of Collatz Sequence. here's my code : start_num = int(input("insert a starting Number > ")) how_many = int(input("how many times you want to check? >")) def even_or_odd(number): if number % 2 == 0: return 'isEven' else: return 'notEven' def collatz(n): z = n counter = 0 while True: if n != 1: if even

Alternative for sample

Deadly 提交于 2021-02-10 05:29:05
问题 I have the following sample code that uses sapply which takes long to process (since executed many times): samples = sapply(rowIndices, function(idx){ sample(vectorToDrawFrom, 1, TRUE, weights[idx, ]) }) The issue is that I have to draw from the weights which are in the matrix, dependent on the indices in rowIndices . Does somebody have a better idea in mind to draw from the rows of the matrix? Reproducable example: rowIndices = floor(runif(1000, 1, 100)) vectorToDrawFrom = runif(5000, 0.0, 2

Python loop through a text file reading data

别来无恙 提交于 2021-02-09 07:01:44
问题 I am new to python, and although I am sure this might be a trivial question, I have spent my day trying to solve this in different ways. I have a file containing data that looks like this: <string> <integer> <N1> <N2> data data ... <string> <integer> <N3> <N4> data data ... And that extends a number of times... I need to read the "data" which for the first set (between the first and second ) contains a number N1 of X points, a number N2 of Y points and a number N1*N2 of Z points. If I had

Python loop through a text file reading data

て烟熏妆下的殇ゞ 提交于 2021-02-09 06:57:25
问题 I am new to python, and although I am sure this might be a trivial question, I have spent my day trying to solve this in different ways. I have a file containing data that looks like this: <string> <integer> <N1> <N2> data data ... <string> <integer> <N3> <N4> data data ... And that extends a number of times... I need to read the "data" which for the first set (between the first and second ) contains a number N1 of X points, a number N2 of Y points and a number N1*N2 of Z points. If I had

Python loop through a text file reading data

吃可爱长大的小学妹 提交于 2021-02-09 06:57:09
问题 I am new to python, and although I am sure this might be a trivial question, I have spent my day trying to solve this in different ways. I have a file containing data that looks like this: <string> <integer> <N1> <N2> data data ... <string> <integer> <N3> <N4> data data ... And that extends a number of times... I need to read the "data" which for the first set (between the first and second ) contains a number N1 of X points, a number N2 of Y points and a number N1*N2 of Z points. If I had