Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python:在列表理解中使用2个参数调用join()?_Python - Fatal编程技术网

python:在列表理解中使用2个参数调用join()?

python:在列表理解中使用2个参数调用join()?,python,Python,我是python新手。这段代码来自Mark Pilgrim的“Dive Into Python”,似乎用2个参数调用了join(),效果很好: dirname="/usr/" [f for f in os.listdir(dirname) if os.path.isdir(os.path.join(dirname,f))] 但如果你尝试: smthn="data" smthnelse="otherdata" print "\n".join(smthn,smthnelse) 我们得到一个错误,

我是python新手。这段代码来自Mark Pilgrim的“Dive Into Python”,似乎用2个参数调用了join(),效果很好:

dirname="/usr/"
[f for f in os.listdir(dirname) if os.path.isdir(os.path.join(dirname,f))]
但如果你尝试:

smthn="data"
smthnelse="otherdata"
print "\n".join(smthn,smthnelse)

我们得到一个错误,join()只能接受一个参数。

os.path.join
将任意数量的字符串作为参数,
str.join
将iterable字符串作为一个参数。这两个函数是单独的函数

os.path.join
将任意数量的字符串作为参数,
str.join
将提供字符串的iterable作为一个参数。这两个函数是单独的函数

像这样使用str.join()查看:

smthn="data"
smthnelse="otherdata"

print( "\n",smthn.join(smthnelse) )
因为将smthnelse放在smthn>>“数据”中的空格之间

os.path.join()看起来不像str.join

第二个是字符串和seq
第一个仅用于文件系统路径。

使用str.join()如下所示:

smthn="data"
smthnelse="otherdata"

print( "\n",smthn.join(smthnelse) )
因为将smthnelse放在smthn>>“数据”中的空格之间

os.path.join()看起来不像str.join

第二个是字符串和seq
第一个仅用于文件系统路径。

您的问题是什么?您是否将其与
str.join
混淆?对不起,您的问题是什么?一个是str.join(),另一个来自操作系统模块,请看这里@aश威尼चhaudhary谢谢你被str.join搞混了你的问题是什么?你把它和
str.join
搞混了吗?对不起,你的问题是什么?一个是str.join(),另一个是os模块,看看这里@aश威尼च傲慢的,谢谢,是的,我被str.join搞糊涂了