2012-03-28 64 views
2

在開始之前 - 讓我們知道我的課程IS允許爲這項任務尋求外部幫助,前提是我們不直接複製代碼。我所要求的是幫助,而不是公然不誠實地獲得代碼。我不打算通過以任何方式提出這個問題來作弊。如何將Python中的列表,標量和向量綁定在一起?

現在,多數民衆贊成清理....

這裏的任務:

#1:寫一個函數scalar_mult(S,V),其採用了許多,S,和一個列表,V並通過s返回v的標量倍數。

例如:

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:編寫一個函數替代(S,舊,新)替換舊與​​新所有出現在字符串s。

例如:

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!'  """ 
    """ 

我還沒有開始#2,但我真的不知道如何處理它。任何想法如何開始或如何工作?

這是週五到期的,並且是昨天分配的。僅供參考。

非常感謝任何回答的人 - 我知道這是一個相當大的問題。 <

如果您需要任何關於作業的說明,請告訴我!任何幫助將非常感謝:)

+0

哦...對不起格式不好!這是我的第一個問題,所以我不太清楚如何格式化代碼:p – 2012-03-28 20:14:36

+0

對於替換函數,我強烈建議您查看[內置字符串方法的文檔](http://docs.python.org/庫/ stdtypes.html#字符串的方法)。 – 2012-03-28 20:19:19

+0

如果您將每行縮進4個空格,它將成爲代碼塊。 (選擇文本和*然後*單擊代碼按鈕將縮進文本給你。) – Amber 2012-03-28 20:20:08

回答

0

第一是因爲Python是強類型的,所以你不能添加任意類型在一起。

>>> '%d %r' % (1, [2, 3]) 
'1 [2, 3]' 

對於第二個委託給替換的字符串方法。

0

所以你試圖實現的是不可能在Python中。你可能要做的是

>>> def scalar_mult(s, v): 
    return [s*x for x in v] 

>>> scalar_mult(5, [1, 2]) 
[5, 10] 
>>> 

現在你上面看到的是所謂的列表理解。你也可以使用地圖做同樣的事情,但也可以將它作爲練習。

因此,如果深入研究代碼,您將遍歷所有元素以及您乘以標量值的每個元素。結果放置在您理解的新列表中。

對於第二個問題,你通常應該使用庫replace,但似乎你可能不會使用它。所以,你可以去通過自注釋代碼

>>> 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 
+0

關於第1部分:OP正在尋求幫助修復錯誤消息,並特別要求_not_代碼將解決他的作業問題。 – dj18 2012-03-28 21:05:05

+0

在你的示例代碼中,我代表什麼? – 2012-03-29 19:49:33

1

爲您的文章的第一部分,您的錯誤信息是由於您使用的+運營商有兩個操作數:一個,(逗號)和v,這大概是一個列表。如果你只想打印v,您print語句應該是這樣的:

print "scalar_mult(", s, ",", v, "is:" # Note that I removed the `+` 

對於第二部分,有許多的方法可以解決這個問題,但最簡單的概念是要明白,在Python中的字符串是一個字符列表,所以你可以對其進行類似於數組的操作。例如:

>>> example_string = 'hello world!' 
>>> example_string[3] 
'l' 
>>> 

當然我不能回答你的家庭作業給你,但我會肯定建議考慮看看Python的built-in types documentation。它可以幫助你理解基本的字符串操作,以便你可以建立你的新功能。希望這會幫助你一點:)

相關問題