java and python equivalent of php's foreach($array as $key => $value)

前端 未结 8 971
抹茶落季
抹茶落季 2020-12-20 11:56

In php, one can handle a list of state names and their abbreviations with an associative array like this:



        
相关标签:
8条回答
  • 2020-12-20 12:27

    in java for associative array use Map

    import java.util.*;
    
    class Foo
    {
        public static void main(String[] args)
        {
            Map<String, String> stateMap = new HashMap<String, String>();
            stateMap.put("ALABAMA", "AL");
            stateMap.put("ALASKA", "AK");
            // ...
            stateMap.put("WYOMING", "WY");
    
            for (Map.Entry<String, String> state : stateMap.entrySet()) {
                 System.out.printf(
                    "The abbreviation for %s is %s%n",
                    state.getKey(),
                    state.getValue()
                );
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-20 12:29

    This is the modified code from o948 where you use a TreeMap instead of a HashMap. The Tree map will preserve the ordering of the keys by the key.

    import java.util.*;
    
    class Foo
    {
     public static void main(String[] args)
     {
        Map<String, String> stateMap = new TreeMap<String, String>();
        stateMap.put("ALABAMA", "AL");
        stateMap.put("ALASKA", "AK");
        // ...
        stateMap.put("WYOMING", "WY");
    
        for (Map.Entry<String, String> state : stateMap.entrySet()) {
                 System.out.printf(
                        "The abbreviation for %s is %s%n",
                        state.getKey(),
                        state.getValue()
                );
          }
        }
     }
    
    0 讨论(0)
  • 2020-12-20 12:30

    Along the lines of Alexander's answer...

    The native python dictionary doesn't maintain ordering for maximum efficiency of its primary use: an unordered mapping of keys to values.

    I can think of two workarounds:

    1. look at the source code of OrderedDict and include it in your own program.

    2. make a list that holds the keys in order:

      states = ['Alabamba', 'Alaska', ...]  
      statesd = {'Alabamba':'AL', 'Alaska':'AK', ...}
      for k in states:
          print "The abbreviation for %s is %s." % (k, statesd[k])
      
    0 讨论(0)
  • 2020-12-20 12:41

    in Python:

    for key, value in stateDict.items(): # .iteritems() in Python 2.x
        print "The abbreviation for %s is %s." % (key, value)
    

    in Java:

    Map<String,String> stateDict;
    
    for (Map.Entry<String,String> e : stateDict.entrySet())
        System.out.println("The abbreviation for " + e.getKey() + " is " + e.getValue() + ".");
    
    0 讨论(0)
  • 2020-12-20 12:44

    Another way of doing it in Java. Although a better way has already been posted, this one's syntactically closer to your php code.

    for (String x:stateDict.keySet()){
            System.out.printf("The abbreviation for %s is %s\n",x,stateDict.get(x));
        }
    
    0 讨论(0)
  • 2020-12-20 12:45

    In python an ordered dictionary is available in Python 2.7 (not yet released) and Python 3.1. It's called OrderedDict.

    0 讨论(0)
提交回复
热议问题