Home > Software design >  Add info from one list to another
Add info from one list to another

Time:02-24

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

CodePudding user response:

Don't try to start from a template. Python makes that hard, because you end up with references rather than copies. Instead, build it out like this:

from pprint import pprint

cities = ["Paris", "London", "New York"]
layers = ['Buildings','Streets','Avenues']
data = []
for city in cities:
    for i in (1,2):
        for j,layer in enumerate(layers)
            data.append( [city, '', '', '', j 1, i, layer] )
pprint(data)

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']]

CodePudding user response:

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 :)

CodePudding user response:

You can do this with a one liner using list comprehension.

result = [[city]   el[1:] for el in data for city in cities]

This is basically a double for loop, where we iterate over the cities in the first one (for city in cities) and then for each iteration, we iterate over the elements of data (for el in data). Then for each iteration, we concatenate the city in a list ([city] with the element of data with the entry at index 0 removed (el[:1]). In Python, list concatenation can be done using .

I suggest you to learn the "Pythonic" way of manipulating lists, it will make your code shorter and easier to understand.

CodePudding user response:

this is your answer

result_1 = []
result_2 = []
for cities_index, cities in enumerate(cities):
    for line_data in data:
        print(cities)
        line_1 = line_data.copy()
        line_1[0] = cities
        print(line_1)
        result_1.append(line_1)
    result_2.extend(result_1)
print(result_2)
  • Related