我目前能够通过使用大量for循环完成我想要的任务,但我觉得应该有一个更简单的方法。
我希望我的prog返回包含在字符串中找到的单词的列表名称。我现在正在做:
my_car = ["blue","ford"]
dads_car = ["green","audi"]
car = input('Tell me either the make or colour of your car: ').lower()
whos_car = ""
for word in my_car:
if word in car:
whos_car = "my_car"
else:
whos_car = "unknown"
for word in dads_car:
if word in car:
whos_car = "dads_car"
else:
whos_car = "unknown"
print(whos_car)
但我想要做的是写一个遍历每个列表的for循环,无论哪个列表包含该单词,都返回列表名称。 这不起作用,但希望它会更清楚:
for word in my_car,dads_car:
if word in car:
whos_car = the list name it found it in...
else:
whos_car = "Unknown"
希望这是有道理的。
非常感谢!
答案 0 :(得分:1)
使用字典和函数:
def get_list(cars, car):
car_parts = car.split()
for list_name, car_info in cars.items():
for item in car_info:
if item in car_parts:
return list_name
return 'unknown'
my_car = ["blue","ford"]
dads_car = ["green","audi"]
cars = {'my_car': my_car, 'dads_car': dads_car}
car = 'the car green'
print(get_list(cars, car))
现在,如果你设置:
car = 'the car green'
打印:
dads_car
但:
car = 'light-green truck'
打印:
unknown
看起来很适合字典:
my_car = ["blue","ford"]
dads_car = ["green","audi"]
cars = {'my_car': my_car, 'dads_car': dads_car}
car = 'green'
for list_name, car_info in cars.items():
if car in car_info:
print(list_name)
break
else:
print('unknown')
现在,如果你设置:
car = 'green'
打印:
dads_car
但:
car = 'yellow'
打印:
unknown
您可以根据需要向cars
添加任意数量的项目,而无需更改代码。所以没有大量的for
循环。
如果两个列表中都存在,那么它应该显示它找到的第一个列表。
一旦找到匹配,就会突破循环。
仅当else:
被执行时才会到达break
下的代码。
从Python 3.6开始。字典保持它们构建的顺序。
因此,首先会搜索my_car
。
具有不同逻辑的替代解决方案:
my_car = ["blue","ford"]
dads_car = ["green","audi"]
cars = {'my_car': my_car, 'dads_car': dads_car}
found = False
for list_name, car_info in cars.items():
for item in car_info:
if item in car:
print(list_name)
found = True
break
if found:
break
else:
print('unknown')
现在,如果你设置:
car = 'light-green'
打印:
dads_car
或更短的功能:
def get_list(cars, car):
for list_name, car_info in cars.items():
for item in car_info:
if item in car:
return list_name
return 'unknown'
print(get_list(cars, car))
答案 1 :(得分:0)
您可以使用locals():
def lookup(query):
my_car = ["blue","ford"]
dads_car = ["green","audi"]
for name, vals in locals().items():
# you need a condition to filter the variables you want to search in!
if name.endswith('_car') and query in vals:
return name
return 'unknown'
result = lookup('green')
print(result)
result = lookup('green2')
print(result)
打印:
dads_car
unknown
答案 2 :(得分:0)
一种简单的方法是在列表中找不到whos_car
时更改my_car = ["blue","ford"]
dads_car = ["green","audi"]
car = input('Tell me either the make or colour of your car: ').lower()
whos_car = None # None is more "Pythonic"
for word in my_car:
if word in car:
whos_car = "my_car"
break # no need to continue looping
if whos_cas is None: # don't search second list if word has already be found
for word in dads_car:
if word in car:
whos_car = "dads_car"
break
if whos_car is None: whos_car = "unknow" # if we haven't found anything...
print(whos_car)
的值,仅在第一个列表中找不到该词时才搜索第二个列表。您的代码可能只是变成:
manage.py
答案 3 :(得分:0)
我的解决方案如下,应该很容易理解:
你也可以按照建议使用whos_car结束forloop后的两次休息(如果你把这些列表放大的话)
代码:
my_car = ["blue", "ford"]
dads_car = ["green" , "audi"]
car = str.lower(input('Tell me either the brand or colour of your car: '))
for element in zip(my_car,dads_car):
if car in element[0]:
whos_car = "mine car now"
elif car in element[1]:
whos_car = "dads car"
else:
whos_car = "uknown"
print(whos_car)
输出:
user@user:~$ python3 script.py
Tell me either the brand or colour of your car: FORD
mine car now
答案 4 :(得分:0)
my_car = ["blue","ford"]
dads_car = ["green","audi"]
car = input('Tell me either the make or colour of your car: ').lower()
whos_car = "unknown"
for owner, lst in (('my_car', my_car), ('dads_car', dads_car)):
for word in lst:
if word == car:
whos_car = owner
break
print(whos_car)
这非常接近您的要求。所以owner和lst是for循环读取的每个元组的共同值。使用break可确保在找到所有者后不会覆盖whos_car。