파이썬의 데이터와 같은 시계열에 대한 중첩 사전 유형 데이터 구조를 구축하는 방법은 무엇입니까?

외국

다음 출력에 대해 중첩 된 사전 또는 유사한 구조를 만들려고합니다.

2014-08-19 23 positive
2014-08-19 23 neutral
2014-08-19 23 positive
2014-08-19 23 bot
2014-08-19 23 positive
2014-08-19 23 positive
2014-08-19 23 bot
2014-08-19 23 positive
2014-08-19 24 positive
2014-08-19 24 positive
2014-08-19 24 bot
2014-08-19 24 positive
2014-08-20 07 positive
2014-08-20 07 positive
2014-08-20 07 positive
2014-08-20 07 bot
2014-08-20 07 positive
2014-08-20 07 neutral
2014-08-20 08 neutral
2014-08-20 08 positive
2014-08-20 08 bot
2014-08-20 08 positive
2014-08-20 08 positive
2014-08-20 08 positive
2014-08-20 08 bot
2014-08-20 08 positive

이상적으로는 다음과 유사한 출력을 원합니다.

2014-08-19:{
            23:{
                positive:5,neutral:1,bot:1}
            24:{
                positive:3, neutral:0,bot:1}}
2014-08-20: {
            07:{
                positive:4,neutral:1,bot:1}
            08:{
                positive:5, neutral:1,bot:2}}

등등. 다음은 내가 지금까지 가지고있는 것입니다.

collect_tweet={}

for line in open('time_short.txt'):
    line=line.strip().split(' ')

    if line[0] not in collect_tweet:
        collect_tweet[line[0]]= {}
        if line[1] not in collect_tweet[line[0]]:
            collect_tweet[line[0]][line[1]]=[]

    collect_tweet[line[0]][line[1]].append(line[2])

이를 달성하기위한 아이디어 나 제안이 있습니까?

속도

당신은 정말 가깝습니다. 이렇게하면 원하는 것을 얻을 수 있습니다.

collect_tweet = {}

with open('time_short.txt') as file:
        for line in file.readlines():
                vals = line.rstrip().split()
                if vals[0] not in collect_tweet:
                        collect_tweet[vals[0]] = {}
                if vals[1] not in collect_tweet[vals[0]]:
                        collect_tweet[vals[0]][vals[1]] = {}
                if vals[2] not in collect_tweet[vals[0]][vals[1]]:
                        collect_tweet[vals[0]][vals[1]][vals[2]] = 1
                else:
                        collect_tweet[vals[0]][vals[1]][vals[2]] += 1

print collect_tweet

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관