使用itertools.combinations时内核死了

时间:2019-12-13 12:08:04

标签: python kernel combinations itertools

我正在使用itertools.combinations来生成列表元素的所有可能组合。但是内核总是在2分钟后死亡。我认为这是一个内存问题。还有其他有效的方法来产生所有可能的组合并将其存储在某种数据结构中吗?

total = ['E', 'ENE', 'ESE', 'N', 'NNE', 'NNW', 'NW', 'S', 'SE', 'SSE',
       'SSW', 'SW', 'W', 'WNW', 'WSW', 'station_0', 'station_1',
       'station_2', 'station_3', 'station_4', 'station_5', 'station_6',
       'station_7', 'station_8', 'station_9', 'station_10', 'year',
       'month', 'day', 'hour', 'SO2', 'NO2', 'CO', 'O3', 'TEMP', 'PRES',
       'DEWP', 'RAIN', 'WSPM']            

total_combinations = []           

for i in range(2,len(total)+1):
    current_comb = list(combinations(total,i))
    total_combinations = total_combinations + current_comb

1 个答案:

答案 0 :(得分:1)

如果我的数学正确,那么您总共有 549755813848 个组合!

var myTime = new Date().toLocaleString(navigator.language, {
                hour: '2-digit',
                minute: '2-digit',
                second: '2-digit'
            });

            function getValue(data) {

                var selectedText = $("#ddselect").find("option:selected").text();
                if (selectedText != "None") {

                    var display = document.getElementById("display");
                    var newRow = display.insertRow(display.rows.length);

                    var cell1 = newRow.insertCell(0);
                    cell1.innerHTML = myTime;

                    var cell2 = newRow.insertCell(1);
                    cell2.innerHTML = selectedText;

                    /* stop time */
                    var stopTime = new Date().toLocaleString(navigator.language, {
                        hour: '2-digit',
                        minute: '2-digit',
                        second: '2-digit'
                    });

                    var cell3 = newRow.insertCell(2);
                    cell3.innerHTML = stopTime;

                    var timeDifference = timediff(myTime, stopTime);
                    var cell4 = newRow.insertCell(3);
                    //cell4.innerHTML = new Date(stopTime.getTime() - myTime.getTime());
                    cell4.innerHTML = timeDifference;

                }

            }
            var timeArray = [];

            function timeobject(t) {
                a = t.replace('AM', '').replace('PM', '').split(':');
                h = parseInt(a[0]);
                m = parseInt(a[1]);
                s = parseInt(a[2]);
                ampm = (t.indexOf('AM') !== -1) ? 'AM' : 'PM';
                return {
                    hour: h,
                    minute: m,
                    seconds: s,
                    ampm: ampm
                };
            }

            function timediff(start, end) {
                start = timeobject(start);
                end = timeobject(end);

                end.hour = (end.ampm === 'PM' && start.ampm !== 'PM' && end.hour < 12) ? end.hour + 12 : end.hour;
                hourDiff = Math.abs(end.hour - start.hour);
                minuteDiff = end.minute - start.minute;
                secondDiff = end.seconds - start.seconds;

                if (minuteDiff < 0) {
                    minuteDiff = Math.abs(60 + minuteDiff);
                    hourDiff = hourDiff - 1;
                }
                if (secondDiff < 0) {
                    secondDiff = Math.abs(60 + secondDiff);
                    minuteDiff = minuteDiff - 1;
                }
                var timeTaken = hourDiff + ':' + Math.abs(minuteDiff) + ":" + Math.abs(secondDiff);
                var totDiff = hourDiff + 'hr ' + Math.abs(minuteDiff) + "min " + Math.abs(secondDiff) + "sec";
                timeArray.push(timeTaken);
                console.log(addTime(timeArray));
                return totDiff;
            }

            function addTime(arr) {
                var pad = function(num) {
                    return ("0" + num).slice(-2);
                }
                var totalTime = [];
                var totalSeconds = 0;
                for (var i = 0; i < arr.length; i++) {
                    totalTime = arr[i].split(":");
                    var hrs = parseInt(totalTime[0], 10);
                    var min = parseInt(totalTime[1], 10);
                    var sec = parseInt(totalTime[2], 10);
                    var currDurationSec = sec + (60 * min) + (60 * 60 * hrs);
                    totalSeconds += currDurationSec;
                }
                var hours = Math.floor(totalSeconds / 3600);
                totalSeconds %= 3600;
                var minutes = Math.floor(totalSeconds / 60);
                var seconds = totalSeconds % 60;
                var sumTime = pad(hours) + ":" + pad(minutes) + ":" + pad(seconds);
                document.getElementById('sumoftime').innerHTML = sumTime;
            }

来自docs

  

返回的项目数为n! / r! /(n-r)!当0 <= r <= n或   当r> n时为零。

因此,不能使用“正常”计算机将它们全部存储在内存中。在分别处理每个项目时,您必须找到一种解决问题的方法。