所以我有這兩個串列,一個包含城市,一個包含其他資訊串列。我想在第二個串列的每個串列的索引 0 中插入城市名稱。問題是最后我只有一個列印的城市名稱(最后一個)。有人可以幫我解決這個問題嗎?謝謝 !
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)
給我
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)
]
而我寧愿有
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']
]
謝謝
uj5u.com熱心網友回復:
不要試圖從模板開始。Python 讓這很困難,因為你最終得到的是參考而不是副本。相反,像這樣構建它:
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)
輸出:
[['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']]
uj5u.com熱心網友回復:
以下是一些可以幫助您解決問題的代碼:
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)
輸出:
['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']
這段代碼的作業方式是:
對于串列中的每個城市,我們首先在tocities中添加每個串列的副本,然后將第一個元素的值更改為城市。dataresult
我希望這有幫助!如果您需要任何進一步的幫助或澄清,請告訴我:)
uj5u.com熱心網友回復:
您可以使用list comprehension使用單行來執行此操作。
result = [[city] el[1:] for el in data for city in cities]
這基本上是一個雙 for 回圈,我們迭代第一個中的城市 ( for city in cities),然后對于每次迭代,我們迭代資料的元素 ( for el in data)。然后對于每次迭代,我們將城市連接到一個串列中 ([city]洗掉索引 0 處的條目的資料元素 ( el[:1])。在 Python 中,可以使用 .
我建議你學習處理串列的“Pythonic”方式,它會讓你的代碼更短更容易理解。
uj5u.com熱心網友回復:
這是你的答案
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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/432198.html
標籤:Python python-3.x 列表
