if match:
occur=0
for item in tweets:
if candidate in item:
popular=item[4]+item[5]
occur+=popular
famous.append(occur)
return occur
的名單,我得到10161只。也因爲這個功能是有辦法,我可以按號碼列表,並與候選人按排序號字典與元組的Python
if match:
occur=0
for item in tweets:
if candidate in item:
popular=item[4]+item[5]
occur+=popular
famous.append(occur)
return occur
的名單,我得到10161只。也因爲這個功能是有辦法,我可以按號碼列表,並與候選人按排序號字典與元組的Python
的修改很簡單,首先你需要創建一個空的列表,其中結果將被保留,然後返回一個列表在for循環中,您需要檢查條件是否已滿足,並將該元素添加到列表中(如果有),最後返回該列表。我也選擇一些更有意義的變量名
def famous(data_tweet, lower, higher):
result=[]
for person, tweets in data_tweet.items():
if all(lower <= tw[2] <= higher for tw in tweets):
result.append(person)
return result
這可以減少與使用listcomprehension
def famous(data_tweet, lower, higher):
return [person for person, tweets in data_tweet.items() if all(lower <= tw[2] <= higher for tw in tweets)]
一點點(注意相似)
在這兩種情況下結果
>>> famous(tweet,1470000000,1480000000)
['b', 'a']
>>>
至於你是不是允許使用all
,那麼它應該做的老派風格,即使用標誌,它是一個變量,它會告訴我們,如果條件滿足與否
def famous(data_tweet, lower, higher):
result=[]
for person, tweets in data_tweet.items():
is_valid = True
for tw in tweets:
if not (lower <= tw[2] <= higher):
is_valid = False
break
if is_valid:
result.append(person)
return result
我們在這裏首先假設條件滿足,那麼我們檢查是有效真的,如果不是我們改變我們的標誌爲false,並打破循環,因爲沒有更多的理由繼續進行進一步的檢查,這是基本上是all
做爲你。之後,根據標誌的價值,我們將該人員追加到列表中。
(如果您還沒有允許使用break
或者,也別擔心,只是將其刪除,這不會影響功能)
關於計數的東西
result=[]
for person in tweet:
count = 0
for tw in tweet[person]:
count += tw[4] + tw[5]
result.append(count)
copperfield是否有一種方法可以不使用全部? – CAVS
是的,但是這可以歸結爲重寫'all',爲什麼你不想使用它? – Copperfield
即使是語言功能標準集的一部分,我也不允許使用 – CAVS
和你期望或希望從你的功能得到什麼結果? – Copperfield
@Copperfield所以如果日期是兩個日期之間,IM goanna補充說,看到誰是著名的代碼(通過添加最後兩個數字),並打印出與人從最大到最小著名 – CAVS
...好,但名單不回答我的問題,這個函數應該做什麼?返回滿足條件的人的名單? – Copperfield