N维数组变成单个数组?

时间:2018-12-17 20:33:29

标签: python multidimensional-array n-dimensional arrayofarrays

我有一个列表数组,我想将其转换为一维数组。我可以使用2个“ for”循环来做到这一点,但一个禁止条件就是我必须这样做,因为它是N维数组。

   [TestMethod]
        public void PrampWordCountEngineTest()
        {

            string document = "Practice makes perfect. you'll only get Perfect by practice. just practice!";
            string[,] result = WordCountEngine(document);
            string[,] expected =
            {
                {"practice", "3"}, {"perfect", "2"},
                {"makes", "1"}, {"youll", "1"}, {"only", "1"},
                {"get", "1"}, {"by", "1"}, {"just", "1"}
            };
            CollectionAssert.AreEqual(expected,result);

        }
        public string[,] WordCountEngine(string document)
        {
            Dictionary<string, int> wordMap = new Dictionary<string, int>();
            string[] wordList = document.Split(' ');
            int largestCount = 0;
            foreach (string word in wordList)
            {
                string lowerWord = word.ToLower(); // can't assing to the same variable

                //remove special/punctuation characters
                var sb = new StringBuilder();
                foreach (var c in lowerWord)
                {
                    if (c >= 'a' && c <= 'z')
                    {
                        sb.Append(c);
                    }
                }
                string cleanWord = sb.ToString();
                if (cleanWord.Length < 1)
                {
                    continue;
                }
                int count = 0;
                if (wordMap.ContainsKey(cleanWord))
                {
                    count = wordMap[cleanWord];
                    count++;
                }
                else
                {
                    count = 1;
                }
                if (count > largestCount)
                {
                    largestCount = count;
                }
                wordMap[cleanWord] = count;
            }

            // we have a list of all of the words in the same length in a given cell of the big list
            List<List<string>> counterList = new List<List<string>>();
            for (int i = 0; i < largestCount + 1; i++)
            {
                counterList.Add(new List<string>());
            }
            foreach (var word in wordMap.Keys)
            {
                int counter = wordMap[word];
                counterList[counter].Add(word);
            }

            string[,] result = new string[wordMap.Keys.Count,2];
            int key = 0;
            //foreach list of words with the same length we insert the count of that word into the 2D array
            for (var index = counterList.Count-1; index > 0; index--)
            {
                var list = counterList[index];
                List<string> wordListCounter = list;
                if (wordListCounter == null)
                {
                    continue;
                }
                foreach (var word in wordListCounter)
                {
                    result[key, 0] = word;
                    result[key, 1] = index.ToString();
                    key++;
                }
            }
            return result;
        }

1 个答案:

答案 0 :(得分:0)

只需使用AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); Intent notificationIntent = new Intent(this, AlarmReceiver.class); notificationIntent.putExtra("user",username); for(int i=0; i<=2;i++) { if (i == 0) { final int id = (int) System.currentTimeMillis(); PendingIntent broadcast = PendingIntent.getBroadcast(this, id, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(System.currentTimeMillis()); cal.set(Calendar.HOUR_OF_DAY, 21); cal.set(Calendar.MINUTE, 33); cal.set(Calendar.SECOND, 00); alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, broadcast); } else if (i == 1) { final int id = (int) System.currentTimeMillis(); PendingIntent broadcast = PendingIntent.getBroadcast(this, id, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); Calendar call = Calendar.getInstance(); call.setTimeInMillis(System.currentTimeMillis()); call.set(Calendar.HOUR_OF_DAY, 22); call.set(Calendar.MINUTE,10); call.set(Calendar.SECOND, 00); notificationIntent.putExtra("service",2); alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, call.getTimeInMillis(), AlarmManager.INTERVAL_DAY, broadcast); }else { final int id = (int) System.currentTimeMillis(); PendingIntent broadcast = PendingIntent.getBroadcast(this, id, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); Calendar calll = Calendar.getInstance(); calll.setTimeInMillis(System.currentTimeMillis()); calll.set(Calendar.HOUR_OF_DAY, 21); calll.set(Calendar.MINUTE,45); calll.set(Calendar.SECOND, 59); notificationIntent.putExtra("service",1); enter code here alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calll.getTimeInMillis(), AlarmManager.INTERVAL_DAY, broadcast); } } 中的chain

代码:

itertools

输出:

import itertools as it

original_list = [[2,4,3,[1,2]],[1,5,6], [9], [7,9,0]]

print(original_list)

new_list = list(it.chain.from_iterable(original_list))

print(new_list)