2017-10-09 21 views
2

キーがdatetime.datetimeの辞書があります。&値はつぶやきのリストです。だから、次のようになります。datetimeオブジェクトのフィルタ日付(月別)

{datetime.datetime(2017, 9, 30, 19, 55, 20) : ['this is some tweet text'], 
datetime.datetime(2017, 9, 30, 19, 55, 20) : ['this is another tweet']... 

私は年の各月に送出つぶやきの数を取得しようとしています。これまでのところ、私は自分自身

startDate = 10 
endDate= 11 
start = True 
while start: 

    for k,v in tweetDict.items(): 
     endDate-=1 
     startDate-=1 

     datetimeStart = datetime(2017, startDate, 1) 
     datetimeEnd = datetime(2017,endDate, 1) 

     print(datetimeStart, datetimeEnd) 

     if datetimeStart < k < datetimeEnd: 
      print(v) 
     if endDate == 2: 
      start = False 
      break 

ただけプリント(私はprint文を認識してんだ)...

2017-08-01 00:00:00 2017-09-01 00:00:00 
2017-07-01 00:00:00 2017-08-01 00:00:00 
2017-06-01 00:00:00 2017-07-01 00:00:00 
2017-05-01 00:00:00 2017-06-01 00:00:00 
2017-04-01 00:00:00 2017-05-01 00:00:00 
2017-03-01 00:00:00 2017-04-01 00:00:00 
2017-02-01 00:00:00 2017-03-01 00:00:00 
2017-01-01 00:00:00 2017-02-01 00:00:00 

はなく、実際のつぶやき...持っています。私は何かを期待していた...

2017-08-01 00:00:00 2017-09-01 00:00:00 
['heres a tweet'] 
['theres a tweet'] 
2017-07-01 00:00:00 2017-08-01 00:00:00 
['there only 1 tweet for this month'].... 

私はこれをどうやって達成できますか?

答えて

1

あなただけgroup by月できる代わりの異なるヶ月の比較/減算しようとしている:

>>> d = {datetime.datetime(2017, 9, 30, 19, 55, 20): ['this is some tweet text'], 
     datetime.datetime(2017, 9, 30, 20, 55, 20): ['this is another tweet'], 
     datetime.datetime(2017, 10, 30, 19, 55, 20): ['this is an october tweet'],} 
>>> from itertools import groupby 
>>> for month, group in groupby(d.items(), lambda (k, v): k.month): 
...  print(month) 
...  for dt, tweet in group: 
...   print(dt, tweet) 
...   
10 
2017-10-30 19:55:20 ['this is an october tweet'] 
9 
2017-09-30 19:55:20 ['this is some tweet text'] 
2017-09-30 20:55:20 ['this is another tweet'] 
>>> 

そしてもちろん、あなたがよりよい形式でというように(内部結合は、各キーのために必要とされ、それを印刷することができますリストのようです):

>>> for month, group in groupby(d.items(), lambda (k, v): k.month): 
...  tweets = list(group) 
...  print("%d tweet(s) in month %d" % (len(tweets), month)) 
...  print('\n'.join(','.join(tweet) for (dt, tweet) in tweets)) 
...  
1 tweet(s) in month 10 
this is an october tweet 
2 tweet(s) in month 9 
this is some tweet text 
this is another tweet 
>>> 
+0

このインスタンスではgroupbyの方が簡単かもしれませんが、 '(k、v)'の直下のforループの最初の行に 'SyntaxError'が続きます。私はPython 3を使用しています。あなたのコードはPython 2のように見えるので、違いがありますか? – e1v1s

+0

ああ、謝罪、@ e1v1sは 'print x'を' print(x) 'に変更します(私はこのマシンにpython 3をインストールしていません)。 – Bahrom

+0

はい、私は既にprintステートメントの周りにかっこを追加しました。上記のコメントに「構文エラー」が記載されています:) – e1v1s

0

最初のこと:全く同じキーで2つのアイテムを入れています。 2番目のものは最初のものを上書きします。残りの部分については、例の2番目の項目が若干異なると仮定します(seconds=21)。

ループ内にendDatestartDateを減らしているため、コードが期待通りに機能しない理由があります。結果として、dict内の1つのアイテムに対して各日付をチェックするだけです。そのアイテムがその月に着陸すると、それが印刷されます。そうでない場合、それはしません。説明するために、ここにあなたがあなたのprintprint(datetimeStart, datetimeEnd, k, v)にを変更した場合、あなたが得るものです:既存のコードへの最小の変更で

2017-09-01 00:00:00 2017-10-01 00:00:00 2017-09-30 19:55:20 ['this is some tweet text'] 
['this is some tweet text'] 
2017-08-01 00:00:00 2017-09-01 00:00:00 2017-09-30 19:55:21 ['this is another tweet'] 
2017-07-01 00:00:00 2017-08-01 00:00:00 2017-09-30 19:55:20 ['this is some tweet text'] 
2017-06-01 00:00:00 2017-07-01 00:00:00 2017-09-30 19:55:21 ['this is another tweet'] 
2017-05-01 00:00:00 2017-06-01 00:00:00 2017-09-30 19:55:20 ['this is some tweet text'] 
2017-04-01 00:00:00 2017-05-01 00:00:00 2017-09-30 19:55:21 ['this is another tweet'] 
2017-03-01 00:00:00 2017-04-01 00:00:00 2017-09-30 19:55:20 ['this is some tweet text'] 
2017-02-01 00:00:00 2017-03-01 00:00:00 2017-09-30 19:55:21 ['this is another tweet'] 
2017-01-01 00:00:00 2017-02-01 00:00:00 2017-09-30 19:55:20 ['this is some tweet text'] 

修正は単にforループの前にデクリメントを移動し、if endDate...ブロックをDEDENTことであろうwhileループのレベルまで:もちろん

while start: 
    endDate-=1 
    startDate-=1 
    for k,v in tweetDict.items(): 
     datetimeStart = datetime(2017, startDate, 1) 
     datetimeEnd = datetime(2017,endDate, 1) 
     print(datetimeStart, datetimeEnd, k, v) 
     if datetimeStart < k < datetimeEnd: 
      print(v) 
    if endDate == 2: 
     start = False 
     break 

、その時点であなたにもちょうどif endDate...ブロックを取り除くとwhile endDate > 2:を行うしまうかもしれません。

関連する問題