从列表生成字符串

时间:2016-05-09 21:11:00

标签: python python-2.7

我想从以下列表中获取值并将它们插入到字符串中。这是列表的格式:

[{u'level': {u'status': u'Automatic', u'lastReportedAt': u'2016-05-09T06:37:26Z', u'lowPercent': 10, u'highPercent': 10}, u'color': u'Black', u'type': u'Economy', u'carId': u'319142c0-c2dc-4498-8cb0-0cc9babdee56', u'engineNumber': u'24B6035', u'rrfType': u'Cab'}

输出字符串应该是这样的:

"Your car is an Automatic, Black, Economy".

“自动”,“黑色”和“经济”值都是列表中的值。这将在Python 2.7中运行。

1 个答案:

答案 0 :(得分:1)

<ul class="nav-justified">
  <li>
    <label>
      <input class="ageSelect" type="radio" id="timeSpan" name="timeSpan" value="0">
      <img class="ageSelectImg" src="css/img/checkbox_ofylld.png" />
      <br />0-2år</label>
  </li>
  <li>
    <label>
      <input class="ageSelect" type="radio" id="timeSpan" name="timeSpan" value="2">
      <img class="ageSelectImg" src="css/img/checkbox_ofylld.png" />
      <br />2-10 år</label>
  </li>
  <li>
    <label>
      <input class="ageSelect" type="radio" id="timeSpan" name="timeSpan" value="10">
      <img class="ageSelectImg" src="css/img/checkbox_ofylld.png" />
      <br />10+ år</label>
  </li>
</ul>

编辑以解决评论

for car in carList:
    print("Your car is a {}, {}, {}.").format(car['level']['status'], car['color'], car['type'])