我正在使用python 2.5.2.以下代码无效.
def findValue(self, text, findText):
index = text.find(findText)
print index
Run Code Online (Sandbox Code Playgroud)
虽然findText它存在text,但它仍然存在None.
我已经打印了text和的值,findText并且它们存在.
编辑:我已经解决了这个问题.
问题是我正在调用代码
for i in arr:
self.findValue(i,"someText")
Run Code Online (Sandbox Code Playgroud)
作为类型i是instance所以这是行不通的.我刚把它改成:
self.findValue(str(i),"someText")
Run Code Online (Sandbox Code Playgroud) 我已经搜索了这个,并遇到了列表返回功能,但我仍然不明白.
我试图理解为什么Print函数返回另一个函数返回以下内容:
生日快乐
生日快乐
无无
我的代码:
def happy():
print("Happy Birthday")
def main():
print( happy(), happy() )
main()
Run Code Online (Sandbox Code Playgroud)
我知道该函数返回一个名为None的特殊对象.但我只是想了解它为什么会这样做?
my_list = ['a','b','c']
def func(input):
for i in input:
print i
print func(my_list)
Run Code Online (Sandbox Code Playgroud)
产量
a
b
c
None
Run Code Online (Sandbox Code Playgroud)
我不想要'无'所以我怎么能用一行代码来返回输入列表中的项目?
我试过了:
return for i in input: print i
Run Code Online (Sandbox Code Playgroud)
但它没有用,因为return语句不能那样工作
我正在尝试根据某些列的值在数据帧上创建一个新列。在所有情况下它都返回 null。有人知道这个简单的例子出了什么问题吗?
df = pd.DataFrame([[0,1,0],[1,0,0],[1,1,1]],columns = ['Foo','Bar','Baz'])
spark_df = spark.createDataFrame(df)
def get_profile():
if 'Foo'==1:
return 'Foo'
elif 'Bar' == 1:
return 'Bar'
elif 'Baz' ==1 :
return 'Baz'
spark_df = spark_df.withColumn('get_profile', lit(get_profile()))
spark_df.show()
Foo Bar Baz get_profile
0 1 0 None
1 0 0 None
1 1 1 None
Run Code Online (Sandbox Code Playgroud)
我希望所有行都填写 get_profile 列。
我也尝试过这个:
spark_udf = udf(get_profile,StringType())
spark_df = spark_df.withColumn('get_profile', spark_udf())
print(spark_df.toPandas())
Run Code Online (Sandbox Code Playgroud)
达到同样的效果。
我有以下练习:
如果是工作日,则参数weekday为True,如果我们正在度假,则参数vacation为True.如果不是工作日或者我们正在休假,我们会睡觉.如果我们入睡,则返回True.
这是我所做的,但第二个打印功能只打印'None'.
def sleep_in(weekday, vacation):
if(not weekday or vacation):
return True
print(sleep_in(False, False))
print(sleep_in(True, False))
print(sleep_in(False, True))
Run Code Online (Sandbox Code Playgroud)
输出:
True
None
True
Run Code Online (Sandbox Code Playgroud) 我是Python和编程的绝对初学者,我刚刚接触过函数.
我在下面定义了两个简单的函数:
def output1():
print "Hello, world!"
def output2():
print "Hello, there!"
output1()
output2()
Run Code Online (Sandbox Code Playgroud)
将上面的内容保存到一个名为function.py的脚本后,我使用windows power shell运行脚本,并按照您的预期打印以下内容:
Hello, world!
Hello, there!
Run Code Online (Sandbox Code Playgroud)
但是当我将脚本修改为:
def output1():
print "Hello, world!"
def output2():
print "Hello, there!"
print output1()
print output2()
Run Code Online (Sandbox Code Playgroud)
它打印:
Hello, world!
None
Hello, there!
None
Run Code Online (Sandbox Code Playgroud)
出于好奇,当我将output1和output2作为print前缀时,为什么会这样做呢?
def greeter(name):
print('Hey %s, I was going to call you
yesterday.'%name)
print('Damn %s, you grew so much since HS.'%name)
print('It was nice to see you %s!\n'%name)
Run Code Online (Sandbox Code Playgroud)
该功能旨在获取名称并将其插入这些问候语中。
输入:
friends = ['kevin','Darwin','Erica']
for friend in friends:
print(greeter(friend))
Run Code Online (Sandbox Code Playgroud)
输出:
嗨,凯文,我昨天要打给你。该死的凯文,自HS以来,您成长了很多。很高兴见到你凯文!
没有
嘿,达尔文,我昨天要打给你。该死的达尔文,自HS以来,您成长了很多。很高兴见到你达尔文!
没有
嘿,埃里卡,我昨天要打电话给你。该死的埃里卡,自HS以来,您成长了很多。很高兴见到你埃里卡!
没有
问题:为什么每次在for循环中的朋友上执行此功能后,都会弹出“无”?
正如您在定义函数时在python中所知,您应该return输入代码来完成函数.在下面的代码中,我该如何完成功能:
def f(x) :
if x == 0:
print 'yes'
print 'no'
print f(2)
Run Code Online (Sandbox Code Playgroud)
当我在打印后在最后一行运行此代码时,会None出现吗?
python ×8
function ×4
python-2.7 ×2
return ×2
apache-spark ×1
find ×1
for-loop ×1
list ×1
printing ×1
pyspark ×1
return-value ×1