如何在python中连接两个不同对象的元素

时间:2017-09-17 19:01:34

标签: python json

我在JSON中有以下数据。目的是给出下面给出的程序命令,以及城市名称,并搜索有关该特定城市的天气信息。 (我没有包含搜索天气数据的代码)

{"tag": "weather",
         "patterns": ["how will be weather today in" ,"What is the weather like in", "what is the weather in"],

         "responses": [""]

        }

我的python脚本

            if i['tag'] == 'weather':
               cities = [' karachi', ' lahore', ' islamabad', ' rawalpindi']
               for pattern in i['patterns']:
                    pattern+=cities
                   print(pattern)

它打印以下语句

how will be weather today in[' karachi', ' lahore', ' islamabad', ' rawalpindi']
What is the weather like in[' karachi', ' lahore', ' islamabad', ' rawalpindi']
what is the weather in[' karachi', ' lahore', ' islamabad', ' rawalpindi']

我想要它打印

What is the weather like in karachi
What is the weather like in islamabad
what is the weather like in rawalpindi

等等。我还尝试使用 pattern + = cities [0] 并打印

只有卡拉奇的天气如何,忽略了所有其他城市。我是python的新手,可能没有正确解释所有内容。希望有人可以帮助我。

1 个答案:

答案 0 :(得分:0)

你需要两个循环:

for pattern in i['patterns']:
    for city in cities:
        print(pattern + city)