我正在尝试遍历列表“已发送”。如果我的另一个列表“食物”在列表“已发送”中包含一个元素,我想将列表“食物”中的值附加到“结果”中,如果没有,则不附加任何内容。
到目前为止,我有:
result = []
sents = ['I love pizza. its my favorite', 'lets go get a sub', 'I just want a big fat steak']
food = ['sub', 'pizza', 'burger', 'noodles', 'candy']
我知道这应该很简单,但是我似乎无法弄清楚该怎么做。香港专业教育学院试图做到以下几点:
for x in sents:
for z in food:
if z in x:
result.append(z)
else:
result.append("None")
但这显然为食物中的每个列表项添加了一个元素。
我正试图让我的“结果”列表看起来像是:
result = ['pizza', 'sub', 'none']
任何帮助将不胜感激。
答案 0 :(得分:1)
首先,您必须解析1 2 3
1 7 3
0 1 3
1 5 3
并查找关键字(sents list
)。
找到这些关键字后,您可以将它们附加到结果列表中。
我的观点是来自另一种编程语言。
您必须将这一理论应用于Python。
(我会发表评论,但声誉得分不足)
答案 1 :(得分:1)
您可以尝试一下。
<?php $the_query = new WP_Query( array(
'post_type' => 'news',
'posts_per_page' => '10',
'orderby' => 'date',
'order' => 'DESC',
) );
if ( $the_query -> have_posts() ) while ( $the_query -> have_posts() ) : $the_query -> the_post(); ?>
<?php
while ( $the_query->have_posts() ) :
$the_query->the_post();
$link = get_field('external_link_url'); ?>
<div class="row mb-2">
<div class="col-md-10">
<h5 class="mb-0"><a class="text-primary" href="<?php echo $link; ?>" target="_blank"><?php echo get_the_title(); ?></a></h5>
<span class="text-muted"><?php echo get_the_excerpt(); ?> | <?php echo get_the_date(); ?></span>
</div>
</div>
<?php endwhile;
wp_reset_postdata();
?>
<?php endwhile; ?>
// second if statement and loop
<?php
$the_query = new WP_Query( array(
'post_type' => 'press-releases',
'posts_per_page' => '10',
'orderby' => 'date',
'order' => 'DESC',
) );
if ( $the_query->have_posts() ) while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php
while ( $the_query->have_posts() ) :
$the_query->the_post(); ?>
<div class="row mb-2">
<div class="col-md-10">
<h5 class="mb-0"><a class="text-primary" href="<?php echo the_permalink(); ?>"><?php echo get_the_title(); ?></a></h5>
<span class="text-muted"><?php echo get_the_date(); ?></span>
</div>
</div>
<?php endwhile;
wp_reset_postdata();
?>
<?php endwhile; ?>
答案 2 :(得分:0)
Result = list([x if sents.find(x) else 'none' for x in food])
#updated为string.find() #also,不考虑大小写差异