更改str和列表名称之间的类型

时间:2017-06-21 14:46:48

标签: python

L1 = [1,2,3,4]
L2 = [5,6,7,8]
M1 = [9,10,11,12]        

a = input() #user would input '1' or '2'
b = input() #user would input 'M' or 'L'

c = b + a #the outcome would be either 'L1', 'L2' or 'M1'

有没有办法将字符串中的c类型更改为list。

3 个答案:

答案 0 :(得分:2)

$('td').click(function(){
   var row_index = $(this).closest("tr").index();
   var col_index = $(this).index();
});

String = list 然后String [0] = 1

答案 1 :(得分:0)

我认为您在变量c中存储了字符串答案,并且您想要访问具有相应名称的列表。有了这个目标,你可以使用python函数eval()

从python docs(here)中你得到了:

  

eval(表达式[,globals [,locals]])

     

表达式参数被解析并作为Python表达式进行求值   (技术上讲,条件列表)使用全局变量和本地变量   字典作为全局和本地命名空间。

这意味着在您的情况下,如果您的python名称空间包含具有此名称的变量,eval将获取您的字符串检查。结果如下:

c = 'L1'

print (eval(c)) # it will print the list L1 = [1,2,3,4]

上面的代码可以帮到您想要的地方。虽然我从未使用任何实际用例,但我必须与之合作。

Obs:感谢您重写问题! :)

答案 2 :(得分:0)

我认为您要做的是添加'li'+'st'以获得list您认为会调用list变量的list = [1,2,3,4] String = 'li'+'st' # then here you expect String = List

所以你想要的是

'li'+'st'

是的,当你'list' String = List时,你会得到String = List但你得到的字符串不是你之前存储的变量

如果您想制作Entity Framework,只需输入Public class Document { Public int Id { get; set; } Public Document Parent { get; set; } } 就不会有任何想象力那么简单

P.S。你应该选择更好的变量名,list可以在python中调用并调用一些String通常是不好的做法。例如,尝试使用lst和stg。