- http://mail.python.org/mailman/listinfo / python-list 你没告诉我们你是怎么失败的,所以我猜不出是什么问题。 但是,你的代码对我有用: >> a = [[''1'',''''',[''3''],[''''',''''',''6''] ,['''',''8'',''9'','0'']] n = [] 对于k:a b $ b .... n.append([in(v)for v in k]) .... >> print n [[1,2],[3],[4,5,6],[7,8,9,0]] (A.虽然你好像混淆了变量b和n。) 加里赫伦 7月21日,3:20 * pm ,Gary Herron< gher ... @ islandtraining.comwrote: Samir写道: 嗨每个人, 我对Python比较新,所以请原谅我看似 a基本问题。 假设我有一个列表,一个由嵌套列表组成的字符串 整数表示,这样 a = [[''1'',''2''],[''3''],[''4'', ''''',''6'',[''7'',''8'',''9'',''0''] 我想将此转换为类似的列表,b,其中值 由整数表示,例如 b = [[1,2],[3],[4,5,6],[7,8,9,0]] 我没有成功尝试以下代码: n = [] for k in a: * * n.append([int(v)for v in k]) print n 有谁知道我做错了什么? 先谢谢。 Samir - http://mail.python.org/mailman/listinfo/python-list 你没告诉我们你是怎么失败的,所以我猜不出是什么问题。 然而,你的代码对我有用: *>> a = [[''1'',''2''],[''3''],[''''',''5 '',''6'',[''7'',''8'',''9'',''0''] *>> n = [] *>>对于k中的k: ... * * n.append([int(v)for v in k]) b $ b ... *>>打印n [[1,2],[3],[4,5,6] ],[7,8,9,0]] (虽然你似乎混淆了变量b和n。) Gary Herron-隐藏引用文字 - - 显示引用文字 - 加里, 感谢您的快速回复(并抱歉混淆了b和n)。 出于某种原因,我发布的逻辑似乎工作正常我正在使用 Python shell,但在我的代码中使用时,程序就会挂起。 它永远不会输出结果。以下是完整的代码。 我的indendentation有问题吗? a = n = [] t =""" 1 2 3 4 5 6 7 8 9 0 " " d = t.split(" \ n") for x in range(1,len(d) )-1): a.append(d [x] .split("")) 打印一个 $ b b中的k为$ b: n.append([in(v)for v in k]) print n 再次感谢。 Samir 7月22日上午6:11,Samir< spytho ... @ gmail.comwrote: [snip] 出于某种原因,我发布的逻辑似乎工作正常而我'在Python shell中使用了 ,但是在我的代码中使用时,程序就会挂起。 它永远不会输出结果。以下是完整的代码。 我的indendentation有问题吗? a = n = [] t =""" 1 2 3 4 5 6 7 8 9 0 " " d = t.split(" \ n") for x in range(1,len(d) )-1): a.append(d [x] .split("")) 打印一个 for a k: n.append([int(v)for v in k]) 看看是什么发生,插入一些打印报表,再加上一些东西 来减慢它的速度,例如 for k in a: print id( a), 打印ID(n),n n.append([int(v)for v in k]) raw_input(''点击Enter继续 - >'') > print n Hi Everyone,I am relatively new to Python so please forgive me for what seems likea basic question.Assume that I have a list, a, composed of nested lists with stringrepresentations of integers, such thata = [[''1'', ''2''], [''3''], [''4'', ''5'', ''6''], [''7'', ''8'', ''9'', ''0'']]I would like to convert this to a similar list, b, where the valuesare represented by integers, such asb = [[1, 2], [3], [4, 5, 6], [7, 8, 9, 0]]I have unsuccessfully tried the following code:n = []for k in a:n.append([int(v) for v in k])print nDoes anyone know what I am doing wrong?Thanks in advance.Samir 解决方案 Samir wrote:Hi Everyone,I am relatively new to Python so please forgive me for what seems likea basic question.Assume that I have a list, a, composed of nested lists with stringrepresentations of integers, such thata = [[''1'', ''2''], [''3''], [''4'', ''5'', ''6''], [''7'', ''8'', ''9'', ''0'']]I would like to convert this to a similar list, b, where the valuesare represented by integers, such asb = [[1, 2], [3], [4, 5, 6], [7, 8, 9, 0]]I have unsuccessfully tried the following code:n = []for k in a: n.append([int(v) for v in k])print nDoes anyone know what I am doing wrong?Thanks in advance.Samir-- http://mail.python.org/mailman/listinfo/python-listYou didn''t tell us how it failed for you, so I can''t guess what''s wrong.However, your code works for me:>>a = [[''1'', ''2''], [''3''], [''4'', ''5'', ''6''], [''7'', ''8'', ''9'', ''0'']]n = []for k in a:.... n.append([int(v) for v in k])....>>print n[[1, 2], [3], [4, 5, 6], [7, 8, 9, 0]](Although you seem to have confused variables b and n.)Gary HerronOn Jul 21, 3:20*pm, Gary Herron <gher...@islandtraining.comwrote:Samir wrote: Hi Everyone, I am relatively new to Python so please forgive me for what seems like a basic question. Assume that I have a list, a, composed of nested lists with string representations of integers, such that a = [[''1'', ''2''], [''3''], [''4'', ''5'', ''6''], [''7'', ''8'', ''9'', ''0'']] I would like to convert this to a similar list, b, where the values are represented by integers, such as b = [[1, 2], [3], [4, 5, 6], [7, 8, 9, 0]] I have unsuccessfully tried the following code: n = [] for k in a: * * n.append([int(v) for v in k]) print n Does anyone know what I am doing wrong? Thanks in advance. Samir -- http://mail.python.org/mailman/listinfo/python-listYou didn''t tell us how it failed for you, so I can''t guess what''s wrong.However, your code works for me:*>>a = [[''1'', ''2''], [''3''], [''4'', ''5'', ''6''], [''7'', ''8'', ''9'', ''0'']]*>>n = []*>>for k in a:... * *n.append([int(v) for v in k])...*>>print n[[1, 2], [3], [4, 5, 6], [7, 8, 9, 0]](Although you seem to have confused variables b and n.)Gary Herron- Hide quoted text -- Show quoted text -Hi Gary,Thanks for your quick response (and sorry about mixing up b and n).For some reason, the logic I posted seems to work ok while I''m usingthe Python shell, but when used in my code, the program just hangs.It never outputs the results. Below is the code in its entirety. Isthere a problem with my indendentation?a = n = []t = """1 234 5 67 8 9 0"""d = t.split("\n")for x in range(1,len(d)-1):a.append(d[x].split(" "))print afor k in a:n.append([int(v) for v in k])print nThanks again.SamirOn Jul 22, 6:11 am, Samir <spytho...@gmail.comwrote:[snip]For some reason, the logic I posted seems to work ok while I''m usingthe Python shell, but when used in my code, the program just hangs.It never outputs the results. Below is the code in its entirety. Isthere a problem with my indendentation?a = n = []t = """1 234 5 67 8 9 0"""d = t.split("\n")for x in range(1,len(d)-1): a.append(d[x].split(" "))print afor k in a: n.append([int(v) for v in k])To see what is happening, insert some print statements, plus somethingto slow it down e.g.for k in a:print id(a), aprint id(n), nn.append([int(v) for v in k])raw_input(''Hit Enter to continue ->'')>print n 这篇关于将String列表转换为Integer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!