标签: for-loop

Java:for(;;)vs. while(true)

标准while(true)循环和for(;;)?之间有什么区别?

编译后是否存在或将两者都映射到相同的字节码?

java for-loop while-loop

60
推荐指数
4
解决办法
2万
查看次数

在Pandas中有一种方法可以在dataframe.apply中使用先前的行值吗?

我有以下数据帧:

 Index_Date    A    B    C    D
 ===============================
 2015-01-31    10   10   Nan  10
 2015-02-01     2    3   Nan  22 
 2015-02-02    10   60   Nan  280
 2015-02-03    10   100   Nan  250
Run Code Online (Sandbox Code Playgroud)

要求:

 Index_Date    A    B    C    D
 ===============================
 2015-01-31    10   10   10   10
 2015-02-01     2    3   23   22
 2015-02-02    10   60   290  280
 2015-02-03    10   100  3000 250
Run Code Online (Sandbox Code Playgroud)

Column C导出用于2015-01-31通过取valueD.

然后,我需要使用valueC用于2015-01-31和乘以valueA2015-02-01添加B.

我尝试了一个apply …

python iteration for-loop dataframe pandas

60
推荐指数
5
解决办法
7万
查看次数

为什么在C ++ 17中基于范围的“ for”循环规范发生了变化?

我查看了一些丑陋的代码(在迭代时修改了基础序列),并探索了基于范围的for循环的定义,我去了cppreference

在那里,我注意到了一些奇怪的事情:

基于范围的for循环在C ++ 17中已更改,但我看不到更改的原因,并且代码对我来说看起来相同(只是“重构”)。因此,旧的是:

{
  auto && __range = range_expression;
  for (auto __begin = begin_expr, __end = end_expr; __begin != __end; ++__begin) {
    range_declaration = *__begin;
    loop_statement
  }
} 
Run Code Online (Sandbox Code Playgroud)

新的是

{
  auto && __range = range_expression;
  auto __begin = begin_expr;
  auto __end = end_expr;
  for ( ; __begin != __end; ++__begin) {
    range_declaration = *__begin;
    loop_statement
  }
} 
Run Code Online (Sandbox Code Playgroud)

为什么要进行此更改,并且它会使任何合法的C ++ 14程序在C ++ 17中表现出未定义的行为(UB)?

c++ for-loop language-lawyer c++17

60
推荐指数
2
解决办法
5143
查看次数

C中完整的"for"循环语法是什么?

for在阅读其他人的代码时,我看到了一些非常奇怪的循环.我一直在尝试搜索for循环的完整语法解释,C但这很难,因为单词" for"出现在不相关的句子中,使得搜索几乎不可能有效地进行Google.

在阅读了这个让我再次好奇的帖子后,我想起了这个问题.

for这里:

for(p=0;p+=(a&1)*b,a!=1;a>>=1,b<<=1);
Run Code Online (Sandbox Code Playgroud)

在中间条件下有一个逗号分隔两段代码,这个逗号有什么作用?右边的逗号我理解,因为它使得a>>=1b<<=1.

但是在循环退出条件下,会发生什么?它是在何时p==0,何时a==1或何时发生时退出?

如果有人能帮助我理解这一点并且可能指向完整for循环语法描述的方向,那将是很棒的.

c syntax for-loop

59
推荐指数
3
解决办法
9万
查看次数

将数组的所有元素初始化为相同的数字

前段时间,我的老教师发布了这段代码,说这是将数组初始化为相同数字的另一种方法(当然不是零).

在这种情况下三个.

他说这种方式比for循环要好一些.为什么我需要左移操作符?为什么我需要另一个长数组呢?我不明白这里发生了什么.

int main() {

    short int A[100];

    long int v = 3;
    v = (v << 16) + 3;
    v = (v << 16) + 3;
    v = (v << 16) + 3;
    long *B = (long*)A;

    for(int i=0; i<25; i++)
        B[i] = v;

    cout << endl;
    print(A,100);
}
Run Code Online (Sandbox Code Playgroud)

c++ arrays for-loop

59
推荐指数
6
解决办法
4984
查看次数

在LaTeX中迭代

