附加到不存在的列表 [英] append to non-existing list

查看:59
本文介绍了附加到不存在的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


如果我这样做:


for sqlsth中的行:

________ pkcolumns.append (row [0] .strip())

________etc
没有事先的



pkcolumns = [];

我在第一次迭代时遇到这个错误:

UnboundLocalError:在分配之前引用的局部变量''pkcolums'


我猜那个'这是正常的,因为它是python的工作方式......?!?


我的问题是:是否无法附加到不存在的列表中?


我懒得首先宣布它,恕我直言它臃肿的代码,并且(不要知道,如果在这里说的话,我会知道)来自(php)我用过

不需要它...

问候,

Yves

解决方案

您好


我认为没有办法追加到现有的清单。

对我的问题感到抱歉,但英语是我的第二语言d我

我不知道IHMO(或恕我直言)的含义是什么。我用Google搜索并发现

,这意味着在我的谦卑的意见中,这是真的吗?谢谢并接受

我的抱怨并不是完全谈论你的问题。


Daniel


我担心你要么回到php或者其他任何编程

语言,以适应你的风格或改变你的风格以适应python。


有很多我不喜欢python但如果你必须使用它,你需要b
$ b必须应对它。


Yves Glodt写道:

我的问题是:有没有办法追加到不存在的列表?

我懒得首先声明它,恕我直言它臃肿的代码,并且(不要我知道在这里说是不是很好我来自哪里(php)我用过
不需要它...

问候,Yves




Yves Glodt写道:

你好,
如果我这样做:

对于sqlsth中的行:
________ pkcolumns.append(row [0] .s行程())
________ iec
没有事先的


pkcolumns = [];

我在第一次迭代时收到此错误:
UnboundLocalError:在赋值之前引用局部变量''pkcolums'

我想这是正常的,因为它是python的工作方式......?!?

我的问题是:有没有办法追加到现有的清单?

我懒得先申报它,恕我直言,它会使代码膨胀,而且(不要)知道我这里说的好吗(我)来自哪里(php)我用过
不需要它......

问候,
Yves




你的意思是你要输入pkcolumns只有一次保持你的代码简短?

这样的东西会有用吗?


pkcolumns = [row.strip()for sqlsth中的行]


Hello,

if I do this:

for row in sqlsth:
________pkcolumns.append(row[0].strip())
________etc
without a prior:

pkcolumns = [];
I get this error on first iteration:
UnboundLocalError: local variable ''pkcolums'' referenced before assignment

I guess that''s normal as it''s the way python works...?!?

My question is: Is there no way to append to a non existing list?

I am lazy for declaring it first, IMHO it bloats the code, and (don''t
know if it''s good to say that here) where I come from (php) I was used
to not-needing it...
regards,
Yves

解决方案

Hi

I think there''s no way to append to a non existing list.

Sorry about my question, but the English is my second language, and I
don''t know what is the meaning of IHMO (or IMHO). I googled and found
that it means "In My Humbled Opinion", is that true? Thanks and accept
my appologies for not talking entirely about your problem.

Daniel


I am afraid you have to either go back to php or whatever programming
language that fits your style or change your style to fit python.

There is a lot I don''t like about python but if you have to use it, you
have to cope with it.

Yves Glodt wrote:

My question is: Is there no way to append to a non existing list?

I am lazy for declaring it first, IMHO it bloats the code, and (don''t
know if it''s good to say that here) where I come from (php) I was used
to not-needing it...
regards,
Yves




Yves Glodt wrote:

Hello,

if I do this:

for row in sqlsth:
________pkcolumns.append(row[0].strip())
________etc
without a prior:

pkcolumns = [];
I get this error on first iteration:
UnboundLocalError: local variable ''pkcolums'' referenced before assignment

I guess that''s normal as it''s the way python works...?!?

My question is: Is there no way to append to a non existing list?

I am lazy for declaring it first, IMHO it bloats the code, and (don''t
know if it''s good to say that here) where I come from (php) I was used
to not-needing it...
regards,
Yves



You mean you want to type "pkcolumns" only once to keep your code short?
Would something like this be useful?

pkcolumns = [row.strip() for row in sqlsth]


这篇关于附加到不存在的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