Pylint说
W: 6: Using possibly undefined loop variable 'n'
使用此代码:
iterator = (i*i for i in range(100) if i % 3 == 0)
for n, i in enumerate(iterator):
do_something(i)
print n
Run Code Online (Sandbox Code Playgroud)
因为如果迭代器是空的(例如[]),则n未定义,好的.但我喜欢这个技巧.如何以安全的方式使用它?
我认为使用len(list(iterator))不是最佳选择,因为你必须做两个循环.使用计数器,并递增它我认为它不是非常pythonic.
python的新手,并试图学习文件i/o的绳索.
我正在使用以下格式从大型(200万行)文件中提取行:
56fr4
4543d
4343d
5irh3
Run Code Online (Sandbox Code Playgroud)
这是我用来返回代码的函数:
def getCode(i):
with open("test.txt") as f:
for index, line in enumerate(f):
if index == i:
code = # what does it equal?
break
return code
Run Code Online (Sandbox Code Playgroud)
一旦索引到达正确的位置(i),我使用什么语法来设置代码变量?
在matplotlib图中,我想用a),b),c)等列举所有(子)图.有没有办法自动执行此操作?
到目前为止,我使用了各个图表的标题,但这远非理想,因为我希望数字保持对齐,而可选的实际标题应该以图形为中心.
该函数choose(n,k)告诉我们k一组n不同元素存在多少个大小子集.假设我需要实际列出这些子集,我是如何创建它的?换句话说,我正在寻找一个接受矢量x(长度n)和数字的函数,k并返回一个矢量列表,每个矢量的大小k,子集的子集x.当然,列表的长度应该是choose(length(x),k).例如
enum.choose = function(x,k) {
# implementation should go here
{
enum.choose(1:3,2)
# should be:
# [[1]]
# 1 2
# [[2]]
# 1 3
# [[3]]
# 2 3
Run Code Online (Sandbox Code Playgroud) 我正在寻找一些有关我的代码的帮助,下面是严格的:
for file in file_name :
if os.path.isfile(file):
for line_number, line in enumerate(fileinput.input(file, inplace=1)):
print file
os.system("pause")
if line_number ==1:
line = line.replace('Object','#Object')
sys.stdout.write(line)
Run Code Online (Sandbox Code Playgroud)
我想修改一些以前提取的文件,以便用matplotlib绘制它们.为此,我删除一些行,评论其他人.
我的问题如下:
使用for line_number, line in enumerate(fileinput.input(file, inplace=1)):只给我5个以前提取的文件中的4个(当查看file_name列表包含5个引用时!)
使用for line_number, line in enumerate(file):给我5个先前提取的文件,但我不知道如何使用相同的文件进行修改而不创建另一个...
你对这个问题有什么看法吗?这是正常的问题吗?
我有一个由两个句子组成的字符串元组
a = ('What', 'happened', 'then', '?', 'What', 'would', 'you', 'like', 'to', 'drink','?')
Run Code Online (Sandbox Code Playgroud)
我试过这个
for i,j in enumerate(a):
print i,j
Run Code Online (Sandbox Code Playgroud)
这使
0 What
1 happened
2 then
3 ?
4 What
5 would
6 you
7 like
8 to
9 drink
10 ?
Run Code Online (Sandbox Code Playgroud)
而我需要的就是这个
0 What
1 happened
2 then
3 ?
0 What
1 would
2 you
3 like
4 to
5 drink
6?
Run Code Online (Sandbox Code Playgroud) 在.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) 我想在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)
但这很麻烦.任何的想法?
我想在 \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 …
我有一个枚举列表,有些项目有数字。我这样写:
\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)
但它将列表下方的所有数字对齐,超出了我想要的位置。我希望我的数字保持在你的项目之下。名单内。