java和python相当于php的foreach($array as $key => $value)
在 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
这正是所需要的。
In php, one can handle a list of state names and their abbreviations with an associative array like this:
<?php
$stateArray = array(
"ALABAMA"=>"AL",
"ALASKA"=>"AK",
// etc...
"WYOMING"=>"WY"
);
foreach ($stateArray as $stateName => $stateAbbreviation){
print "The abbreviation for $stateName is $stateAbbreviation.\n\n";
}
?>
Output (with key order preserved):
The abbreviation for ALABAMA is AL.
The abbreviation for ALASKA is AK.
The abbreviation for WYOMING is WY.
EDIT: Note that the order of array elements is preserved in the output of the php version. The Java implementation, using a HashMap, does not guarantee the order of elements. Nor does the dictionary in Python.
How is this done in java and python? I only find approaches that supply the value, given the key, like python's:
stateDict = {
"ALASKA": "AK",
"WYOMING": "WY",
}
for key in stateDict:
value = stateDict[key]
EDIT: based on the answers, this was my solution in python,
# a list of two-tuples
stateList = [
('ALABAMA', 'AL'),
('ALASKA', 'AK'),
('WISCONSIN', 'WI'),
('WYOMING', 'WY'),
]
for name, abbreviation in stateList:
print name, abbreviation
Output:
ALABAMA AL
ALASKA AK
WISCONSIN WI
WYOMING WY
Which is exactly what was required.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
在Python中:
在Java中:
in Python:
in Java:
在java中关联数组使用Map
in java for associative array use Map
此外,为了保持插入顺序,您可以使用 LinkedHashMap 而不是 HashMap。
Also, to maintain insertion order, you can use a LinkedHashMap instead of a HashMap.
在Python中,有序字典在Python 2.7中可用(尚未发布)和Python 3.1。 它被称为 OrderedDict。
In python an ordered dictionary is available in Python 2.7 (not yet released) and Python 3.1. It's called OrderedDict.
这是来自 o948 的修改后的代码,其中使用 TreeMap 而不是 HashMap。 树形图将按键保留键的排序。
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.
Java 中的另一种方法。 尽管已经发布了更好的方法,但这个方法在语法上更接近您的 php 代码。
Another way of doing it in Java. Although a better way has already been posted, this one's syntactically closer to your php code.
沿着亚历山大的回答......
原生Python字典不维护其主要用途的最大效率的排序:键到值的无序映射。
我可以想到两种解决方法:
查看 OrderedDict 的源代码并将其包含在您自己的程序中。
创建一个按顺序保存键的列表:
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:
look at the source code of OrderedDict and include it in your own program.
make a list that holds the keys in order:
TreeMap 不是您问题的答案,因为它按键对元素进行排序,而 LinkedHashMap 保留原始顺序。 然而,TreeMap 因为排序而更适合字典。
TreeMap is not an answer to your question because it sorts elements by key, while LinkedHashMap preserves original order. However, TreeMap is more suitable for the dictionary because of sorting.