>>>lis = df['col'].values.tolist()
>>>lis = [[(('A', 'WE'), ('1,21', 'rr'), ('io', 'mp'))],
[(('B', 'WE'), ('5', 'rr'), ('io', 'mp'))],
[(('A', 'WE'), ('3', 'rr'), ('io', 'mp')),
(('C', 'WE'), ('0', 'rr'), ('io', 'mp'))],
....
[(('D', 'WE'), ('6', 'rr'), ('io', 'mp'))],
[(('A', 'WE'), ('9.0', 'rr'), ('io', 'mp'))]]
如何只获取每个元组的第一个元素并将其重新格式化为:
[[A, 1,21, io],
[B, 5, io],
[A, 3, io],
[C, 0, io],
....
[D, 6, io],
[A, 9.0', io]]
我已经准备好了:
[' '.join(map(str,lis[0][0])) for x in lis]
和
[' '.join(map(str,lis[0][:1])) for x in lis]
和
' '.join(map(str, lis))
和
new_lis, _ = zip(*lis[0][0])
return ' '.join(new_lis)
更新
pandas列如下所示:
Col
0 [(('A', 'WE'), ('1,21', 'rr'), ('io', 'mp'))]
1 [(('B', 'WE'), ('5', 'rr'), ('io', 'mp'))]
2 [(('A', 'WE'), ('3', 'rr'), ('io', 'mp'))
3 [(('C', 'WE'), ('0', 'rr'), ('io', 'mp'))]
....
n [(('D', 'WE'), ('6', 'rr'), ('io', 'mp'))]
n-1 [(('A', 'WE'), ('9.0', 'rr'), ('io', 'mp'))]
答案 0 :(得分:3)
lis = [[(('A', 'WE'), ('1,21', 'rr'), ('io', 'mp'))],
[(('B', 'WE'), ('5', 'rr'), ('io', 'mp'))],
[(('A', 'WE'), ('3', 'rr'), ('io', 'mp')),
(('C', 'WE'), ('0', 'rr'), ('io', 'mp'))],
[(('D', 'WE'), ('6', 'rr'), ('io', 'mp'))],
[(('A', 'WE'), ('9.0', 'rr'), ('io', 'mp'))]]
print [list(next(zip(*tup))) for subl in lis for tup in subl ]
输出:
[['A', '1,21', 'io'],
['B', '5', 'io'],
['A', '3', 'io'],
['C', '0', 'io'],
['D', '6', 'io'],
['A', '9.0', 'io']]
答案 1 :(得分:1)
你可以尝试这个:
new_list = [[nested[0] for nested in sub_l] for l in lis for sub_l in l]
print(new_list)
[['A', '1,21', 'io'],
['B', '5', 'io'],
['A', '3', 'io'],
['C', '0', 'io'],
['D', '6', 'io'],
['A', '9.0', 'io']]
它不是很易读,但它可以满足您的需求。
<强>更新强> 如果您想拥有一个字符串列表,可以使用以下代码:
[",".join(nested[0] for nested in sub_l) for l in lis for sub_l in l]