我如何将数组中的值分离,就像这样… [(1,3),(2,5),(4,8),(9,10),(5,6),(4,7),(7,10)]?

huangapple 未分类评论50阅读模式
标题翻译

How can i separate values in an array like this..[(1,3),(2,5),(4,8),(9,10),(5,6),(4,7),(7,10)]?

问题

例如,您有患者的入院和出院日期的数组(入院,出院)
类似 [(1,3),(2,5),(4,8),(9,10),(5,6),(4,7),(7,10)],我想要将每个元素分开..
帮助我!

英文翻译

For example, you have array of entry and exit date of patients(entry, exit)
like [(1,3),(2,5),(4,8),(9,10),(5,6),(4,7),(7,10)] and i want to separate each elements ..
Help me!

答案1

得分: 0

以下是代码的翻译部分:

import java.util.*;

public class ArraySeparation {
    public static void main(String[] args) {
        Integer[] patient = {1, 3, 2, 5, 4, 8, 9, 10};
        int n = patient.length;

        ArrayList<Integer> entry = new ArrayList<Integer>();
        ArrayList<Integer> exit = new ArrayList<Integer>();

        int i = 0;
        while (i < n) {
            if (i != n)
                entry.add(patient[i]);
            i += 2;
        }
        i = 1;
        while (i < n) {
            if (i != n)
                exit.add(patient[i]);
            i += 2;
        }

        System.out.println("Entry List:");
        for (int l : entry) System.out.print(l + " ");

        System.out.println("\nExit List:");
        for (int l : exit) System.out.print(l + " ");
    }
}

/*
 * 输出:
 * Entry List:
 * 1 2 4 9
 * Exit List:
 * 3 5 8 10
 */
英文翻译

You can use following code to separate the array and save it into 2 lists called as entry and exit

import java.util.*;

public class ArraySeparation {
    public static void main (String[] args)
    {
        Integer[] patient = {1,3,2,5,4,8,9,10};
        int n = patient.length;
    		  
        ArrayList&lt;Integer&gt; entry = new ArrayList&lt;Integer&gt;();
        ArrayList&lt;Integer&gt; exit = new ArrayList&lt;Integer&gt;();
        
        int i = 0;
        while(i &lt; n) 
        {
           if(i != n)
         	  	entry.add(patient[i]);
           i+=2;
         
        }i = 1;
        while(i &lt; n)
        {
            if(i != n)
            	 exit.add(patient[i]);
            i+=2;
        }
        
        System.out.println(&quot;Entry List:&quot;);
        for(int l:entry) System.out.print(l + &quot; &quot;);
        
        System.out.println(&quot;\nExit List:&quot;);
        for(int l:exit) System.out.print(l + &quot; &quot;);
    }
}

/*
 * Output:
 * Entry List:
 * 1 2 4 9
 * Exit List:
 * 3 5 8 10
 */

答案2

得分: -1

利用Python的zip和解压概念,我们可以轻松地进行分离。
例如:

a = [(1, 3), (2, 5), (4, 8), (9, 10), (5, 6), (4, 7), (7, 10)]
b = []

for start_d, end_d in a:
    max_d = end_d - start_d
    b.append(max_d)
    print(f'停留时长 max_d:{max_d}')
b.sort()
print(b)
print(b[(b.__len__() - 1)])

输出为

停留时长 max_d:2
停留时长 max_d:3
停留时长 max_d:4
停留时长 max_d:1
停留时长 max_d:1
停留时长 max_d:3
停留时长 max_d:3
[1, 1, 2, 3, 3, 3, 4]
4// 最长停留天数
英文翻译

Using Python zip and Unzip concept we can separate it easily.
for example :

a=  [(1,3),(2,5),(4,8),(9,10),(5,6),(4,7),(7,10)]
b=[]

for start_d, end_d in a:
    max_d = end_d - start_d
    b.append(max_d)
    print(f&#39;Duration of stays max_d: {max_d}&#39;)
b.sort()    
print(b) 
print(b[(b.__len__()-1)])

and output

Duration of stays max_d: 2
Duration of stays max_d: 3
Duration of stays max_d: 4
Duration of stays max_d: 1
Duration of stays max_d: 1
Duration of stays max_d: 3
Duration of stays max_d: 3
[1, 1, 2, 3, 3, 3, 4]
4// max-m day stay

huangapple
  • 本文由 发表于 2020年5月30日 22:40:32
  • 转载请务必保留本文链接:https://java.coder-hub.com/62104004.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定