作为一种简单的方法来测试拉出大型tileset的某些元素的正确方法,在我的程序中作为2D列表输入,我将以下内容放入Python Shell:
->>>test = [ [1,2,3],[4,5,6],[7,8,9] ]
Run Code Online (Sandbox Code Playgroud)
在试图测试它时,我得到了这个:
->>>test1 = test[:][2]
->>>test1
[7, 8, 9]
Run Code Online (Sandbox Code Playgroud)
从输入test[:][2]我预期的test1到返回[3,6,9]
有人可以向我解释为什么我会这样做[7,8,9]吗?
另外,如果你能解释我将如何获得输出[3,6,9]?我知道我可以说以下内容:
>>>test1 = [ test[0][2], test[1][2], test[2][2] ]
Run Code Online (Sandbox Code Playgroud)
但如果我在我的程序环境中编写它,那条线就会更长,更难维护.如果有一种[3,6,9]比我上面列出的方式更容易获得的方式,我将非常感谢有关获得它的一些见解.
使用python 2.7.5
x = (raw_input)'''What will you Reply?
a. Nod
b. Answer
c. Stare
'''))
if x == "Nod" or "a" or "nod":
print '''You nod shyly.
"Oh that's Great"'''
elif x == "Answer" or "b" or "answer":
print '''You answer in a low tone.
"You can speak!"'''
elif x == "Stare" or "c" or "stare":
print '''you stare blankly, so does the AI.
"Well okay then."'''
Run Code Online (Sandbox Code Playgroud)
当我运行它时,无论我在提示中输入什么,它只会触发'''你害羞地点头."哦,太好了"'''
但是,如果我复制它并将其粘贴到我的python shell中,它有一个"哦"的问题,如果我摆脱它,它有一个问题在"那是",如果我只是摆脱"这是伟大的"它对下一个elif语句的前三个字符有问题.WTF是错误的,我的python代码和shell最近工作正常,能够拆分if和elif.但现在突然间它只是不想.
为了使用random.choice(),我必须将我的字符串转换为列表:
>>> x = "hello"
>>> y = list(x)
>>> y
['h', 'e', 'l', 'l', 'o']
Run Code Online (Sandbox Code Playgroud)
但试图反过来产生一个实际上看起来像['h', 'e', 'l', 'l', 'o']而不仅仅是的字符串hello.重复这样做会导致无限循环,产生如下所示的字符串:
"'", '"', ',', ' ', "'", ',', "'", ',', ' ', "'", ' ', "'", ',', ' ', "'", '"', "'", ',', ' ', '"', "'", '"', ',', ' ', "'", '"', "'", ',', ' ', "'", ',', "'", ',', ' ', "'", ' ', "'", ',', ' ', '"', "'", '"', ',', …Run Code Online (Sandbox Code Playgroud) 我希望脚本的一部分是这样的.
if list[1] is in list.pop n times:
return True
Run Code Online (Sandbox Code Playgroud) 我有一个dict列表,例如,
[{'id': 1L, 'name': u'Library'}, {'id': 2L, 'name': u'Arts'}, {'id': 3L, 'name': u'Sports'}]
Run Code Online (Sandbox Code Playgroud)
现在,我必须从这个字典中检索以下列表而不使用列表理解
[u'Library', u'Arts', u'Sports']
Run Code Online (Sandbox Code Playgroud)
有没有办法在python中实现这一点?我看到了许多类似的问题,但所有的答案都是使用列表理解.
任何建议表示赞赏.提前致谢.