我从以下内容中获得了良好的结果,但是如何从元组中提取数据?换句话说,我该如何清理数据?
这是数据库中的数据,我用完了。
>>> policy_id = ((2309L,), (118L,), (94L,))
>>> for i in policy_id:
print i
(2309L,)
(118L,)
(94L,)
但我希望结果如下:
2309
118
94
答案 0 :(得分:11)
policy_id = ((2309L,), (118L,), (94L,))
for i in policy_id:
print i[0]
答案 1 :(得分:3)
>>> from itertools import chain
>>> policy_id = ((2309L,), (118L,), (94L,))
>>> for i in chain.from_iterable(policy_id):
print i
2309
118
94
答案 2 :(得分:2)
print '\n'.join(str(x[0]) for x in policy_id)
答案 3 :(得分:2)
我喜欢的概念,可能会引起混淆,如下:
<强> Python2 强>
let dict = ["emailOK" : "Hello"]
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
do {
request.HTTPBody = try NSJSONSerialization.dataWithJSONObject(dict, options: NSJSONWritingOptions.PrettyPrinted)
} catch _ {
request.HTTPBody = nil
}
<强> Python3 强>
policy_id = ((2309L,), (118L,), (94L,))
for i, in policy_id:
print i
policy_id = ((2309,), (118,), (94,))
for i, in policy_id:
print(i)
之后,
解包单元素元组的元素(对于元素多于一元素的元组不起作用)。
答案 4 :(得分:1)
>>> policy_id = ((2309L,), (118L,), (94L,))
>>> print("\n".join(str(x[0]) for x in policy_id))
2309
118
94
答案 5 :(得分:0)
使用地图的其他方式
map(lambda x: str(x[0]), policy_id)
如果你想要换行
"\n".join(map(lambda x: str(x[0]), policy_id))