需要帮助以使用python列表为以下输入生成输出

时间:2018-10-03 08:04:45

标签: python python-2.7

输入数据如下:

0:70 1:60 2:66 3:62 4:72 5:82  6:83 7:86 8:95 9:96 10:98 11:98 12:96 13:94 14:66 15:62 16:60 17:66 18:67 19:67 20:64 21:66 22:66 23:69

使用python输出如下

enter image description here

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

70 60 66 62 72 82 83 86 95 96 98 98 96 94 66 62 60 66 67 67 64 66 66 69

2 个答案:

答案 0 :(得分:0)

我假设您的输入数据结构是python dict(关联数组)。您正在尝试在此dict中打印键和值的列表。下面的代码段将打印keysvalues

input = {0: 70, 1: 60, 2: 66, 3: 62, 4: 72, 5: 82, 6: 83, 7: 86, 8: 95, 9: 96, 10: 98, 11: 98, 12: 96, 13: 94, 14: 66, 15: 62, 16: 60, 17: 66, 18: 67, 19: 67, 20: 64, 21: 66, 22: 66, 23: 69}
# Below is the output with keys, values
print(input.keys())
print(input.values())

答案 1 :(得分:0)

代码(已更新):

import re

l = "0: 70 1: 60 2: 66 3: 62 4: 72 5: 82 6: 83 7: 86 8: 95 9: 96 10: 98 11: 98 12: 96 13: 94 14: 66 15: 62 16: 60 17: 66 18: 67 19: 67 20: 64 21: 66 22: 66 23: 69"

l = re.split("\s(?=\d+:)", l)

a = []
b = []
for x in l:
    a.append(x.split(": ")[0])
    b.append(x.split(": ")[1])

a = ["<td>" + x + "</td>" for x in a]
b = ["<td>" + x + "</td>" for x in b]

print("<table><tr>" + "".join(a) + "</tr><tr>" + "".join(b) + "</tr></table>")