比较两个不同大小的列表并创建一个新列表

时间:2020-08-19 03:50:01

标签: python list compare

我有以下两个列表列表:

A=[['NUCPANT     500.00', 'MID500      500.00', '1 '], ['MID230      230.00', 'DOWNTN      230.00', '2 ']]
B=[['NUCPANT     500.00', 151], ['MID230      230.00', 153], ['DOWNTN      230.00', 154], ['MID500      500.00', 152]]

必需的输出:

C = [[151, 152, '1'],[153, 154, '2']]

我在输出中想要的是:

  1. 如果A中每个子列表的第一个元素('NUCPANT 500.00')等于B中子列表的第一个元素('NUCPANT 500.00'),则C中的子列表的第一个元素(151)应该是B子列表的第二个元素
  2. 如果A中每个子列表的第二个元素('MID500 500.00')等于B中子列表的第一个元素('MID500 500.00'),则C中的子列表的第一个元素(153)应为B子列表的第二个元素

预先感谢

1 个答案:

答案 0 :(得分:1)

我做了以下事情以获得想要的东西

public class MainActivity extends AppCompatActivity {
    

    // References
    private User user;
    private Room room;
    private String userKey;

    // Database References
    private FirebaseDatabase database;
    private DatabaseReference referenceUser, referenceUserStatus, referenceUserRoom, referenceRooms, referenceUserSlot;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        hideSystemUI();
        setContentView(R.layout.activity_main);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        userKey = getIntent().getStringExtra("userKey");
        Log.e("USER KEY: ", userKey);
        user = new User();
        user.setUserKey(userKey);

        // Database References
        database = FirebaseDatabase.getInstance();
        referenceUser = database.getReference("Users/"+user.getUserKey());
        referenceUserStatus = database.getReference("Users/"+user.getUserKey()+"/userStatus/");
        referenceRooms = database.getReference("Rooms");

            // Create Initial User
            referenceUser.setValue(user);
            referenceUserStatus.onDisconnect().setValue("OFFLINE");






        checkConnectionStatus();


    }

    private void searchForRooms(){

        final ProgressBar progressBarRoom = findViewById(R.id.progressBarRoom);
        final TextView textFindingRoom = findViewById(R.id.textSearchingForRoom);
        progressBarRoom.setVisibility(View.VISIBLE);
        textFindingRoom.setVisibility(View.VISIBLE);

        if(referenceUserSlot!= null) {
            referenceUserSlot.setValue("");
        }

        user.setUserSlot("");
        user.setUserRoomKey("");
        user.setUserStatus("ONLINE");
        referenceUser.setValue(user);

        referenceRooms.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                for(DataSnapshot dataSnapshot : snapshot.getChildren()){
                    Room roomFromList = dataSnapshot.getValue(Room.class);

                    int emptySlot = roomFromList.getEmptySlot();
                        if(emptySlot != 0 /* 0 means no empty slots */){

                            // Assign  room key to user
                            user.setUserRoomKey(roomFromList.getRoomKey());
                            user.setUserSlot("slot"+Integer.toString(emptySlot));

                            referenceUser.setValue(user);

                            // Assign User's key to slot
                            roomFromList.setSlotKey(user.getUserKey(), emptySlot);

                            // Set new value of room slot
                            dataSnapshot.getRef().setValue(roomFromList);

                            // MAKE SURE TO UPDATE REFERENCES SO USER HAS ACCURATE POINTERS TO DATABASE
                            updateDatabaseReferences();

                            // Make searching frame invisible
                            progressBarRoom.setVisibility(View.INVISIBLE);
                            textFindingRoom.setVisibility(View.INVISIBLE);


                            break;
                        }

                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        });



    }

    private void checkConnectionStatus(){
        DatabaseReference referenceConnection = database.getReference(".info/connected");
        referenceConnection.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                boolean isConnected = snapshot.getValue(Boolean.class);

                if(isConnected){
                        Log.e("CONNECTION", "REGAINED");
                        searchForRooms();
                }
                else{
                   // TRIGGERS ON STARTUP FOR SOME REASON?
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        });
    }

    private void updateDatabaseReferences(){

        referenceUserRoom = database.getReference("Rooms/"+user.getUserRoomKey());
        referenceUserSlot = database.getReference("Rooms/"+user.getUserRoomKey()+"/"+user.getUserSlot());

        referenceUserSlot.onDisconnect().setValue("");


    }

    public void hideSystemUI(){
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        getSupportActionBar().hide();
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
    }
}