![](/img/trans.png)
[英]To get the list elements at odd places in a list made by using map function
[英]Made a list of inputs (places in my country) now i am trying to input temperatur on the places in the list
所以我已經完成了第一個任務,使用輸入制作一個地點列表
places = []
count = 0
max_places = 7
while True:
count += 1
new_places = input("Hi. Write and fill the list with 7 places: ")
places.append(new_places)
if count == max_places:
break
print(f"The 7 places are {places}"
接下來我想輸入我在地點列表中輸入的地點的溫度。
所以現在我得到:
完成后,我希望文本更改為
然后我希望它以一個包含地點和溫度的新列表結束。
我試過了
places = []
count = 0
max_places = 7
new_temp = 0
max_temp = 7
while True:
count += 1
new_places = input("Hi. Write and fill the list with 7 places: ")
places.append(new_places)
if count == max_places:
while True:
count += 1
temp = input(f"What is the temperature in {places[0]} ?" )
temp.append(int(new_temp))
if count == maks_temp:
break
只是為了開始,但我似乎不能將 append 與 int 一起使用。 所以當這個問題解決后,我需要找到一種方法來使循環 go 遍歷列表中的所有位置。 然后打印出地點和溫度
這樣的事情應該有效。 幾件事:
list_name.append(element_name)
places_list = []
temperatures_list = []
max_places = 3
while len(places_list) < max_places:
# populate list of places, break when reach the limit
new_place = input("Hi. Write and fill the list with 7 places: ")
places_list.append(new_place)
print(f"The 7 places are {places}")
# now that you have your list of places, go through them again and populate a different list for temperatures
for place in places_list:
temp = input(f"What is the temperature in {place}?")
temperatures_list.append(temp)
# print the corresponding places and temperatures using zip to navigate through the 2 lists together
for place, temp in zip(places_list, temperatures_list):
print(f"The temperature in {place} is {temp}")
或者,您可以使用包含位置溫度對的列表:
places_list = []
places_temperatures_list = []
max_places = 3
while len(places_list) < max_places:
# populate list of places, break when reach the limit
new_places = input("Hi. Write and fill the list with 7 places: ")
places_list.append(new_places)
print(f"The 7 places are {places_list}")
for place in places_list:
temp = input(f"What is the temperature in {place}?")
places_temperatures_list.append((place, temp))
for place_temp in places_temperatures_list:
print(f"The temperature in {place_temp[0]} is {place_temp[1]}")
例如,我使用城市溫度字典以便能夠迭代單個 object 並能夠在以后輕松重用這些值。 除了 first while True
你也可以使用for
。
max_places = 7
places = []
for _ in range(max_places):
new_place = input(
f'Hi. Write and fill the list with {max_places} places: ')
places.append(new_place)
matched_place_temp = dict()
for place in places:
temp = int(input(f'What is the temperature in {place} ?'))
matched_place_temp.update({place: temp})
print(*(f'The temperature in {place} is {temp}'
for place, temp in matched_place_temp.items()),
sep='\n')
減少解決方案:
max_places = 7
places = [
input(f'Hi. Write and fill the list with {max_places} places: ')
for _ in range(max_places)
]
matched_place_temp = {
place: int(input(f'What is the temperature in {place} ?'))
for place in places
}
print(*(f'The temperature in {place} is {temp}'
for place, temp in matched_place_temp.items()),
sep='\n')
places, temps = [], []
count = 0
while count < 7:
count += 1
new_places = input("Hi. Write and fill the list with 7 places: ")
places.append(new_places)
new_temp = input("What is the temperature in "+str(new_places)+" ?")
temps.append(new_temp)
places_temps = [{"Place": t, "Temperature": s} for t, s in zip(places, temps)]
print(places_temps)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.