Issue
So I have this two list, one with cities and one with lists of other infos. I would like to insert the name of the cities in index 0 of each list of the second list. Problem is at the end I have only one city name that is printed (the last one). Can somebody help me out with this ? Thank you !
cities = ["Paris", "London", "New York"]
data = [
["", "", "", "", 1, 1, "Buildings"],
["", "", "", "", 2, 1, "Streets"],
["", "", "", "", 3, 1, "Avenues"],
["", "", "", "", 1, 2, "Buildings"],
["", "", "", "", 2, 2, "Streets"],
["", "", "", "", 3, 2, "Avenues"],
]
result = []
counter = -1
while counter != len(cities) - 1:
counter += 1
number = -1
while number != len(data) - 1:
number += 1
data[number][0] = cities[counter]
result.append(data[number])
print(result)
gives me
result = [['New York', '', '', '', 1, 1, 'Buildings'],
['New York', '', '', '', 2, 1, 'Streets'],
['New York', '', '', '', 3, 1, 'Avenues'],
['New York', '', '', '', 1, 2, 'Buildings'],
['New York', '', '', '', 2, 2, 'Streets'],
['New York', '', '', '', 3, 2, 'Avenues'],
# (and so on)
]
while I'd rather have
result = [['New York', '', '', '', 1, 1, 'Buildings'],
['Paris', '', '', '', 2, 1, 'Streets'],
['Paris', '', '', '', 3, 1, 'Avenues'],
['Paris', '', '', '', 1, 2, 'Buildings'],
['Paris', '', '', '', 2, 2, 'Streets'],
['Paris', '', '', '', 3, 2, 'Avenues'],
['Paris', '', '', '', 1, 1, 'Buildings'],
['London', '', '', '', 2, 1, 'Streets'],
['London', '', '', '', 3, 1, 'Avenues'],
['London', '', '', '', 1, 2, 'Buildings'],
['London', '', '', '', 2, 2, 'Streets'],
['London', '', '', '', 3, 2, 'Avenues'],
['London', '', '', '', 1, 1, 'Buildings'],
['New York', '', '', '', 2, 1, 'Streets'],
['New York', '', '', '', 3, 1, 'Avenues'],
['New York', '', '', '', 1, 2, 'Buildings'],
['New York', '', '', '', 2, 2, 'Streets'],
['New York', '', '', '', 3, 2, 'Avenues']
]
Thank you
Solution
Here is some code that can help with your problem:
cities = ["Paris", "London", "New York"]
data = [
["", "", "", "", 1, 1, "Buildings"],
["", "", "", "", 2, 1, "Streets"],
["", "", "", "", 3, 1, "Avenues"],
["", "", "", "", 1, 2, "Buildings"],
["", "", "", "", 2, 2, "Streets"],
["", "", "", "", 3, 2, "Avenues"],
]
result = []
for i in cities:
for j in data:
result.append(j.copy()) #adding a copy of the list to result
result[len(result) - 1][0] = i #editing the list we just added (the now last element in result) so the first element is the city
#printing the result
for i in result:
print(i)
Output:
['Paris', '', '', '', 1, 1, 'Buildings']
['Paris', '', '', '', 2, 1, 'Streets']
['Paris', '', '', '', 3, 1, 'Avenues']
['Paris', '', '', '', 1, 2, 'Buildings']
['Paris', '', '', '', 2, 2, 'Streets']
['Paris', '', '', '', 3, 2, 'Avenues']
['London', '', '', '', 1, 1, 'Buildings']
['London', '', '', '', 2, 1, 'Streets']
['London', '', '', '', 3, 1, 'Avenues']
['London', '', '', '', 1, 2, 'Buildings']
['London', '', '', '', 2, 2, 'Streets']
['London', '', '', '', 3, 2, 'Avenues']
['New York', '', '', '', 1, 1, 'Buildings']
['New York', '', '', '', 2, 1, 'Streets']
['New York', '', '', '', 3, 1, 'Avenues']
['New York', '', '', '', 1, 2, 'Buildings']
['New York', '', '', '', 2, 2, 'Streets']
['New York', '', '', '', 3, 2, 'Avenues']
The way this code works is that:
For every city in the list cities
, we first add a copy of every list inside of data
to result
and then change the value of the first element to the city.
I hope this helped! Let me know if you need any further help or clarification :)
Answered By - Ani M
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.