在 python 中按特定顺序更改和排序列表中的列表
Change and Sort list in list in specific order in python
大家好,我在列表中有这种类型的数据
array = [
[
"PRODUCT NAME PACK",
"BAIGAM KOT",
"FIAZ BAGH",
"OLD ANARKALI",
"SULTAN PURA",
"TEZAB AAHATA",
"GHORAY SHAH",
"KOT KHAWAJA SAEED",
],
["002188", 0, 0, 0, 0, 0, "2", "3"],
[
"PRODUCT NAME PACK",
"BAGHBAN PURA",
"GAWAL MANDI",
"OLD ANARKALI",
"MAYO HOSPITAL",
"GARHI SHAHU BAZAR",
"BILAL GUNJ",
"LADY WELLINGTON HOSPITAL",
],
["002188", "3", 0, 0, 0, 0, 0, 0],
["008999", 0, 0, 0, 0, "1", 0, 0],
["012961", 0, 0, 0, 0, 0, "3", 0],
]
每个包含 "PRODUCT NAME PACK"
的列表都有实际的城市名称,它们的值放在下一个列表中,例如 "BAIGAM KOT"
在项目代码“002188”之后的值为 0。我想将它循环到 table 中,它在其中获取每一行中的值
[["002188", "BAIGAM KOT",0],["002188","FIAZ BAGH" ,0],["002188", "OLD ANARKALI",0]["002188", "SULTAN PURA",0],["002188","TEZAB AAHATA" ,0],["002188","GHORAY SHAH", "2"],["002188", "KOT KHAWAJA SAEED","3"]]
您需要 for
-循环 if/else
到 运行 不同列表的不同代码。
对于带有 "PRODUCT NAME PACK"
的列表,您必须将带有名称的列表保存在变量中 - 因此当您获得带有数字的列表时,您可以在下一个循环中使用它
对于其他列表,您可以将第一个数字保留为 index
,然后在 zip(names, numbers)
中使用其余的数字来创建对 (name, number)
,后者可用于创建 [index, name, number]
array = [
[
"PRODUCT NAME PACK",
"BAIGAM KOT",
"FIAZ BAGH",
"OLD ANARKALI",
"SULTAN PURA",
"TEZAB AAHATA",
"GHORAY SHAH",
"KOT KHAWAJA SAEED",
],
["002188", 0, 0, 0, 0, 0, "2", "3"],
[
"PRODUCT NAME PACK",
"BAGHBAN PURA",
"GAWAL MANDI",
"OLD ANARKALI",
"MAYO HOSPITAL",
"GARHI SHAHU BAZAR",
"BILAL GUNJ",
"LADY WELLINGTON HOSPITAL",
],
["002188", "3", 0, 0, 0, 0, 0, 0],
["008999", 0, 0, 0, 0, "1", 0, 0],
["012961", 0, 0, 0, 0, 0, "3", 0],
]
# --- convert ---
result = []
#names = None # default value at start
for data in array:
if data[0] == "PRODUCT NAME PACK":
names = data[1:]
else:
index = data[0]
numbers = data[1:]
for name, number in zip(names, numbers):
result.append( [index, name, number] )
# --- display ---
for item in result:
print(item)
结果:
['002188', 'BAIGAM KOT', 0]
['002188', 'FIAZ BAGH', 0]
['002188', 'OLD ANARKALI', 0]
['002188', 'SULTAN PURA', 0]
['002188', 'TEZAB AAHATA', 0]
['002188', 'GHORAY SHAH', '2']
['002188', 'KOT KHAWAJA SAEED', '3']
['002188', 'BAGHBAN PURA', '3']
['002188', 'GAWAL MANDI', 0]
['002188', 'OLD ANARKALI', 0]
['002188', 'MAYO HOSPITAL', 0]
['002188', 'GARHI SHAHU BAZAR', 0]
['002188', 'BILAL GUNJ', 0]
['002188', 'LADY WELLINGTON HOSPITAL', 0]
['008999', 'BAGHBAN PURA', 0]
['008999', 'GAWAL MANDI', 0]
['008999', 'OLD ANARKALI', 0]
['008999', 'MAYO HOSPITAL', 0]
['008999', 'GARHI SHAHU BAZAR', '1']
['008999', 'BILAL GUNJ', 0]
['008999', 'LADY WELLINGTON HOSPITAL', 0]
['012961', 'BAGHBAN PURA', 0]
['012961', 'GAWAL MANDI', 0]
['012961', 'OLD ANARKALI', 0]
['012961', 'MAYO HOSPITAL', 0]
['012961', 'GARHI SHAHU BAZAR', 0]
['012961', 'BILAL GUNJ', '3']
['012961', 'LADY WELLINGTON HOSPITAL', 0]
大家好,我在列表中有这种类型的数据
array = [
[
"PRODUCT NAME PACK",
"BAIGAM KOT",
"FIAZ BAGH",
"OLD ANARKALI",
"SULTAN PURA",
"TEZAB AAHATA",
"GHORAY SHAH",
"KOT KHAWAJA SAEED",
],
["002188", 0, 0, 0, 0, 0, "2", "3"],
[
"PRODUCT NAME PACK",
"BAGHBAN PURA",
"GAWAL MANDI",
"OLD ANARKALI",
"MAYO HOSPITAL",
"GARHI SHAHU BAZAR",
"BILAL GUNJ",
"LADY WELLINGTON HOSPITAL",
],
["002188", "3", 0, 0, 0, 0, 0, 0],
["008999", 0, 0, 0, 0, "1", 0, 0],
["012961", 0, 0, 0, 0, 0, "3", 0],
]
每个包含 "PRODUCT NAME PACK"
的列表都有实际的城市名称,它们的值放在下一个列表中,例如 "BAIGAM KOT"
在项目代码“002188”之后的值为 0。我想将它循环到 table 中,它在其中获取每一行中的值
[["002188", "BAIGAM KOT",0],["002188","FIAZ BAGH" ,0],["002188", "OLD ANARKALI",0]["002188", "SULTAN PURA",0],["002188","TEZAB AAHATA" ,0],["002188","GHORAY SHAH", "2"],["002188", "KOT KHAWAJA SAEED","3"]]
您需要 for
-循环 if/else
到 运行 不同列表的不同代码。
对于带有 "PRODUCT NAME PACK"
的列表,您必须将带有名称的列表保存在变量中 - 因此当您获得带有数字的列表时,您可以在下一个循环中使用它
对于其他列表,您可以将第一个数字保留为 index
,然后在 zip(names, numbers)
中使用其余的数字来创建对 (name, number)
,后者可用于创建 [index, name, number]
array = [
[
"PRODUCT NAME PACK",
"BAIGAM KOT",
"FIAZ BAGH",
"OLD ANARKALI",
"SULTAN PURA",
"TEZAB AAHATA",
"GHORAY SHAH",
"KOT KHAWAJA SAEED",
],
["002188", 0, 0, 0, 0, 0, "2", "3"],
[
"PRODUCT NAME PACK",
"BAGHBAN PURA",
"GAWAL MANDI",
"OLD ANARKALI",
"MAYO HOSPITAL",
"GARHI SHAHU BAZAR",
"BILAL GUNJ",
"LADY WELLINGTON HOSPITAL",
],
["002188", "3", 0, 0, 0, 0, 0, 0],
["008999", 0, 0, 0, 0, "1", 0, 0],
["012961", 0, 0, 0, 0, 0, "3", 0],
]
# --- convert ---
result = []
#names = None # default value at start
for data in array:
if data[0] == "PRODUCT NAME PACK":
names = data[1:]
else:
index = data[0]
numbers = data[1:]
for name, number in zip(names, numbers):
result.append( [index, name, number] )
# --- display ---
for item in result:
print(item)
结果:
['002188', 'BAIGAM KOT', 0]
['002188', 'FIAZ BAGH', 0]
['002188', 'OLD ANARKALI', 0]
['002188', 'SULTAN PURA', 0]
['002188', 'TEZAB AAHATA', 0]
['002188', 'GHORAY SHAH', '2']
['002188', 'KOT KHAWAJA SAEED', '3']
['002188', 'BAGHBAN PURA', '3']
['002188', 'GAWAL MANDI', 0]
['002188', 'OLD ANARKALI', 0]
['002188', 'MAYO HOSPITAL', 0]
['002188', 'GARHI SHAHU BAZAR', 0]
['002188', 'BILAL GUNJ', 0]
['002188', 'LADY WELLINGTON HOSPITAL', 0]
['008999', 'BAGHBAN PURA', 0]
['008999', 'GAWAL MANDI', 0]
['008999', 'OLD ANARKALI', 0]
['008999', 'MAYO HOSPITAL', 0]
['008999', 'GARHI SHAHU BAZAR', '1']
['008999', 'BILAL GUNJ', 0]
['008999', 'LADY WELLINGTON HOSPITAL', 0]
['012961', 'BAGHBAN PURA', 0]
['012961', 'GAWAL MANDI', 0]
['012961', 'OLD ANARKALI', 0]
['012961', 'MAYO HOSPITAL', 0]
['012961', 'GARHI SHAHU BAZAR', 0]
['012961', 'BILAL GUNJ', '3']
['012961', 'LADY WELLINGTON HOSPITAL', 0]