Python:按一定顺序取出给定数组的元素
Python: pull out elements of given array in certain order
我有一个包含名称、地址、城市、州和邮政编码的数组。有没有办法可以集中使用这些元素并以名称,地址,城市,州,邮政编码的格式逐行显示信息?另外我不确定如何去掉数组末尾的 space。
这是数组:
[['Alex Morales', ''], ['311 N Sangamon St', ''], ['Chicago, IL 60607', ''],
['Delfino Santana', ''], ['1 Main St', ''], ['Belvedere Tiburon, CA 94920', ''],
['Ponce De Leon', ''], ['74 King St', ''], ['St. Augustine, FL 32084', ''],
['Coit Tower', ''], ['1 Telegraph Hill Blvd', ''], ['San Francisco, CA 94133']]
我是 Python 的初学者,所以请不要使用疯狂的语法!
>>> for name, address, csp in zip(*(iter(i[0] for i in arr),) * 3):
... print(f"Name: {name} Address: {address} City/State/Postal: {csp}")
...
Name: Alex Morales Address: 311 N Sangamon St City/State/Postal: Chicago, IL 60607
Name: Delfino Santana Address: 1 Main St City/State/Postal: Belvedere Tiburon, CA 94920
Name: Ponce De Leon Address: 74 King St City/State/Postal: St. Augustine, FL 32084
Name: Coit Tower Address: 1 Telegraph Hill Blvd City/State/Postal: San Francisco, CA 94133
棘手的 zip
内容是必需的,因为数据采用棘手的格式。最好将此数据放入(例如)csv 文件中,这样您就可以将相关信息组合在一起,避免事后 zip
它的需要。
通过以 3 的步长遍历列表来创建字典列表:
l = [['Alex Morales', ''], ['311 N Sangamon St', ''], ['Chicago, IL 60607', ''],
['Delfino Santana', ''], ['1 Main St', ''], ['Belvedere Tiburon, CA 94920', ''],
['Ponce De Leon', ''], ['74 King St', ''], ['St. Augustine, FL 32084', ''],
['Coit Tower', ''], ['1 Telegraph Hill Blvd', ''], ['San Francisco, CA 94133']]
info = [{"name": l[i][0], "street": l[i+1][0], "city": l[i+2][0]}
for i in range(0, len(l), 3)]
现在你已经掌握了每本词典的所有相关信息,你可以打印出来:
for x in info:
print(f"Name: {x['name']}\nAddress: {x['street']} x['city']}\n")
我有一个包含名称、地址、城市、州和邮政编码的数组。有没有办法可以集中使用这些元素并以名称,地址,城市,州,邮政编码的格式逐行显示信息?另外我不确定如何去掉数组末尾的 space。
这是数组:
[['Alex Morales', ''], ['311 N Sangamon St', ''], ['Chicago, IL 60607', ''],
['Delfino Santana', ''], ['1 Main St', ''], ['Belvedere Tiburon, CA 94920', ''],
['Ponce De Leon', ''], ['74 King St', ''], ['St. Augustine, FL 32084', ''],
['Coit Tower', ''], ['1 Telegraph Hill Blvd', ''], ['San Francisco, CA 94133']]
我是 Python 的初学者,所以请不要使用疯狂的语法!
>>> for name, address, csp in zip(*(iter(i[0] for i in arr),) * 3):
... print(f"Name: {name} Address: {address} City/State/Postal: {csp}")
...
Name: Alex Morales Address: 311 N Sangamon St City/State/Postal: Chicago, IL 60607
Name: Delfino Santana Address: 1 Main St City/State/Postal: Belvedere Tiburon, CA 94920
Name: Ponce De Leon Address: 74 King St City/State/Postal: St. Augustine, FL 32084
Name: Coit Tower Address: 1 Telegraph Hill Blvd City/State/Postal: San Francisco, CA 94133
棘手的 zip
内容是必需的,因为数据采用棘手的格式。最好将此数据放入(例如)csv 文件中,这样您就可以将相关信息组合在一起,避免事后 zip
它的需要。
通过以 3 的步长遍历列表来创建字典列表:
l = [['Alex Morales', ''], ['311 N Sangamon St', ''], ['Chicago, IL 60607', ''],
['Delfino Santana', ''], ['1 Main St', ''], ['Belvedere Tiburon, CA 94920', ''],
['Ponce De Leon', ''], ['74 King St', ''], ['St. Augustine, FL 32084', ''],
['Coit Tower', ''], ['1 Telegraph Hill Blvd', ''], ['San Francisco, CA 94133']]
info = [{"name": l[i][0], "street": l[i+1][0], "city": l[i+2][0]}
for i in range(0, len(l), 3)]
现在你已经掌握了每本词典的所有相关信息,你可以打印出来:
for x in info:
print(f"Name: {x['name']}\nAddress: {x['street']} x['city']}\n")