数组作为位置参数
array as position argument
try:
target = int(input("Please enter the ammount you are looking for :"))
except ValueError:
print("wrong value please enter a number")
target = int(input("Please enter the ammount you are looking for :"))
found = False
location = [] # I want to use this list as position argument for another array passed from another function. is it possible?
for pos in range(0, len(wydatki)):
if wydatki[pos] == target:
found=True
location.append(pos)
if found==True:
#prints the locations in the list that the target was found
print (target,"\nappears in the following locations: ",months[location])
else:
print (target,"\nwas not found in the list.")
months[location] <------ 我想使用名为 location 的列表,该列表包含多个变量以将分配给列表中位置的值打印到屏幕上可以叫月吗?
通常你只能使用单个变量来指向数组中的位置?
如果你稍微改变一下你的输出就可以了:
target = 42
location = []
# 42 occures at positions 3,6,8
# 0 1 2 3 4 5 6 7 8 9
wydatki = [ 1,2,3,42,4,5,42,6,42,8]
# pos: 01234567890
months = list("abcdefghijk")
# get the positions unsing enumerate
for pos,value in enumerate(wydatki):
if value == target:
location.append(pos)
if location: # empty lists are False, lists with elements are True
#prints the locations in the list that the target was found
print (target,"\nappears in the following locations: ",
','.join( (months[a] for a in location) ) )
else:
print (target,"\nwas not found in the list.")
输出:
42
appears in the following locations: d,g,i
本质上,您需要将所有月份条目插入并连接到一个字符串中 - f.e。在 ","join( ... )
语句中使用生成器表达式。
如您所见,您无法传递列表并将其用作索引。
您必须遍历每个索引,或者构建一个完整的字符串并打印它。
例如
print(target,"\nappears in the following locations: ", end="")
for index in location:
print(months[index], end=" ")
print("")
end=""
意味着 print 将在末尾添加一个空字符串,而不是通常的新行。
此外,您还可以通过其他两种方式改进您的代码。
布尔值 found
可以对应于包含任何值的列表 location
,因此
if location: # an empty list evaluates to False
print("Found")
else:
print("Not found")
您的输入可能看起来像这样
target = None
done = False
while not done:
try:
target = int( input("Please enter the amount you are looking for:") )
done = True
except ValueError:
print("Wrong value, input a number.")
所以用户可以多次失败,程序将不会继续。
try:
target = int(input("Please enter the ammount you are looking for :"))
except ValueError:
print("wrong value please enter a number")
target = int(input("Please enter the ammount you are looking for :"))
found = False
location = [] # I want to use this list as position argument for another array passed from another function. is it possible?
for pos in range(0, len(wydatki)):
if wydatki[pos] == target:
found=True
location.append(pos)
if found==True:
#prints the locations in the list that the target was found
print (target,"\nappears in the following locations: ",months[location])
else:
print (target,"\nwas not found in the list.")
months[location] <------ 我想使用名为 location 的列表,该列表包含多个变量以将分配给列表中位置的值打印到屏幕上可以叫月吗?
通常你只能使用单个变量来指向数组中的位置?
如果你稍微改变一下你的输出就可以了:
target = 42
location = []
# 42 occures at positions 3,6,8
# 0 1 2 3 4 5 6 7 8 9
wydatki = [ 1,2,3,42,4,5,42,6,42,8]
# pos: 01234567890
months = list("abcdefghijk")
# get the positions unsing enumerate
for pos,value in enumerate(wydatki):
if value == target:
location.append(pos)
if location: # empty lists are False, lists with elements are True
#prints the locations in the list that the target was found
print (target,"\nappears in the following locations: ",
','.join( (months[a] for a in location) ) )
else:
print (target,"\nwas not found in the list.")
输出:
42
appears in the following locations: d,g,i
本质上,您需要将所有月份条目插入并连接到一个字符串中 - f.e。在 ","join( ... )
语句中使用生成器表达式。
如您所见,您无法传递列表并将其用作索引。
您必须遍历每个索引,或者构建一个完整的字符串并打印它。
例如
print(target,"\nappears in the following locations: ", end="")
for index in location:
print(months[index], end=" ")
print("")
end=""
意味着 print 将在末尾添加一个空字符串,而不是通常的新行。
此外,您还可以通过其他两种方式改进您的代码。
布尔值 found
可以对应于包含任何值的列表 location
,因此
if location: # an empty list evaluates to False
print("Found")
else:
print("Not found")
您的输入可能看起来像这样
target = None
done = False
while not done:
try:
target = int( input("Please enter the amount you are looking for:") )
done = True
except ValueError:
print("Wrong value, input a number.")
所以用户可以多次失败,程序将不会继续。