简体   繁体   中英

Program that groups numbers/letters with how many times it iterates, and puts it into array

So I'm trying to make a program that checks an Array(Array2) to see if it contains numbers from another Array(Array1), and groups them along with the numbers or letters that it's connected with. With the data I put below, I was hoping for it to return something like (14T,6in,4s,3aegr,2o,1ESTfhlmpx), instead it returns 14T,6i,4s,3a,2o,1e. It seems like it's not wanting to iterate all of the other letters and numbers in the array. I tried putting it into a string instead of changing the array to add the numbers which have it, and it showed that the for loop iterated through all of the letters/numbers, but it just won't add it to the array. I've been stuck on this for multiple hours now, and was just curious if someone could help me out with this.

Array1[0] = "14";
Array1[1] = "6";
Array1[2] = "4";
Array1[3] = "3";
Array1[4] = "2";
Array1[5] = "1";

String[] Array2 = new String[18];
Array2[0] = "E1";
Array2[1] = "S1";
Array2[2] = "T14";
Array2[3] = "a3";
Array2[4] = "e3";
Array2[5] = "f1";
Array2[6] = "g3";
Array2[7] = "h1";
Array2[8] = "i6";
Array2[9] = "l1";
Array2[10] = "m1";
Array2[11] = "n6";
Array2[12] = "o2";
Array2[13] = "p1";
Array2[14] = "r3";
Array2[15] = "s4";
Array2[16] = "t4";
Array2[17] = "x1";
    

for(int a = 0; a < Array1.length; a++){  
   for(int x = 0; x < Array2.length; x++){
       String checkers = Array2[x].substring(1); 
       // ^^ Gets the letter after exp. 
       if(checkers.equals(Array1[a])){
            Array1[a] += Array2[x].substring(0,1);
        }
        else{

          }
        }
    }
    System.out.println(Arrays.toString(Array1));
  }
}

This comparison checkers.equals(Array1[a]) will not be true after the first appending to Array1[a] . To resolve this, it's beter to provide a temporary variable before the nested loop String num = Array1[a]; and compare to this variable:

for (int a = 0; a < Array1.length; a++) {
    String num = Array1[a];
    for (int x = 0; x < Array2.length; x++) {
        String checkers = Array2[x].substring(1);
        if (num.equals(checkers)) {
            Array1[a] += Array2[x].substring(0,1);
        }
    }
}

System.out.println(Arrays.toString(Array1));

Output:

[14T, 6in, 4st, 3aegr, 2o, 1ESfhlmpx]

This is because by the time you run if(checkers.equals(Array1[a])) to update Array1[a] a second time (like in the case for "6"), Array1[a] has already been updated with one of its letters (so for "6", Array1[a] becomes "6i"), and although checkers may equal "6", Array[1] now equals "6i". This is what stands in the way of continuously updating Array1[a] . To get around this, you can create a third array Array3 that has all the initial values of Array1 . This third array will be used in comparison to checkers .

Replace if(checkers.equals(Array1[a])) with: if(Array3[a].equals(checkers)) .

First of all, never use captial letters for your objects. These should only be used for class definitions.

Now to the case. Use regex and create a dictionary of arrays of the second array. The keys will be the numbers and the values will be the letters connected to those numbers. After that you can write out this info however you want:

import re

match_dict = {}

# Construct the match dict
for letter_digit in array2:
    # Use regex for string matching
    match_obj = re.match( r'([a-zA-Z]+)([0-9]+)', letter_digit, re.M|re.I)
    letter = match_obj.group(1)
    digit = match_obj.group(2)
    
    if digit in match_dict:
        match_dict[digit].append(letter)
    else:
        match_dict[digit] = [letter]

# Get only letters that match with array1
array1_matches = {}
for digit in array1:
    if digit in match_dict:
        array1_matches[digit] = match_dict[digit]
    else:
        # No idea what you want to do if there's no match
        array1_matches[digit] = None

# Print everything
for key, value in array1_matches.items():
    if value is not None:
        print(key, end='')
        for letter in value:
            print(letter, end='')
        print(",", end='')

I haven't tested the code myself, but it should do what you're after.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM