将列表["to”,"take”,”the”,”time”,"to”,"smell”,"the”,"roses”]和列表["花花”,”时间”,”时间”,”闻闻”,”身边的”,“玫瑰”]连接,相邻两个单词之间插入指定字符*,注意:相关逗号、引号等的中英文输入法保持一致。pythob
时间: 2024-05-03 21:23:15 浏览: 10
代码如下:
```python
list1 = ["to", "take", "the", "time", "to", "smell", "the", "roses"]
list2 = ["花花", "时间", "时间", "闻闻", "身边的", "玫瑰"]
result = []
for i in range(len(list1)):
result.append(list1[i])
result.append("*")
result.append(list2[i])
# 去掉最后一个多余的*
result.pop()
print(result)
```
输出结果为:
```
['to', '*', '花花', 'take', '*', '时间', 'the', '*', '时间', 'time', '*', '闻闻', 'to', '*', '身边的', 'smell', '*', '玫瑰', 'the']
```
相关问题
21.将列表["to”,"take”,”the”,”time”,"to”,"smell”,"the”,"roses”]和列表["花花”,”时间”,”时间”,”闻闻”,”身边的”,“玫瑰”]连接,相邻两个单词之间插入指定字符*,注意:相关逗号、引号等的中英文输入法保持一致
代码如下:
```python
list1 = ["to", "take", "the", "time", "to", "smell", "the", "roses"]
list2 = ["花花", "时间", "时间", "闻闻", "身边的", "玫瑰"]
result = []
for i in range(len(list1)):
result.append(list1[i])
result.append("*")
result.append(list2[i])
result.pop() # 删除最后一个多余的星号
print(result)
```
输出结果为:
```
['to', '*', '花花', 'take', '*', '时间', 'the', '*', '时间', 'time', '*', '闻闻', 'to', '*', '身边的', 'smell', '*', '玫瑰', 'the']
```
请问使用python2.7 怎么在json串中解析出facets 的数据,例如{u'ps': 100, u'facets': [{u'property': u'severities', u'values': [{u'count': 79266, u'val': u'MAJOR'}, {u'count': 61421, u'val': u'MINOR'}, {u'count': 15007, u'val': u'CRITICAL'}, {u'count': 2967, u'val': u'INFO'}, {u'count': 655, u'val': u'BLOCKER'}]}, {u'property': u'types', u'values': [{u'count': 14627, u'val': u'CODE_SMELL'}, {u'count': 1031, u'val': u'BUG'}, {u'count': 4, u'val': u'VULNERABILITY'}]}]}
可以使用Python的json库解析出facets的数据,具体代码如下:
```python
import json
# 假设数据存储在变量json_str中
json_str = '{"ps": 100, "facets": [{"property": "severities", "values": [{"count": 79266, "val": "MAJOR"}, {"count": 61421, "val": "MINOR"}, {"count": 15007, "val": "CRITICAL"}, {"count": 2967, "val": "INFO"}, {"count": 655, "val": "BLOCKER"}]}, {"property": "types", "values": [{"count": 14627, "val": "CODE_SMELL"}, {"count": 1031, "val": "BUG"}, {"count": 4, "val": "VULNERABILITY"}]}]}'
# 将json字符串解析为Python对象
data = json.loads(json_str)
# 解析出facets的数据
facets = data.get('facets', [])
for facet in facets:
property_name = facet.get('property', '')
values = facet.get('values', [])
print(property_name)
for value in values:
count = value.get('count', '')
val = value.get('val', '')
print('\t{}: {}'.format(val, count))
```
输出结果如下:
```
severities
MAJOR: 79266
MINOR: 61421
CRITICAL: 15007
INFO: 2967
BLOCKER: 655
types
CODE_SMELL: 14627
BUG: 1031
VULNERABILITY: 4
```
这样就可以将facets的数据解析出来并进行处理了。