我有一个清单:
lists = (['1','2','3','S','3','4','S','4','6','7'])
Run Code Online (Sandbox Code Playgroud)
我希望每次出现"S"时将列表拆分为较小的列表,并将"S"消除为:
([['1','2','3'],['3','4],['4','6','7']])
Run Code Online (Sandbox Code Playgroud)
我的代码:
def x (lists):
empty = ''
list = []
for x in lists:
if x == empty:
list[-1:].append(x)
else:
list.append([x])
return (list)
Run Code Online (Sandbox Code Playgroud)
我试过这样的事情,但我对python很新,而且我无处可去.没什么好看的,我怎么解决我的问题?
朋友,基本上,我想采取一个字典:
fruit_dict = {'oranges':['big','small'],'apples':['green','yellow','red']}
Run Code Online (Sandbox Code Playgroud)
并通过在不同键的所有值之间进行所有可能的排列来获得以下字典列表:
output_list =
[
{'oranges':'big','apples':'green'},
{'oranges':'big','apples':'yellow'},
{'oranges':'big','apples':'red'},
{'oranges':'small','apples':'green'},
{'oranges':'small','apples':'yellow'},
{'oranges':'small','apples':'red'}
]
Run Code Online (Sandbox Code Playgroud)
怎么做?太感谢了!
这个问题很容易理解,这里是程序 -
hisc = [1,2,3,4]
print("\n", hisc)
ohisc = hisc
hisc.append(5)
print("\nPreviously...", ohisc)
print("\nAnd now...", hisc)
input("\nETE")
Run Code Online (Sandbox Code Playgroud)
当我运行它时ohisc获得5.为什么ohisc会改变?如何阻止它改变?如果这是显而易见的事情,请道歉.
我正在写一些Python 3.2代码,这个问题来找我:
我有这些变量:
# a list of xml.dom nodes (this is just an example!)
child_nodes = [node1, node2, node3]
# I want to add every item in child_node into this node (this also a xml.dom Node)
parent = xml_document.createElement('TheParentNode')
Run Code Online (Sandbox Code Playgroud)
这正是我想要做的:
for node in child_nodes:
if node is not None:
parent.appendChild(node)
Run Code Online (Sandbox Code Playgroud)
我在一行中写了这样的:
[parent.appendChild(c) for c in child_nodes if c is not None]
Run Code Online (Sandbox Code Playgroud)
我根本不打算使用列表结果,我只需要appendChild来完成它的工作.
我不是一个非常有经验的python程序员,所以我想知道哪个更好?我喜欢单线解决方案,但我想从经验丰富的python程序员那里了解到:
在代码美/可维护性和性能/内存使用的上下文中哪一个更好.
当我将这段代码输入到python shell中时,它可以正常工作,但是在程序中它会产生错误.
import os
h = os.environ['HOME']
Run Code Online (Sandbox Code Playgroud)
在脚本中它给出了这个错误:
AttributeError: 'str' object has no attribute 'environ'
Run Code Online (Sandbox Code Playgroud)
为什么会发生这种情况,有什么方法可以解决它吗?
(我有点像学习python所以我不太了解.谷歌没有帮助)
python脚本有什么问题?
码:
import os
import shutil
import getpass
os.mkdir("C:\\dtmp")
shutil.copy("C:\\path\\to\\bb-freeze-script.py","C:\\dtmp")
os.chdir("C:\\dtmp")
shutil.copy("C:\\path\\to\\main.py","C:\\dtmp")
os.system("python bb-freeze-script.py main.py")
os.mkdir("C:\\Program Files\\Directories v0.6")
os.chdir("C:\\")
shutil.rmtree("C:\\dtmp")
print getpass.getuser()
Run Code Online (Sandbox Code Playgroud)
错误:
Traceback (most recent call last):
File "bb-freeze-script.py", line 8, in <module>
load_entry_point('bbfreeze==0.97.3', 'console_scripts', 'bb-freeze')()
File "C:\Python27\lib\site-packages\bbfreeze-0.97.3-py2.7-win32.egg\bbfreeze\__init__.py", line 24, in main
f.addScript(x)
File "C:\Python27\lib\site-packages\bbfreeze-0.97.3-py2.7-win32.egg\bbfreeze\freezer.py", line 410, in addScript
s = self.mf.run_script(path)
File "C:\Python27\lib\site-packages\bbfreeze-0.97.3-py2.7-win32.egg\bbfreeze\modulegraph\modulegraph.py", line 241, in run_script
co = compile(file(pathname, READ_MODE).read()+'\n', pathname, 'exec')
File "C:\dtmp\main.py", line 14
^
IndentationError: expected an indented block
Run Code Online (Sandbox Code Playgroud)
操作系统 - Windows XP
我想从2个不同的文件夹导入Python程序:
Prog1 从路上 /home/francis/docs/folder1/Prog2 从路上 /home/francis/docs/folder2/如何在我的主程序中导入这两个程序/home/francis/docs/folder3/?
我有一个JSON字符串,我发布到我的Python脚本.这是一个示例字符串:
{"uid":"1111111","method":"check_user"}
Run Code Online (Sandbox Code Playgroud)
在我的Python代码我只需拨打simplejson.loads( str )其中str来自请求的JSON字符串.JSON字符串看起来很好,因为当我在请求时打印它时它完好无损.但是我得到一个ValueError:
Extra data: line 1 column 41 - line 1 column 48 (char 41 - 48)
Traceback (most recent call last): File
"/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/webapp/_webapp25.py",
line 703, in __call__
handler.post(*groups) File "/Users/.../app/controller/api_controller.py", line 25, in post
req = simplejson.loads( req ) File
"/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/django_0_96/django/utils/simplejson/__init__.py",
line 232, in loads
return cls(encoding=encoding, **kw).decode(s) File
"/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/django_0_96/django/utils/simplejson/decoder.py",
line 254, in decode
raise ValueError(errmsg("Extra data", s, end, len(s)))
Run Code Online (Sandbox Code Playgroud)
任何想法可能是什么?我已经尝试从字符串中删除新的行,制表符和斜杠,甚至使用它来解码它.decode('string_escape')
我想找出一个人在Python中的操作系统.我知道我总能做到这一点:
try:
os.system(unixonlycommand)
except:
os.system(windowsonlycommand)
Run Code Online (Sandbox Code Playgroud)
但还有另一种方法吗?这个或那个特殊的模块?
这是我的一段代码......
def contactMaster(data="",url= str(chosenMaster)+"todolist"):
print "url: "+url
Run Code Online (Sandbox Code Playgroud)
它只打印"todolist"而不是"http://www.mysite.com/blah/1234/todolist"
为什么不工作?
python ×10
python-3.x ×3
for-loop ×2
list ×2
append ×1
decode ×1
dictionary ×1
import ×1
indentation ×1
json ×1
permutation ×1
string ×1
windows-xp ×1