Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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 3.x Python3:TypeError:序列项0:应为str实例,找到列表_Python 3.x_List_Loops_Dictionary_Iterator - Fatal编程技术网

Python 3.x Python3:TypeError:序列项0:应为str实例,找到列表

Python 3.x Python3:TypeError:序列项0:应为str实例,找到列表,python-3.x,list,loops,dictionary,iterator,Python 3.x,List,Loops,Dictionary,Iterator,我正在尝试打印“宅男”这个词的所有可能的字符替换 #/usr/bin/python3 进口itertools 用户输入=“宅男” dict={ 'c':['c','c'], 'a':['a','a','@'], 't':['t','t'], 'k':['k','k'], “u”:[“u”,“u”], “e”:[e”,“e”,“3'], 'o':['o','o','0'] } output=“” 对于itertools.product中的i(dict['o']、dict['t']、dict['a'

我正在尝试打印“宅男”这个词的所有可能的字符替换

#/usr/bin/python3
进口itertools
用户输入=“宅男”
dict={
'c':['c','c'],
'a':['a','a','@'],
't':['t','t'],
'k':['k','k'],
“u”:[“u”,“u”],
“e”:[e”,“e”,“3'],
'o':['o','o','0']
}
output=“”
对于itertools.product中的i(dict['o']、dict['t']、dict['a']、dict['k']、dict['u']):
输出+=''。加入(i)+'\n“
打印(输出)
上面的脚本可以工作,但我需要
itertools.product()
输入(
dict['o'],dict['t'],dict['a'],dict['k'],dict['u']
)是动态的(例如,
new_list
):

#/usr/bin/python3
进口itertools
用户输入=“宅男”
dict={
'c':['c','c'],
'a':['a','a','@'],
't':['t','t'],
'k':['k','k'],
“u”:[“u”,“u”],
“e”:[e”,“e”,“3'],
'o':['o','o','0']
}
新列表=[]
对于用户_输入中的i:
新列表追加(dict[i])
output=“”
对于itertools.product中的i(新列表):
输出+=''。加入(i)+'\n“
打印(输出)
这将导致以下错误:

TypeError: sequence item 0: expected str instance, list found
我尝试了找到的解决方案,但将列表转换为str会破坏itertools.product系列

如何将动态输入传递到
itertools.product()

期望输出:

otaku
otakU
otaKu
otaKU
otAku
otAkU
otAKu
otAKU
ot@ku
ot@kU
ot@Ku
ot@KU
oTaku
oTakU
oTaKu
oTaKU
oTAku
oTAkU
oTAKu
oTAKU
oT@ku
oT@kU
oT@Ku
oT@KU
Otaku
OtakU
OtaKu
OtaKU
Ot@KU

在itertools.product(*新列表)中为i尝试


您需要添加一个
*
来解压缩
新列表

在itertools.product(*new\u list)中为i尝试


您需要添加一个
*
来解包
新列表

您可以使用
*
来解包:

itertools.product(*新列表)中i的

您也可以通过列表理解

c=[“”。在itertools.product(*新列表)中为x加入(x)]
对于c中的i:
印刷品(一)

您可以使用
*
解包:

itertools.product(*新列表)中i的

您也可以通过列表理解

c=[“”。在itertools.product(*新列表)中为x加入(x)]
对于c中的i:
印刷品(一)

哇,我真不敢相信这就是解决办法。为什么这里需要通配符?您的代码很好,但当您将
new_list
作为参数传递时,实际上是在传递列表。您要传递的是列表中的所有元素。添加
*
时,您正在解压列表并将每个元素作为参数传递。这就是预期的结果。看看这个函数:
def test_func(a,b,c):#做点什么
。如果我通过了一个类似于
test\u-func(new\u-list)
的列表,它将意味着
test\u-func(a=new\u-list)
。如果我像
test\u-func(*new\u-list)
那样解压列表,它将意味着
test\u-func(a=new\u-list[0],b=new\u-list[1]…)
。如果你知道args和kwargs是如何工作的,那真的很有用:哇,我真不敢相信这就是解决方案。为什么这里需要通配符?您的代码很好,但当您将
new_list
作为参数传递时,实际上是在传递列表。您要传递的是列表中的所有元素。添加
*
时,您正在解压列表并将每个元素作为参数传递。这就是预期的结果。看看这个函数:
def test_func(a,b,c):#做点什么
。如果我通过了一个类似于
test\u-func(new\u-list)
的列表,它将意味着
test\u-func(a=new\u-list)
。如果我像
test\u-func(*new\u-list)
那样解压列表,它将意味着
test\u-func(a=new\u-list[0],b=new\u-list[1]…)
。如果您了解args和kwargs的工作原理,那么这将非常有用: