Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.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 不能将两个列表相乘_Python_String_List - Fatal编程技术网

Python 不能将两个列表相乘

Python 不能将两个列表相乘,python,string,list,Python,String,List,抱歉,我是python新手。我使用正则表达式从文本文件中提取了两个数字列表,但现在我想将这两个列表相乘。但是,我只得到一个值{} listd = {} listn = {} numerator = ['1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1'] denominator = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13']

抱歉,我是python新手。我使用正则表达式从文本文件中提取了两个数字列表,但现在我想将这两个列表相乘。但是,我只得到一个值
{}

listd = {}
listn = {}

numerator = ['1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1']
denominator = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13']

[numerator*denominator for numerator,denominator in zip(listn,listd)]
当我打印listd或listn的结果时,出现如下错误:


{}
listd
lstn
声明为空dict(
{}

分子
分母
是列表,它们被您的列表装置隐藏(名称相同,范围较低的变量)

[int(n)*int(d)表示n,d(分子、分母)]
可能就是您要找的

但我不太清楚你想做什么

编辑

  • 增加了整数转换
  • {}是空的dict,而不是set
FTR:

  • {1,2,3}=集([1,2,3])
  • {1:2}=>dict
  • {}=>dict

请注意,您有字符串列表,而不是数字列表,因此需要对其进行转换

您还将
listd
listn
中的元素相乘,这些元素都是空字典,您甚至不需要这些变量

numerator = [int(n) for n in numerator]
denominator = [int(d) for d in denominator]
multiplied_list = [n*d for n,d in zip(numerator ,denominator)]

别忘了输入是字符串-OP必须先用
int()
将它们转换成整数。没有什么东西是一个快速的map不能解决的:
[n*d代表n,d在zip中(map(int,分子),map(int,分母))]
我在实际操作中使用了int而不是映射这两个列表,对于那些不擅长函数式编程的人来说,似乎更容易理解,
{}
是一个空的
dict
,而不是一个空的
集合
@Bruce是的,我想你是对的。我给这两个计时是因为。。。为什么不呢?>>%timeit[int(n)*int(d)表示压缩中的n,d(分子,分母)]100000个循环,最好的3:17.8µs/循环>>>%timeit[n*d表示压缩中的n,d(map(int,分子),map(int,分母))]100000个循环,最好的3:17.4µs/循环我预计它会慢很多。不是你知道的越多。