标签: enumerate

有没有办法从Directory.EnumerateFiles中的异常中恢复?

在.NET 4中,这个带有递归的Directory.EnumerateFiles()方法看起来很方便.
但是,如果在递归中发生异常,我如何继续/从中恢复并继续枚举其余文件?

try
{
  var files = from file in Directory.EnumerateFiles("c:\\",
                           "*.*", SearchOption.AllDirectories)
              select new
              {
                File = file
              };

  Console.WriteLine(files.Count().ToString());

}
catch (UnauthorizedAccessException uEx)
{
  Console.WriteLine(uEx.Message);
}
catch (PathTooLongException ptlEx)
{
  Console.WriteLine(ptlEx.Message);
}
Run Code Online (Sandbox Code Playgroud)

.net linq exception enumerate

5
推荐指数
1
解决办法
2620
查看次数

如何枚举无上下文语法的字符串?

您使用什么算法来枚举由无上下文语法生成的字符串?

当没有递归时似乎可行,但我无法弄清楚如何在一般情况下这样做,它可能包含各种(可能是间接的)递归.

(我不是在寻找像本页那样的深奥解决方案;我正在寻找一种可以映射到标准命令式代码的算法.)

grammar enumerate context-free-grammar

5
推荐指数
1
解决办法
1706
查看次数

Scala mkString除了最后一个

我想在scala中执行以下操作:

val l = List("An apple", "a pear", "a grapefruit", "some bread")
... some one-line simple function ...
"An apple, a pear, a grapefruit and some bread"
Run Code Online (Sandbox Code Playgroud)

用这种方式编写它的最短方法是什么?

到目前为止,我最好的尝试是:

def makeEnumeration(l: List[String]): String = {
  var s = ""
  var size = l.length
  for(i <- 0 until size) {
    if(i > 0) {
      if(i != size - 1) { s += ", "
      } else s += " and "
    }
    s += l(i)
  }
  s
}
Run Code Online (Sandbox Code Playgroud)

但这很麻烦.任何的想法?

scala enumerate punctuation

5
推荐指数
1
解决办法
691
查看次数

通过循环遍历未知数量的数组来创建 JavaScript 对象的笛卡尔积(powerset?)

我是初学者,所以如果这是微不足道的,请原谅我的无知。

我有一个未知长度的 javascript 对象,每个属性的值都是一个数组(对我来说也是未知长度)。例如:

var obj = {"varA":[1,2,3],
           "varB":['good','bad'],
           "varC":[0,100],
           "varD":['low','med','high']
          }
Run Code Online (Sandbox Code Playgroud)

我想遍历每个属性并为每个属性值组合创建一个新对象。如果我知道属性的数量,我可以强制使用 for 循环,但是有没有一种方法可以在不知道要硬编码多少个循环的情况下进行枚举?

我基本上想做这样的事情:

var oblist = [];
for (a in varA){
 for (b in varB){
  for (c in varC){
   for (d in varD){
    oblist.push({"varA":varA[a], "varB":varB[b], "varC":varC[c], "varD":varD[d]});
   }
  }
 }
}
Run Code Online (Sandbox Code Playgroud)

这样oblist将包含以下对象:

{"varA":1, "varB":"good", "varC":0, "varD":"low"}
{"varA":1, "varB":"good", "varC":0, "varD":"med"}
...
{"varA":3, "varB":"bad", "varC":100, "varD":"high"}
Run Code Online (Sandbox Code Playgroud)

谢谢!

编辑:看,我不是在寻求 for 循环或索引语法帮助。我在问如果我不知道对象中的属性数量怎么办(例如 varA、varB、varC、varD、varE,我知道我可以拥有 varZZ),所以我不能只是努力-代码 4 for 循环。有没有办法使用 obj[Object.keys(obj)[i]].length 设置它?

javascript arrays enumerate cartesian powerset

5
推荐指数
1
解决办法
855
查看次数

在现代C++中,有没有相当于python中基于范围的`enumerate`循环?

有没有相当于enumerateC++中python 的基于范围的循环?我会想象这样的事情.

enumerateLoop (auto counter, auto el, container) {
    charges.at(counter) = el[0];
    aa.at(counter) = el[1];
}
Run Code Online (Sandbox Code Playgroud)

可以使用模板或宏来完成吗?

我知道我可以使用旧学校循环并迭代直到我到达container.size().但我很感兴趣如何使用模板或宏来解决这个问题.

编辑

在评论中提示后,我使用了增强迭代器.我使用C++ 14获得了另一个有效的解决方案.

template <typename... T>
auto zip(const T &... containers) -> boost::iterator_range<boost::zip_iterator<
decltype(boost::make_tuple(std::begin(containers)...))>> {
  auto zip_begin =
    boost::make_zip_iterator(boost::make_tuple(std::begin(containers)...));
  auto zip_end =
    boost::make_zip_iterator(boost::make_tuple(std::end(containers)...));
  return boost::make_iterator_range(zip_begin, zip_end);
}

template <typename T>
auto enumerate(const T &container) {
return zip(boost::counting_range(0, static_cast<int>(container.size())),
container);
} 
Run Code Online (Sandbox Code Playgroud)

https://gist.github.com/kain88-de/fef962dc1c15437457a8

c++ enumerate c++14