我想使用一些迭代控制流来简化以下LaTeX代码.

  \begin{sidewaystable}
  \caption{A glance of images}
  \centering
  \begin{tabular}{| c ||c| c| c |c| c|| c |c| c|c|c| }
  \hline
  \backslashbox{Theme}{Class} &\multicolumn{5}{|c|}{Class 0} & \multicolumn{5}{|c|}{Class 1}  \\
  \hline

  \hline
          1 &
          \includegraphics[scale=2]{../../results/1/0_1.eps}
          &\includegraphics[scale=2]{../../results/1/0_2.eps}
          &\includegraphics[scale=2]{../../results/1/0_3.eps}
          &\includegraphics[scale=2]{../../results/1/0_4.eps}
          &\includegraphics[scale=2]{../../results/1/0_5.eps}
          &\includegraphics[scale=2]{../../results/1/1_1.eps}
          &\includegraphics[scale=2]{../../results/1/1_2.eps}
          &\includegraphics[scale=2]{../../results/1/1_3.eps}
          &\includegraphics[scale=2]{../../results/1/1_4.eps}
          &\includegraphics[scale=2]{../../results/1/1_5.eps}      \\
  \hline

  ...  % similarly for 2, 3, ..., 22

  \hline
          23 &
          \includegraphics[scale=2]{../../results/23/0_1.eps}
          &\includegraphics[scale=2]{../../results/23/0_2.eps}
          &\includegraphics[scale=2]{../../results/23/0_3.eps}
          &\includegraphics[scale=2]{../../results/23/0_4.eps}
          &\includegraphics[scale=2]{../../results/23/0_5.eps}
          &\includegraphics[scale=2]{../../results/23/1_1.eps}
          &\includegraphics[scale=2]{../../results/23/1_2.eps}
          &\includegraphics[scale=2]{../../results/23/1_3.eps}
          &\includegraphics[scale=2]{../../results/23/1_4.eps}
          &\includegraphics[scale=2]{../../results/23/1_5.eps}      \\
  \hline

  \end{tabular}
  \end{sidewaystable}
Run Code Online (Sandbox Code Playgroud)

我了解到forloop包提供了for循环.但我不确定如何将它应用于我的案例?还是其他不通过forloop的方法?


如果我还想要另一个类似的情况,唯一的区别是目录不是从1,2到23运行,而是以某种任意顺序,如3,2,6,9,......,甚至一个字符串列表,如dira,dirc,dird,dirb,....如何将LaTeX代码转换为循环呢?

iteration latex for-loop

58
推荐指数
2
解决办法
8万
查看次数

继续循环

我有以下代码

For x = LBound(arr) To UBound(arr)

    sname = arr(x)  
    If instr(sname, "Configuration item") Then  
        '**(here i want to go to next x in loop and not complete the code below)**  

    '// other code to copy past and do various stuff

Next x  
Run Code Online (Sandbox Code Playgroud)

所以我认为我可以简单地使用该语句Then Next x,但这会给出"no for statement声明"错误.

那么我可以在If instr(sname, "Configuration item") Then它进入x的下一个值之后放入什么呢?

vba loops for-loop next

58
推荐指数
5
解决办法
19万
查看次数

Python迭代对象属性

如何在Python中迭代对象的属性?

我有一节课:

class Twitt:
    def __init__(self):
        self.usernames = []
        self.names = []
        self.tweet = []
        self.imageurl = []

    def twitter_lookup(self, coordinents, radius):
        cheese = []
        twitter = Twitter(auth=auth)
        coordinents = coordinents + "," + radius
        print coordinents
        query = twitter.search.tweets(q="", geocode=coordinents, rpp=10)
        for result in query["statuses"]:
            self.usernames.append(result["user"]["screen_name"])
            self.names.append(result['user']["name"])
            self.tweet.append(h.unescape(result["text"]))
            self.imageurl.append(result['user']["profile_image_url_https"])
Run Code Online (Sandbox Code Playgroud)

现在我可以通过这样做获取我的信息:

k = Twitt()
k.twitter_lookup("51.5033630,-0.1276250", "1mi")
print k.names
Run Code Online (Sandbox Code Playgroud)

我想能够做的是迭代for循环中的属性,如下所示:

for item in k:
   print item.names
Run Code Online (Sandbox Code Playgroud)

python for-loop object python-2.7

58
推荐指数
3
解决办法
10万
查看次数

为什么在for循环中允许任意目标表达式?

我不小心写了这样的代码:

foo = [42]
k = {'c': 'd'}

for k['z'] in foo:  # Huh??
    print k
Run Code Online (Sandbox Code Playgroud)

但令我惊讶的是,这不是语法错误.相反,它打印{'c': 'd', 'z': 42}.

我的猜测是代码翻译成字面意思:

i = iter(foo)
while True:
    try:
        k['z'] = i.next()  # literally translated to assignment; modifies k!
        print k
    except StopIteration:
        break
Run Code Online (Sandbox Code Playgroud)

但是......为什么语言允许这样做?我希望在for-stmt的目标表达式中只允许使用单个标识符和标识符元组.是否存在实际有用的情况,而不仅仅是一个奇怪的问题?

python for-loop language-lawyer

58
推荐指数
4
解决办法
2899
查看次数

这段代码中list [:]的含义是什么?

此代码来自Python的文档.我有点困惑.

words = ['cat', 'window', 'defenestrate']
for w in words[:]:
    if len(w) > 6:
        words.insert(0, w)
print(words)
Run Code Online (Sandbox Code Playgroud)

以下是我最初的想法:

words = ['cat', 'window', 'defenestrate']
for w in words:
    if len(w) > 6:
        words.insert(0, w)
print(words)
Run Code Online (Sandbox Code Playgroud)

为什么这段代码创建了一个无限循环而第一个没有?

python iteration for-loop list

58
推荐指数
3
解决办法
2万
查看次数