有没有一种方法可以根据条件语句从各种列表中删除项目?

时间:2019-07-04 12:18:50

标签: python list

我正在编写一个程序,该程序将为纽约摄影师提供摄影创意。现在它的工作方式非常简单,代码利用random.choice函数从列表中随机提取项目,然后代码以最终形成英语句子的方式打印出来。

我的问题是我需要为此添加一些逻辑,因为某些结果对于摄影师来说是没有意义的(至少在我看来)。在此示例中,我试图从technique_list中删除“ Bracketed(HDR)”,如果python选择主题项时随机选择了“ Portrait”,则为“ Portrait”。

我觉得我在条件if语句中误用了.remove函数。有一个更好的方法吗?我已附上代码的相关部分进行检查。

我尝试了technique_list.remove('Bracketed(HDR)')以及

del technique_list [0],都作为if语句的响应部分。

import random 

print ("You should try taking a...")

#pool of items that the program will randomly choose..
theme_list = ['Cityscape','Peoplescape','Port-Scape', 'Portrait']
technique_list  = ['Bracketed (HDR)','Wide Angle', 'Zoom','Long 
Exposure','Fast Shutter','Daytime Long Expo','Timelapse']


#what we need here are conditional IF statements,  that manipulate items 
from various lists

#this bit of code determines the theme of a photo idea
theme_var = random.choice(theme_list) 
for theme in theme_var:
if theme == 'Portrait':
        technique_list.remove('Bracketed (HDR)') 
print("",theme_var)

#this bit of code determines the technique of a photo idea
technique_var = random.choice(technique_list)
print("", technique_var)

print("picture, from")

#this line of code determines the location of a photo idea
location_var = random.choice(location_list)
print("", location_var) 

这仍然是代码的可能结果之一:

You should try taking a...
 Portrait
 Bracketed (HDR)
picture, from
 34th Street
during
 Sunrise
and then give it a
 Black & White
edit in Lightroom!
[Finished in 0.2

正如我之前说的,肖像和包围曝光(HDR)永远不应成为同一结果的一部分,对于这种情况没有意义。

3 个答案:

答案 0 :(得分:1)

问题(我认为)是因为您要遍历随机选择的结果而不是列表本身,所以不需要for循环。

theme_var = random.choice(theme_list) 

if theme_var == 'Portrait':
        technique_list.remove('Bracketed (HDR)') 
print("",theme_var)

#this bit of code determines the technique of a photo idea
technique_var = random.choice(technique_list)
print("", technique_var)

print("picture, from")

#rest of the code

应该这样做

答案 1 :(得分:0)

我会使用一本不适当的技术字典,一个列表理解方法,并以f字符串开头:

import random 

#pool of items that the program will randomly choose..
theme_list = ['Cityscape','Peoplescape','Port-Scape', 'Portrait']
technique_list  = ['Bracketed (HDR)','Wide Angle', 'Zoom','Long Exposure','Fast 
Shutter','Daytime Long Expo','Timelapse']
location_list = ['34th Street']

# dictionary of inappropriate techniques for given theme
d_inappropes = {'Cityscape': [],
            'Port-Scape': [],
            'Portrait': ['Bracketed (HDR)'],
            'Peoplescape': ['Long Exposure', 'Timelapse', 'Daytime Long Expo']}

#this bit of code determines the theme of a photo idea
theme_var = random.choice(theme_list) 

#this bit of code determines the technique of a photo idea
# list comprehension generates a new list with the inappropriate techniques removed,
# without affecting the original list
technique_var = random.choice([ti for ti in technique_list if ti not in d_inappropes[theme_var]])

#this line of code determines the location of a photo idea
location_var = random.choice(location_list)

# use f-stirngs to put the whole output in a single line to keep it managable 
print(f"You should try taking a \n {theme_var} {technique_var} picture, \n from 
{location_var}.")

答案 2 :(得分:0)

如果我可以添加答案并给出更多解释

  1. 如果选择了potrait,则要删除“括号(HDR)”。不要使用.remove,因为它将永久删除“括号(HDR)”并阻止其他主题使用该技术。您可以使用Kingfischer建议的不适当技术词典

  2. random.choice从列表中输出了一个值。您不应该使用for循环,因为for循环会在random.choice

  3. 输出的值中迭代字符/字母
  4. 如果我可以提供反馈,那么您的代码片段中的缩进非常混乱。有些行应该有缩进,没有缩进。我不知道..也许是意料之外的,并且问题出在我的浏览器上。如果是这样,对不起!

相关问题