5
推荐指数
1
解决办法
1850
查看次数

枚举的复杂性

我看到很多关于建的方法Python的的运行时间复杂度的问题,有很多答案了很多的方法(例如https://wiki.python.org/moin/TimeComplexity,HTTPS:/ /www.ics.uci.edu/~pattis/ICS-33/lectures/complexitypython.txt,LEN()的开销功能,等)

我没有看到任何枚举的内容.我知道它至少返回一个新数组(索引),但生成它需要多长时间,而另一个数组只是原始数组?

换句话说,我假设它是O(n)用于创建新数组(迭代)和O(1)用于重用原始数组...总共O(n)(我认为).副本的另一个O(n)是否为O(n ^ 2),或其他什么......?

python arrays big-o enumerate time-complexity

5
推荐指数
3
解决办法
3679
查看次数

\table 中的 \enumerate 乳胶环境

我想在 \enumerate 环境中插入一个 \table ,我有这样的事情:

\begin{enumerate}
    \item Item 1.
    \item Item 2.
    \item \begin{table}[htbp]
        \textbf{\caption{Table1 Caption}}
        \centering  
        \begin{tabular}{c c}
        \hline\hline
        Value 1 & Value 2\\
        \hline
        r1c1 & r2c2\\
        r2c1 & r2c2\\
        r3c1 & r3c2\\           
        \hline
        \end{tabular}
        \label{table1}
        \end{table}

    \item \begin{table}[htbp]
        \textbf{\caption{Table 2 Caption}}
        \centering
        \begin{tabular}{ccc}
        \hline\hline
        Value 1 & Value 2 & Value 3\\
        \hline
        r1c1 & r1c2 & r1c3\\
        r2c1 & r2c2 & r2c3\\
        r3c1 & r3c2 & r3c3\\
        \end{tabular}
        \label{table2}
    \end{table}

    \item \ref{table1} and \ref{table2}
\end{enumerate}
Run Code Online (Sandbox Code Playgroud)

但是当我编译乳胶文档时,\enumerate …

latex enumerate

5
推荐指数
1
解决办法
9086
查看次数

如何在枚举列表中的项目之间放置数字?

我有一个枚举列表,有些项目有数字。我这样写:

\begin{enumerate}
    \item Estado da arte: 
    \item Levantar os requisitos
    \item Com o microcontrolador
\ref{figurametodo3}.    
    \begin{figure}[h!]
        \begin{center}
            \includegraphics[scale=0.6]{./dados/figuras/metodo_3}
            \caption{Sistema para leitura da identificação de uma Tag} 
            \label{figurametodo3}
        \end{center}
    \end{figure}


    \item Estudar

    \begin{figure}[h]
    \begin{center}
        \includegraphics[scale=0.4]{./dados/figuras/metodo_4}
        \caption{Comunicação entre o microcontrolador e o celular} 
        \label{figurametodo4}
    \end{center}
\end{figure}

    \item Desenvolver 

    \begin{figure}[h]
    \begin{center}
        \includegraphics[scale=0.6]{./dados/figuras/metodo_final}
        \caption{Comunicação entre celulares e servidor} 
        \label{figura22}
    \end{center}
\end{figure}

\end{enumerate}
Run Code Online (Sandbox Code Playgroud)

但它将列表下方的所有数字对齐,超出了我想要的位置。我希望我的数字保持在你的项目之下。名单内。

latex list enumerate

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

从无限列表中取出所有长度为4的子字符串

我对Haskell还是很陌生,并且正在尝试解决以下问题:

我有一个函数,该函数生成无限长的字符串,这些字符串的长度不同。但是一定长度的字符串数量受到限制。

现在,我要提取具有一定长度n的列表的所有子字符串。不幸的是,我做了很多研究,尝试了很多东西,但对我没有任何帮助。

我知道这filter()行不通,因为它会检查列表的每个部分并导致无限循环。

这是我生成无限列表的函数:

allStrings =  [ c : s | s <- "" : allStrings, c <- ['R', 'T', 'P']]
Run Code Online (Sandbox Code Playgroud)

我已经尝试过了:

allStrings = [x | x <- [ c : s | s <- "" : allStrings, 
                  c <- ['R', 'T', 'P']], length x == 4] 
Run Code Online (Sandbox Code Playgroud)

并没有终止。

谢谢你的帮助!

haskell functional-programming list enumerate infinite

5
推荐指数
1
解决办法
94
查看次数

枚举导致 mypy 类型不兼容错误

下面的代码:

from typing import Union


def process(actions: Union[list[str], list[int]]) -> None:
    for pos, action in enumerate(actions):
        act(action)


def act(action: Union[str, int]) -> None:
    print(action)
Run Code Online (Sandbox Code Playgroud)

生成 mypy 错误: Argument 1 to "act" has incompatible type "object"; expected "Union[str, int]"

但是,当删除枚举函数时,输入就可以了:

from typing import Union


def process(actions: Union[list[str], list[int]]) -> None:
    for action in actions:
        act(action)


def act(action: Union[str, int]) -> None:
    print(action)
Run Code Online (Sandbox Code Playgroud)

有谁知道枚举函数正在做什么来影响类型?这是 python 3.9 和 mypy 0.921

python typing enumerate mypy

5
推荐指数
1
解决办法
2072
查看次数