在这个Python赋值中,如何将列表、标量和向量联系在一起?
在我开始之前-让我们知道,我的类可以为这个任务寻求外部帮助,只要我们不直接复制代码。我请求的是帮助,而不是明目张胆地不诚实地获取代码。我无意以任何方式问这个问题来作弊 现在一切都清楚了 这是作业: 1:编写一个函数scalar mult(s,v),该函数接受一个数字s和一个列表v,并返回v乘以s的标量倍数。 例如:在这个Python赋值中,如何将列表、标量和向量联系在一起?,python,list,vector,scalar,Python,List,Vector,Scalar,在我开始之前-让我们知道,我的类可以为这个任务寻求外部帮助,只要我们不直接复制代码。我请求的是帮助,而不是明目张胆地不诚实地获取代码。我无意以任何方式问这个问题来作弊 现在一切都清楚了 这是作业: 1:编写一个函数scalar mult(s,v),该函数接受一个数字s和一个列表v,并返回v乘以s的标量倍数。 例如: def scalar_mult(s, v): """ >>> scalar_mult(5, [1, 2]) [5, 10]
def scalar_mult(s, v):
"""
>>> scalar_mult(5, [1, 2])
[5, 10]
>>> scalar_mult(3, [1, 0, -1])
[3, 0, -3]
>>> scalar_mult(7, [3, 0, 5, 11, 2])
[21, 0, 35, 77, 14]
"""
我已经开始了这一部分,这就是我所拥有的:
import math
s = input("Please enter a single number to be our scalar value: ")
v = input("Please enter a vector value, like [1, 2] or [5, 6, 3], to be our vector value: ")
#Note to self: TUPLES use the parentheses. LISTS use the brackets
print "scalar_mult(", s, ",", + v, "is:"
print v * s
scalar_mult(s, v)
但我一直收到这样的错误信息:
print "scalar_mult(", s, ",", + v, "is:"
TypeError: bad operand type for unary +: 'list'
你知道如何解决这个问题吗
然后是第二部分
#2:编写一个函数replace(s,old,new),将字符串s中所有出现的old替换为new。
例如:
def replace(s, old, new):
"""
>>> replace('Mississippi', 'i', 'I')
'MIssIssIppI'
>>> s = 'I love spom! Spom is my favorite food. Spom, spom, spom, yum!'
>>> replace(s, 'om', 'am')
'I love spam! Spam is my favorite food. Spam, spam, spam, yum!'
>>> replace(s, 'o', 'a')
'I lave spam! Spam is my favarite faad. Spam, spam, spam, yum!' """
"""
>>> example_string = 'hello world!'
>>> example_string[3]
'l'
>>>
我还没有开始,但我真的不知道该怎么做。有关于如何开始或如何工作的想法吗
这是星期五交的,昨天分配的。仅供参考
非常感谢所有回答这个问题的人,我知道这是一个非常大的问题,首先是因为Python是强类型的,所以不能同时添加任意类型
>>> '%d %r' % (1, [2, 3])
'1 [2, 3]'
对于第二种方法,委托给替换的string方法。,因此您试图实现的目标在Python中是不可能的。你可能会做的是
>>> def scalar_mult(s, v):
return [s*x for x in v]
>>> scalar_mult(5, [1, 2])
[5, 10]
>>>
你在上面看到的叫做列表理解。你也可以用地图做同样的事情,但可能你可以把它当作一个练习
因此,如果深入研究代码,您将遍历所有元素,并为每个元素乘以标量值。结果将放在您理解的新列表中
对于第二个问题,您通常应该使用该库,但似乎您可能不使用它。因此,您可以浏览自注释代码
>>> def replace(st,old,new):
res=[] #As string is not mutable
#so start with a list to store the result
i=0 #you cannot change an index when using for with range
#so use a while with an external initialization
while i < len(st): #till the end of the string, len returns the string length
if st[i:i+len(old)]==old: #you can index a string as st[begin:end:stride]
res.append(new) #well if the string from current index of length
#equal to the old string matches the old string
#append the new string to the list
i+=len(old) #update the index to pass beyond the old string
else:
res.append(st[i]) #just append the current character
i+=1 # Advance one character
return ''.join(res) #finally join all the list element and generate a string
def更换(st、旧、新):
res=[]#因为字符串是不可变的
#因此,从一个列表开始存储结果
i=0#使用for with range时不能更改索引
#因此,在外部初始化时使用一段时间
当i+
运算符:a、
(逗号)和v
,这可能是一个列表。如果您只想打印v
,您的print
语句应该如下所示:
print "scalar_mult(", s, ",", v, "is:" # Note that I removed the `+`
对于第二部分,有许多方法可以解决这个问题,但从概念上讲,最简单的方法是理解python中的字符串是一个字符列表,因此可以像处理数组一样对其进行操作。例如:
def replace(s, old, new):
"""
>>> replace('Mississippi', 'i', 'I')
'MIssIssIppI'
>>> s = 'I love spom! Spom is my favorite food. Spom, spom, spom, yum!'
>>> replace(s, 'om', 'am')
'I love spam! Spam is my favorite food. Spam, spam, spam, yum!'
>>> replace(s, 'o', 'a')
'I lave spam! Spam is my favarite faad. Spam, spam, spam, yum!' """
"""
>>> example_string = 'hello world!'
>>> example_string[3]
'l'
>>>
我当然不能为你回答家庭作业,但我绝对建议你看看python的。它可以帮助您理解基本的字符串操作,以便构建新函数。希望这能帮你一点忙:)Ooh。。。对不起,格式不好!这是我的第一个问题,所以我不太确定如何格式化代码:对于替换函数,我强烈建议您查看。如果您将每行缩进4个空格,它将成为一个代码块。(选择文本,然后单击“代码”按钮将为您缩进该文本。)@NolenRoyalty:Your
replace
方法存在为s.replace(旧、新)
。您是否允许使用内置函数?关于第1部分:OP要求帮助修复错误消息,并明确要求不要提供能为他解决家庭作业问题的代码。