计算列表中的某些项,然后在if语句中使用该输出

时间:2017-08-16 08:13:23

标签: python arrays python-2.7 list if-statement

我正在尝试开发一个程序,该程序从用户那里获取大量输入,然后计算用户输入该数字的特定次数,然后使用该信息确定最常见的数据。

FFVideoFrame *FFMpeg::getVideoFrame(double r_time) { // r_time is current system time
    FFVideoFrame *vframe = nullptr;

    double timestamp;
    double clock_current_time = r_time - sys_clock_start;
    double clock_current_frame = 0.0;

    mutex_vid_queues.lock();

    int vid_queue_size = vid_queue.size();

    if (playback_started) {
        if (vid_queue_size > MAX_VID_OVERRUN) { // keep the buffer queues low
            playback_clock_start += 0.75;
            LOGD("TIMESHIFT buffers over MAX_VID_OVERRUN");
        } else if (vid_queue_size < 20) {
            playback_clock_start -= 0.4; // resync
            LOGD("TIMESHIFT buffers below 20");
        }
    }

    if (!playback_started) {
        // queued video buffers need to be at least 1 second for network stream

        if (((vid_queue_size > MIN_VID_FRAMES_START_NON_NETWORK && !isNetworkStream)
                || (vid_queue_size > MIN_VID_FRAMES_START && isNetworkStream))
                ) {

            playback_started = true;

            vframe = vid_queue.front();
            vid_queue.pop_front();

            playback_clock_start = vframe->timestamp_f; // set stream start time from time stamp

            sys_clock_start = r_time; // set system start time from current time
        }
    } else {
        bool in_bounds = true;
        FFVideoFrame *frame_temp;
        int drop_count = 0;

        while (in_bounds) {
            if (vid_queue_size == 0) {
                in_bounds = false;
            } else {
                frame_temp = vid_queue.front();

                timestamp = frame_temp->timestamp_f;
                clock_current_frame = timestamp - playback_clock_start;

                if (clock_current_frame > clock_current_time) {
                    in_bounds = false;
                } else {

                    // adds 0.xx of a second tolerance for video playback REMOVED FROM HERE
                    // this may get increased at some point

                    vid_queue.pop_front();
                    vid_queue_size--;

                    if (isNetworkStream
                        && fabs(clock_current_time - clock_current_frame) < 0.05) {
                        in_bounds = false;
                    }

                    if (vframe) {
                        vid_unused.push_back(vframe);
                        drop_count++;
                    }

                    vframe = frame_temp;
                }
            }
        }

        // if rendering takes too long then dropped frames will occur

        if (drop_count > 0) LOGD("Dumped %d video frames", drop_count);
    }

    mutex_vid_queues.unlock();

    if (vframe) clock_last_frame = (int64_t)timestamp;

    return vframe;
}

它将输出如下内容:

from collections import Counter
results = []
while len(results)<7:
    entered=int(raw_input('enter results:'))
    results.append(entered)

print results

distribution = Counter(results)

print distribution

从这里开始我希望能够获取最后给出的信息并告诉用户哪个是最常见的,我知道用户可以从计数器输出中看到它,但是我已经给出了它的一部分任务。我正在考虑使用一些

enter results:1
enter results:2
enter results:3
enter results:2
enter results:6
enter results:5
enter results:4
[1, 2, 3, 2, 6, 5, 4]
Counter({2: 2, 1: 1, 3: 1, 4: 1, 5: 1, 6: 1})

2 个答案:

答案 0 :(得分:0)

我不完全确定distribution.most_common的输出,但如果这不起作用,那么这是一个相当简单的循环来完成这项工作

most_common = 0
for i in distribution:
    if distribution[i] > distribution[most_common]:
        most_common = i

print most_common

此外,如果您需要知道该数字的频率是否超过输入的一半,您可以

if distribution[most_common] > 7./2:
    more_than_half = True
else:
    more_than_half = False

答案 1 :(得分:0)

根据我的理解,您想要的是以下内容:

from collections import Counter

distribution = Counter({2: 2, 1: 1, 3: 1, 4: 1, 5: 1, 6: 1})

most_common_item = distribution.most_common(1)
print('most common is: {}'.format(most_common_item[0][0]))  # Output: most common is: 2