python:将项目添加到列表时形状不匹配

时间:2019-01-05 08:16:12

标签: python python-3.x numpy matplotlib

我使用matplotlib从带有字符串和整数的字典中绘制图形,当我以其初始值绘制图形时,它可以工作,但是如果我在字典中添加另一个键 例如说

.authorizationStatus(for:)

当我再次绘制图形时,会出现以下错误

import EventKit

class EventHelper {

    // MARK: Properties
    var store: EKEventStore!


    // MARK: Methods
    /*
     This thing I got from here     https://stackoverflow.com/questions/28379603/how-to-add-an-event-in-the-device-calendar-using-swift more or less
     */
    func createCalendarEvent(for event: Event) -> String? {
        // Act base upon the current authorisation status
        switch EKEventStore.authorizationStatus(for: .event) {
        case EKAuthorizationStatus.notDetermined:
            // Ask for permission
            requestAuthorisationToCalendar()

            // And try again
            return try createCalendarEvent(for: event)

        case EKAuthorizationStatus.denied:
            print("Access to the Event Store was denied.")

        case EKAuthorizationStatus.restricted:
            print("Access to the Event Store was restricted.")

        case EKAuthorizationStatus.authorized:
            // Create a new event
            let newEvent = EKEvent(eventStore: store)
            newEvent.title = event.name
            newEvent.startDate = event.date

            // Create a timeinterval for the end date
            let twoHourTimeInterval = TimeInterval(exactly: 7200)
            newEvent.endDate =     event.date.addingTimeInterval(twoHourTimeInterval!)

            // choose the calender the event should be assinged to
            newEvent.calendar = self.store.defaultCalendarForNewEvents

            // try to save the new event in the event store
            do {
            try self.store.save(newEvent, span: .thisEvent, commit: true)
            print("Saved event with ID: \(String(describing: newEvent.eventIdentifier))")
                return newEvent.eventIdentifier
            } catch let error as NSError {
                print("Failed to save event with error: \(error)")
                return nil
            }
        }
    }

    // MARK: Privat Methods
    private func requestAuthorisationToCalendar() {
        store.requestAccess(to: .event) { (granted, error)  in
            if (granted) && (error == nil) {
                DispatchQueue.main.async {
                    print("User has granted access to calendar")
                }
            } else {
                DispatchQueue.main.async {
                    print("User has denied access to calendar")
                }
            }    
        }
    }
}

但是此问题仅在该图上发生,每当我使用饼图时,我就可以向字典中添加项目,并再次成功绘制饼图。

比我更有经验的人可以告诉我我在做什么错吗?

这是我的代码中有关此问题的相关部分 谢谢您的宝贵时间。

ADD,example,example.com,20.0

1 个答案:

答案 0 :(得分:0)

nlist(因此group_data)在while循环之前创建一次。因此,一旦您在while循环中将新项目添加到字典中,empresas的元素就会比nlist多,因此group_names的长度会比group-data长致电graph1

要解决此问题,请在对group_data的调用中提取graph1的值:

def graph1():
    gen = ((k, v[1]) for k, v in empresas.items())
    group_names, group_data = zip(*gen)
    fig, ax = plt.subplots()
    ax.barh(group_names, group_data)
    plt.show()