我正在尝试使用NetworkX读取一个Shapefile并使用该函数write_shp()生成将包含节点和边缘的Shapefile(在此示例之后 < - 此链接现在已经死了),但是当我尝试运行它给出的代码时我有以下错误:
Traceback (most recent call last): File
"C:/Users/Felipe/PycharmProjects/untitled/asdf.py", line 4, in
<module>
nx.write_shp(redVial, "shapefiles") File "C:\Python34\lib\site-packages\networkx\readwrite\nx_shp.py", line
192, in write_shp
for key, data in e[2].iteritems(): AttributeError: 'dict' object has no attribute 'iteritems'
Run Code Online (Sandbox Code Playgroud)
我正在使用Python 3.4并通过pip install安装NetworkX.
这个错误之前,它已经给了我另外一个说:"x范围不存在"或类似的东西,所以我看着它,只是改变xrange以range在nx_shp.py文件,这似乎解决它.
根据我的阅读,它可能与Python版本(Python2与Python3)有关.
Python函数可以作为另一个函数的参数吗?
说:
def myfunc(anotherfunc, extraArgs):
# run anotherfunc and also pass the values from extraArgs to it
pass
Run Code Online (Sandbox Code Playgroud)
所以这基本上是两个问题:
BTW,extraArgs是anotherfunc参数的列表/元组.
更新:我不是在询问可变数量的参数.最终结果(如果可能的话)应该是一个完全按照我下面的例子定义的函数,或者至少完全按照这种方式行事 - 我在问题中添加了一些例子来试图澄清这一点.
(可能的情况是,实现这一目标的唯一方法是使用
__init__然后手动处理这些并在输入与预期不完全匹配时引发适当的异常,在这种情况下,这将是一个有效的答案)
为了我自己的娱乐,我想知道如何实现以下目标:
functionA = make_fun(['paramA', 'paramB'])
functionB = make_fun(['arg1', 'arg2', 'arg3'])
Run Code Online (Sandbox Code Playgroud)
相当于
def functionA(paramA, paramB):
print(paramA)
print(paramB)
def functionB(arg1, arg2, arg3):
print(arg1)
print(arg2)
print(arg3)
Run Code Online (Sandbox Code Playgroud)
这意味着需要以下行为:
functionA(3, paramB=1) # Works
functionA(3, 2, 1) # Fails
functionB(0) # Fails
Run Code Online (Sandbox Code Playgroud)
问题的焦点在于变量argspec - 我很舒服地使用通常的装饰器技术创建函数体.
对于那些感兴趣的人,我正在尝试以编程方式创建类似以下的类.同样困难在于__init__使用编程参数生成方法 - 类的其余部分使用装饰器或可能是元类看起来很简单.
class MyClass:
def __init__(self, paramA=None, paramB=None):
self._attr = ['paramA', 'paramB']
for a in self._attr:
self.__setattr__(a, None)
def __str__(self):
return str({k:v for (k,v) in self.__dict__.items() if k in …Run Code Online (Sandbox Code Playgroud) 我是 Python 新手,试图了解继承方法背后的哲学/逻辑。问题最终涉及为什么以及何时必须__init__在子类中使用该方法。例子:
从超类继承的子类似乎不需要自己的构造函数(__init__) 方法。下面,一只狗继承了哺乳动物的属性(名字、年龄)和方法(makenoise)。你甚至可以添加一个方法 ( do_a_trick) 一切都像它“应该的”那样工作,看起来。
但是,如果我想在子类中添加一个新属性,就像我在 Cats 类中所做的那样,我会收到一条错误消息,指出“self”未定义。然而,我在狗类的定义中使用了“自我”。区别的本质是什么?它似乎定义了 Cats,因为我希望我需要使用__init__(self,name)和super()__init__(name)。为什么会有差异?
class Mammals(object):
def __init__(self,name):
self.name = name
print("I am a new-born "+ self.name)
self.age = 0
def makenoise(self):
print(self.name + " says Hello")
class Dogs(Mammals):
def do_a_trick(self):
print(self.name + " can roll over")
class Cats(Mammals):
self.furry = "True" #results in error `self' is not defined
mymammal = Mammals("zebra") #output "I am a new-born zebra"
mymammal.makenoise() #output "zebra says hello" …Run Code Online (Sandbox Code Playgroud) 我有一个函数,它接受可变数量的变量作为参数。some_list我如何将下面示例中的内容发送到我的myfunc2()
def myfunc1(*args):
arguments = [i for i in args]
#doing some processing with arguments...
some_list = [1,2,3,4,5] # length of list depends on what was passed as *args
var = myfunc2(???)
Run Code Online (Sandbox Code Playgroud)
我能想到的唯一方法是将列表或元组作为myfunc2()参数传递,但也许有一个更优雅的解决方案,所以我不必重写myfunc2()和其他几个函数。
我是python的新手,我开始学习执行函数.我开始添加数字,但我只能加两个数字,如果我想总结更多,那就需要我编辑程序.这是我的代码
def sum(num1,num2):
return num1+num2
a=5
b=7
c=sum(a,b)
print (c)
Run Code Online (Sandbox Code Playgroud)
现在我想创建一个函数来总结你想要的任何数量的数字,而无需编辑代码.这是我做的:
def sum(num1,num2):
return num1+num2
a=int(input("what is the number you want to add?: "))
ask="yes"
while(ask=="yes"):
b=int(input("what is the number you want to add?: "))
c=sum(a,b)
a=c
ask=input("do you want to add another number?: ")
else:
c=a
print (c)
Run Code Online (Sandbox Code Playgroud)
这有效,但我认为应该有一个更简单的方法来实现这个功能......对吗?谢谢你的帮助!
我正在尝试制作一个函数,它接受不确定数量的参数,即单词,它将在单个单词之间添加一个“+”。最后它应该仍然是一个字符串。
def add_words(word1 word2 word3 ...)
Run Code Online (Sandbox Code Playgroud)
输出:
"word1+word2+word3 ...")
Run Code Online (Sandbox Code Playgroud) 如何将int转换为包含该int的列表.
我的代码:
y=x
Run Code Online (Sandbox Code Playgroud)
这里x是列表中int的几率是95%到5%.如果x是一个int然后很好.但如果x是一个列表,我必须使用:
y=list(x)
Run Code Online (Sandbox Code Playgroud)
但在这种情况下,它会为int创建问题.
我可以将x声明为所有情况的列表.但只有10%的机会使用列表是非常不必要的.
有没有其他方法,如果我可以将一个int转换为列表(而不是字符串),以便我可以一直使用y=list(x)?
我需要一个简单的方法来将所需的参数传递给一个接受多个参数的函数.
def sample1(arg1=0,arg2=0,arg3=0 ... ... argn=0)
Run Code Online (Sandbox Code Playgroud)
参数词典
argument_dictionary={'arg1':0,'arg4':1}
Run Code Online (Sandbox Code Playgroud)
我想遍历argument_dictionary并仅将arg1和arg4传递给sample1 as
sample1(arg1=0,arg4=1)
Run Code Online (Sandbox Code Playgroud)