在Python中将行分组为字典

时间:2017-11-13 11:54:04

标签: python dictionary

我有一个输入文件:

5181        zzzzzzzzzzzzzzzz
6220        aaaaaaaa
1498        bbbbbbbbb 
1498        ccccccccccc

我想对具有相同ID号的行进行分组。所以它会是这样的:

5181        zzzzzzzzzzzzzzzz 
6220        aaaaaaaa
1498        bbbbbbbbbb
1498        ccccccccccc

我尝试创建一个包含3个键但没有附加所有值的字典。

这就是我写的:

for i in package_ids:
    dict2[i] = (x for x in textt if int(i) in textt is True)

2 个答案:

答案 0 :(得分:1)

您可以使用collections.defaultdict

from collections import defaultdict
d = defaultdict(list)
file_data = [[int(a), b] for a, b in [i.strip('\n').split('\t') for i in open('filename.txt')]]
for a, b in file_data:
   d[a].append(b)

for a, b in sorted(d.items(), key=lambda x:x[0]):
    print(a, b)

没有defaultdict

d = {}
for a, b in file_data:
   if a not in d:
       d[a] = [b]
   else:
       d[a].append(b)

答案 1 :(得分:1)

假设 id 编号和行的文本用 tab (s)001b2bbd t void rtc::CopyOnWriteBuffer::AppendData<unsigned char, (void*)0>(unsigned char const*, unsigned int) 000ee055 t rtc::CopyOnWriteBuffer::EnsureCapacity(unsigned int) 000edf51 t rtc::CopyOnWriteBuffer::CloneDataIfReferenced(unsigned int) 000fafd9 t char* rtc::CopyOnWriteBuffer::data<char, (void*)0>() 000fae61 t unsigned char* rtc::CopyOnWriteBuffer::data<unsigned char, (void*)0>() 000eddcd t rtc::CopyOnWriteBuffer::Clear() 00092cd1 t void rtc::CopyOnWriteBuffer::SetData<char, (void*)0>(char const*, unsigned int) 00092cd1 t void rtc::CopyOnWriteBuffer::SetData<unsigned char, (void*)0>(unsigned char const*, unsigned int) 000edb4d t rtc::CopyOnWriteBuffer::SetSize(unsigned int) 000fad19 t rtc::CopyOnWriteBuffer::operator=(rtc::CopyOnWriteBuffer&&) 000ed6f1 t rtc::CopyOnWriteBuffer::CopyOnWriteBuffer(unsigned int) 000ed7d9 t rtc::CopyOnWriteBuffer::CopyOnWriteBuffer(unsigned int, unsigned int) 000ed6e1 t rtc::CopyOnWriteBuffer::CopyOnWriteBuffer(rtc::CopyOnWriteBuffer&&) 0003462f t rtc::CopyOnWriteBuffer::CopyOnWriteBuffer(rtc::CopyOnWriteBuffer const&) 0002f1f7 t rtc::CopyOnWriteBuffer::CopyOnWriteBuffer() 000faa35 t rtc::CopyOnWriteBuffer::CopyOnWriteBuffer<char, (void*)0>(char const*, unsigned int) 000ed6f1 t rtc::CopyOnWriteBuffer::CopyOnWriteBuffer(unsigned int) 000ed7d9 t rtc::CopyOnWriteBuffer::CopyOnWriteBuffer(unsigned int, unsigned int) 000ed6e1 t rtc::CopyOnWriteBuffer::CopyOnWriteBuffer(rtc::CopyOnWriteBuffer&&) 0003462f t rtc::CopyOnWriteBuffer::CopyOnWriteBuffer(rtc::CopyOnWriteBuffer const&) 0002f1f7 t rtc::CopyOnWriteBuffer::CopyOnWriteBuffer() 000faa35 t rtc::CopyOnWriteBuffer::CopyOnWriteBuffer<char, (void*)0>(char const*, unsigned int) 0003452d t rtc::CopyOnWriteBuffer::~CopyOnWriteBuffer() 0003452d t rtc::CopyOnWriteBuffer::~CopyOnWriteBuffer() 分隔,简单排序:

QT  += core xml
QT  -= gui

TARGET = MYPROJECT_Core
TEMPLATE = lib

DEFINES += MYPROJECT_CORE_LIBRARY

DEFINES += QT_DEPRECATED_WARNINGS
DEFINES += NOMINMAX
DEFINES += ENABLE_RTC_EVENT_LOG


QMAKE_CFLAGS_WARN_ON -= -Wall
QMAKE_CXXFLAGS_WARN_ON -= -Wall
CONFIG += warn_off

android {
    CONFIG += c++11
    QMAKE_CXXFLAGS += -std=c++11

    DEFINES += NDEBUG=1
    DEFINES += WEBRTC_POSIX
    DEFINES += WEBRTC_ANDROID

    LIBS += -L$$PWD/../Libs/webrtc-58/lib/android/armeabi-v7a/jni/armeabi-v7a -ljingle_peerconnection_so
    INCLUDEPATH += $$PWD/../Libs/webrtc-58/include
    DEPENDPATH += $$PWD/../Libs/webrtc-58/include
} 


SOURCES += \
    main.cpp \

输出:

\t