python - 如何从元组列表列表中删除项目,同时保留结构
问题描述
我有一个 2 元组列表的列表。它看起来像这样:
data = [[[('hey', 9), ('how', 10), ('are', 7), ('you?', 21)], [('I', 0), ('am', 5), ('fine,', 8), ('and', 6), ('you?', 21)], [('I', 0), ('am', 5), ('fine,', 8), ('too.', 17)]], [[('My', 2), ('name', 13), ('is', 11), ('Jason,', 1), ("what's", 18), ('your', 22), ('name?', 14)], [('My', 2), ('name', 13), ('is', 11), ('Tina.', 4)], [('Nice', 3), ('to', 15), ('meet', 12), ('you.', 20)], [('Nice', 3), ('to', 15), ('meet', 12), ('you,', 19), ('too,', 16)]]]
如何按文本和整数拆分它,同时保留原始列表的结构?本质上,我想要:
text = [[['hey', 'how', 'are', 'you?'], ['I', 'am', 'fine,', 'and', 'you?'], ['I', 'am', 'fine,', 'too.']], [['My', 'name', 'is', 'Jason,', "what's", 'your', 'name?'], ['My', 'name', 'is', 'Tina.'], ['Nice', 'to', 'meet', 'you.'], ['Nice', 'to', 'meet', 'you,', 'too,']]]
ints = [[[9, 10, 7, 21], [0, 5, 8, 6, 21], [0, 5, 8, 17]], [[2, 13, 11, 1, 18, 22, 14], [2, 13, 11, 4], [3, 15, 12, 20], [3, 15, 12,19, 16]]]
解决方案
你可以这样试试。
data = [[[('hey', 9), ('how', 10), ('are', 7), ('you?', 21)], [('I', 0), ('am', 5), ('fine,', 8), ('and', 6), ('you?', 21)], [('I', 0), ('am', 5), ('fine,', 8), ('too.', 17)]], [[('My', 2), ('name', 13), ('is', 11), ('Jason,', 1), ("what's", 18), ('your', 22), ('name?', 14)], [('My', 2), ('name', 13), ('is', 11), ('Tina.', 4)], [('Nice', 3), ('to', 15), ('meet', 12), ('you.', 20)], [('Nice', 3), ('to', 15), ('meet', 12), ('you,', 19), ('too,', 16)]]]
text = []
inst = []
for p_data in data:
x = []
y = []
for q_data in p_data:
a = []
b = []
for tup in q_data:
a.append(tup[0])
b.append(tup[1])
x.append(a)
y.append(b)
text.append(x)
inst.append(y)
print(text)
print(inst)
推荐阅读
- javascript - AngularJS | 仅在满足条件时如何呈现 HTML 元素(指令)?
- ansible - ansible 使用 with_items 在 hostvars 中分配 shell 结果特定主机
- mysql - 如何使用字符串连接创建 SQL 过程?
- python-3.x - 错误:mdb_env_open: No such file or directory BLAST+ 本地数据库问题
- android - 在 App Distribution 中上传的新应用不会显示在 Crashlytics 中
- dependency-injection - 如何在 DI 中用模拟替换类?Phalcon + Codeception
- java - Java 更新后 Windows 身份验证无法连接
- angular - 当在 mat 菜单中启用 contenteditable 时,contentEditable.focus 需要 setTimeout 才能工作
- apache-kafka - 一些数据没有被 kafka-engine 消耗
- javascript - Mongoose:如何通过对象的键进行查询