创建2D字符串数组

时间:2019-12-02 21:54:36

标签: python arrays string 2d

我正在尝试创建一个由国家名和省会城市组成的简单2D数组。在Java中,这很简单,但是我正在使用Python。

我想要一些类似的东西:

Scotland    Edinburgh
England    London
Wales     Cardiff

有什么想法吗?

4 个答案:

答案 0 :(得分:0)

例如,您可以创建两个数组,一个数组用于首都,一个数组用于国家,然后通过在相同索引下访问它们的值,您可以获取首都城市及其国家!

capitalCities= ["Edinburgh", "London", "Cardiff"]

countries= ["Scotland", "England", "Wales"]

答案 1 :(得分:0)

if (strpos($_SERVER['HTTP_HOST'], 'frontpage') !== false) {
    $frontstyle = frontstyle(); // you now have access to the styling values
    $worldstyle = $frontstyle['world'];
    $USA = $frontstyle['usa'];
}

不过,使用字典可能会更好:How to use a Python dictionary?

答案 2 :(得分:0)

我同意luckydog32,字典应该可以很好地解决这个问题。

capitolDict = {'england':'london', 'whales': 'cardiff'}
print(capitolDict.get('england'))

答案 3 :(得分:0)

首先,列出国家名称及其各自的首都。

countries = ['ScotLand','England','Wales']
capitals = ['Edinburgh','London','Cardiff']

然后,您可以将国家和首都压缩到一个元组列表中。在这里,元组是不可变的序列。

sequence = zip(countries,capitals)

现在,显示:

for country, capital in sequence:
    print(country,capital)