15

在 php 中,可以使用如下关联数组处理状态名称列表及其缩写:

<?php
    $stateArray = array(
        "ALABAMA"=>"AL",
        "ALASKA"=>"AK",
        // etc...
        "WYOMING"=>"WY"
    );

    foreach ($stateArray as $stateName => $stateAbbreviation){
        print "The abbreviation for $stateName is $stateAbbreviation.\n\n";
    }
?>

输出(保留键顺序):

The abbreviation for ALABAMA is AL.

The abbreviation for ALASKA is AK.

The abbreviation for WYOMING is WY.

编辑:请注意,数组元素的顺序保留在 php 版本的输出中。使用 HashMap 的 Java 实现不保证元素的顺序。Python中的字典也没有。

这是如何在 java 和 python 中完成的?我只找到提供值的方法,给定键,比如 python 的:

stateDict = {
    "ALASKA": "AK",
    "WYOMING": "WY",
}

for key in stateDict:
    value = stateDict[key]

编辑:根据答案,这是我在 python 中的解决方案,

# a list of two-tuples
stateList = [
    ('ALABAMA', 'AL'),
    ('ALASKA', 'AK'),
    ('WISCONSIN', 'WI'),
    ('WYOMING', 'WY'),
]

for name, abbreviation in stateList:
    print name, abbreviation

输出:

ALABAMA AL
ALASKA AK
WISCONSIN WI
WYOMING WY

这正是所需要的。

4

8 回答 8

34

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() + ".");
于 2009-08-02T19:07:31.160 回答
6

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()
            );
        }
    }
}
于 2009-08-02T19:13:14.843 回答
2

在 Python中,Python 2.7(尚未发布)和 Python 3.1 中提供了有序字典。它被称为 OrderedDict。

于 2009-08-02T20:49:19.763 回答
2

这是来自 o948 的修改后的代码,您使用 TreeMap 而不是 HashMap。Tree map 将按键保留键的顺序。

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()
            );
      }
    }
 }
于 2009-08-02T20:50:26.653 回答
2

此外,为了维护插入顺序,您可以使用 LinkedHashMap 而不是 HashMap。

于 2009-08-02T19:55:56.363 回答
1

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));
    }
于 2009-08-02T19:25:27.123 回答
1

按照亚历山大的回答...

本机 python 字典不维护其主要用途的最大效率的排序:键到值的无序映射。

我可以想到两种解决方法:

  1. 查看 OrderedDict 的源代码并将其包含在您自己的程序中。

  2. 制作一个按顺序保存键的列表:

    states = ['Alabamba', 'Alaska', ...]  
    statesd = {'Alabamba':'AL', 'Alaska':'AK', ...}
    for k in states:
        print "The abbreviation for %s is %s." % (k, statesd[k])
    
于 2009-08-02T21:57:32.267 回答
0

TreeMap 不是您问题的答案,因为它按键对元素进行排序,而 LinkedHashMap 保留原始顺序。但是,TreeMap 更适合字典,因为它是排序的。

于 2009-08-03T08:49:52.560 回答