有没有办法根据条件语句从各种列表中删除项?

2024-04-25 22:57:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在写一个程序,将作为一个纽约摄影师摄影创意发电机。它现在的工作方式非常简单,代码利用了随机选择函数从列表中随机抽取项目,然后代码以一种形成英语句子的方式打印出来作为最终结果。你知道吗

我的问题是我需要添加一些逻辑,因为一些结果对摄影师来说没有意义(至少在我看来)。在本例中,如果python选择主题项时“肖像”碰巧是随机选择的,那么我试图从technique\u列表中删除“Bracketed(HDR)”。你知道吗

我觉得我在条件if语句中错误地使用了.remove函数。有没有更好的办法?我已附上守则的有关部分以供检查。你知道吗

我试过这个技巧_列表.删除(‘带括号(HDR)’)以及

del\u 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)永远不应该是同一个结果的一部分,这在这种情况下是没有意义的。你知道吗


Tags: ofthe代码列表ifhdrvarlocation
3条回答

这个问题(我认为)是因为你在迭代随机选择的结果而不是列表本身,你不需要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

我应该这么做

我会用一本关于不恰当技巧的词典,一份理解列表,然后用一个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}.")

请允许我补充一下,并对答案作更多的解释

  1. 如果选择了potrait,则需要删除“bracked(HDR)”。不要使用.remove,因为它将永久删除“bracked(HDR)”并阻止其他主题使用该技术。你可以像金菲舍尔建议的那样使用不恰当的技巧词典

  2. 你知道吗随机选择从列表中输出一个值。您不应该将for循环与它一起使用,因为for循环将遍历由输出的值中的字符/字母随机选择

  3. 如果我可以给你一个反馈,你的代码片段中的缩进是相当混乱的。有些行应该有缩进,但没有。我不知道。。也许这是无意的,问题出在我的浏览器上。如果是这样,对不起!

相关问题 更多 >