我要合并两个列表,一个是城市列表,另一个是州列表。
这是我的城市清单:
print(combined_city_list)
[['Los Angeles', 'Long Beach', 'Anaheim'], ['Dallas', 'Fort Worth'], ['Miami', 'Fort Lauderdale'], ['Minneapolis', 'St Paul'], ['Louisville', 'Jefferson County'], ['North Port', 'Sarasota', 'Bradenton'], ['Winston', 'Salem'], ['Santa Maria', 'Santa Barbara'], ['Crestview', 'Fort Walton Beach', 'Destin'], ['Macon', 'Bibb County'], ['Champaign', 'Urbana'], ['Lafayette', 'West Lafayette'], ['California', 'Lexington Park'], ['Pinehurst', 'Southern Pines'], ['Hermiston', 'Pendleton'], ['Wisconsin Rapids', 'Marshfield'], ['Arkansas City', 'Winfield']]
这是我的状态列表:
print(combined_state_list)
[[' CA'], [' TX'], [' FL'], [' MN'], [' KY'], [' FL'], [' NC'], [' CA'], [' FL'], [' GA'], [' IL'], [' IN'], [' MD'], [' NC'], [' OR'], [' WI'], [' KS']]
这是我尝试过的:
combined_state_short = [i[0] for i in combined_state_list]
combined_city_state = []
for c, s in zip (combined_city_list, combined_state_list):
for i in c:
combined_city_state = str(i) + ',' + str(s)
print(combined_city_state)
我将其作为输出:
Winfield,[' KS']
我想要这样的东西:
['Los Angeles, CA', 'Long Beach, CA' ...]
答案 0 :(得分:1)
以您的情况
newlist = [z + ',' + y[0] for x , y in zip(combined_city_list,combined_state_list) for z in x]
答案 1 :(得分:1)
这将起作用:
[city + ',' + state for (cities, (state, *_)) in zip(combined_city_list, combined_state_list) for city in cities]
答案 2 :(得分:0)
对于这种形式的初学者可能更清楚:
new_list = []
for cities, state in zip(combined_city_list, combined_state_list):
for city in cities:
new_list.append(city + ',' + state[0])
new_list