[英]How to remove items from a list of lists of lists of tuples, while preserving structure
我有一個 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)
你可以使用 zip 逆
text = list()
ints = list()
for i in data:
text_inner = list()
ints_inner = list()
for e in i:
t, n = zip(*e)
text_inner.append(list(t))
ints_inner.append(list(n))
text.append(text_inner)
ints.append(ints_inner)
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]]]
如果您想做一些就地提取(這意味着如果您想同時獲取文本和整數,則可能需要進行深度復制),可能會有更簡單的代碼。 如果結合列表理解,甚至可以使用一行代碼。
for i in data:
for j, e in enumerate(i):
t, n = zip(*e)
i[j] = list(t)
data
[[['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,']]]
您需要瀏覽列表並從中制作新列表。
# this will get you the list of the string
ls = []
for list1 in data:
for list2 in list1:
ls.append([x[0] for x in list2])
# will get you the list of integers.
ls = []
for list1 in data:
for list2 in list1:
ls.append([x[1] for x in list2])
如果你想避免兩個不同的 for 循環。
words, numb = [], []
for list1 in data:
for list2 in list1:
words.append([x[0] for x in list2])
numb.append([x[1] for x in list2])
print (words)
print (numb)
[['hey', 'how', 'are', 'you?'], ['I', 'am', 'fine,', 'and', 'you?'], -output - > ['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,']]
[[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]]
使用copy.deepcopy
功能的小技巧(保留初始“結構”):
from copy import deepcopy
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, ints = deepcopy(data), deepcopy(data)
for i, list_2d in enumerate(data):
for j, list_1d in enumerate(list_2d):
text[i][j] = [t[0] for t in list_1d]
ints[i][j] = [t[1] for t in list_1d]
print(text)
print(ints)
output:
[[['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,']]]
[[[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]]]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.