繁体   English   中英

如何从元组列表列表中删除项目,同时保留结构

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM