我试图使用列表推导将嵌套列表划分为两个嵌套列表。如果不将内部列表转换为字符串,我无法这样做,这反过来又破坏了我以后访问/打印/控制值的能力。
我试过这个::
paragraphs3 = [['Page: 2', 'Bib: Something', 'Derived: This n that'], ['Page: 3', 'Bib: Something', 'Argument: Wouldn't you like to know?'], ...]
derived = [k for k in paragraphs3 if 'Derived:' in k]
therest = [k for k in paragraphs3 if 'Derived:' not in k]
会发生的是,整个paragraph3 = []最终会出现在where = []中,除非我这样做:
for i in paragraphs3:
i = str(i)
paragraphs4.append(i)
如果我然后将paragraph4提供给列表理解,我会得到两个列表,就像我想要的那样。但是它们不再是嵌套列表了:
for i in therest:
g.write('\n'.join(i))
g.write('\n\n')
写每个!角色! inst = []在一个单独的行中:
'
P
a
g
e
:
2
'
因此,我正在寻找一种更好的方法来分割段落3 ......或者解决方案可能在其他地方?我要找的最终结果/输出是:
Page: 2
Bib: Something
Derived: This n that
Page: 3
Bib: Something
.
.
.
答案 0 :(得分:2)
此代码根据子列表是否包含以'Derived:'
开头的字符串来分隔子列表。
paragraphs3 = [['Page: 2', 'Bib: Something', 'Derived: This n that'], ['Page: 3', 'Bib: Something', "Argument: Wouldn't you like to know?"], ]
def show(paragraphs):
for para in paragraphs:
print('\n'.join(para), '\n')
derived = []
therest = []
print('---input---')
show(paragraphs3)
for para in paragraphs3:
if any(item.startswith('Derived:') for item in para):
derived.append(para)
else:
therest.append(para)
print('---derived---')
show(derived)
print('---therest---')
show(therest)
---input---
Page: 2
Bib: Something
Derived: This n that
Page: 3
Bib: Something
Argument: Wouldn't you like to know?
---derived---
Page: 2
Bib: Something
Derived: This n that
---therest---
Page: 3
Bib: Something
Argument: Wouldn't you like to know?
此代码最重要的部分是
`any(item.startswith('Derived:') for item in para)`
这会迭代para
(当前段落)中的各个字符串,并在找到以True
开头的字符串后立即返回'Derived:'
。
FWIW,for
循环可以缩减为:
for para in paragraphs3:
(therest, derived)[any(item.startswith('Derived:') for item in para)].append(para)
因为False
和True
分别计算为0和1,所以它们可用于索引(therest, derived)
元组。然而,许多人会认为这是不可读的。 :)
答案 1 :(得分:0)
您编写的代码几乎是正确的。您需要检查列表的第3个元素中是否存在'Derived:'
。 k
基本上包含paragraphs3
>>> paragraphs3 = [['Page: 2', 'Bib: Something', 'Derived: This n that'], ['Page: 3', 'Bib: Something', 'Argument: Wouldn\'t you like to know?']]
>>> paragraphs3[0]
['Page: 2', 'Bib: Something', 'Derived: This n that']
>>> paragraphs3[0][2] # Here is where you want to check the condition
'Derived: This n that'
所以你要做的就是将条件改为if 'Derived:' in k[2]
。
>>> [k for k in paragraphs3 if 'Derived:' in k[2]]
[['Page: 2', 'Bib: Something', 'Derived: This n that']]
>>> [k for k in paragraphs3 if 'Derived:' not in k[2]]
[['Page: 3', 'Bib: Something', "Argument: Wouldn't you like to know?"]]
答案 2 :(得分:0)
'Dim i As Long
'For i = 5 To 25
'If Not IsEmpty(Sheets("RMDA").Range("D" & i)) Then _
Sheets("Overview").Range("D" & i) = Sheets("RMDA").Range("D" & i)
'Next i
复制整个列表:
derived = [l for l in paragraphs3 if any(filter(lambda k: 'Derived: ' in k, l))]
therest = [l for l in paragraphs3 if any(filter(lambda k: 'Derived: ' not in k, l))]
使用条件复制列表:
[l for l in paragraph3]
功能[l for l in paragraph3 if sublist_contains('Derived: ', l)]
尚未实现,所以让我们实现它。
仅检索与sublist_contains
匹配的项目:
condition_check
由于filter(condition_check, l)
可以表示为lambda函数:
condition_check
将结果转换为布尔值(如果找到至少一个匹配条件的项目,则为True):
filter(lambda k: 'Derived: ' in k, l)
用结果内联代码替换any(filter(lambda k: 'Derived: ' in k, l))
:
sublist_contains
答案 3 :(得分:0)
在我看来,这是最直接的做法:
[p for p in paragraphs3 if 'Derived:' in '\n'.join(p)]
[p for p in paragraphs3 if 'Derived:' not in '\n'.join(p)]
然而,如果你愿意的话,你可以获得更多的爱好者,并将其拉到一行(虽然它会比必要的更复杂)。
result = {k:[p for p in paragraphs3 if ('Derived:' in '\n'.join(p)) == test] for k,test in {'derived': True, 'therest': False}.items()}
这会产生dict
'derived'
和'therest'
作为键。现在你可以这样做:
for k,p in result.items():
print(k)
for i in p:
print(''.join(i))
答案 4 :(得分:0)
看起来你的内心清单有结构;列表本身是一个值,而不仅仅是一个不相关的值列表。考虑到这一点,您可以编写一个类来表示该数据。
paragraphs3 = [['Page: 2', 'Bib: Something', 'Derived: This n that'], ['Page: 3', 'Bib: Something', 'Argument: Wouldn\'t you like to know?'], ...]
class Paragraph(object):
def __init__(self, page, bib, extra):
self.page = page
self.bib = bib
self.extra = extra
@property
def is_derived(self):
return 'Derived: ' in self.extra
paras = [Paragraph(p) for p in paragraphs3]
然后,您可以使用itertools中的partition配方将该列表拆分为两个迭代器。
def partition(pred, iterable):
'Use a predicate to partition entries into false entries and true entries'
# partition(is_odd, range(10)) --> 0 2 4 6 8 and 1 3 5 7 9
t1, t2 = tee(iterable)
return filterfalse(pred, t1), filter(pred, t2)
(not_derived_paras, derived_paras) = partition(lambda p: p.is_derived, paras)