Change and Sort list in list in specific order in python

2024/7/4 16:04:17

Hello guys I have this type of data in list in list

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

Each list that contain "PRODUCT NAME PACK" has actual city name and their values are placed in next list like "BAIGAM KOT" has value 0 after the item code "002188". I want to loop through it into table in which it get the value in each row like that

[["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"]]
Answer

You need for-loop with if/else to run different code for different lists.

For list with "PRODUCT NAME PACK" you have to keep list with names in variable - so you may use it in next loops when you get list with numbers

For other list you can keep first number as index and rest use in zip(names, numbers) to create pairs (name, number) which can be used to create [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)

Result:

['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]
https://en.xdnf.cn/q/120051.html

Related Q&A

How to interact with the reCAPTCHA Solve the challenge button using Selenium and Python

Im trying to interact with the recaptcha Solve the challenge button on image verification popup using Selenium and Python. The xpath looks correct in dev tools but using Selenium unable to interact wit…

How to convert an adjacency matrix to an adjacency list with python?

I have an adjacency matrix like: [[ 0., 15., 0., 7., 10., 0.],[ 15., 0., 9., 11., 0., 9.],[ 0., 9., 0., 0., 12., 7.],[ 7., 11., 0., 0., 8., 14.],[ 10., 0., 12., …

how to plot a box plot of a column of a data frame in two groups in matplotlib

I have the following data frame:value total_spend margin1 29493.14 10203.371 27551.22 19003.072 22881.26 15142.681 15254.77 12337.221 11873.95 9424.231 11382.89 8767.83…

Text manipulation to form an equation

a=0.77 ,b=0.2 ,c=0.20, d=0.79 ,z=(c+d), e=(z*a) ,output=(z+e) I have a text file like above. I need a parser logic that will throw an equation like output=(0.20+0.79)+((0.20+0.79)*a) what are some effi…

Python If statement and logical operator issue [duplicate]

This question already has answers here:How to test multiple variables for equality against a single value?(31 answers)Closed 6 years ago.Im trying to create a small python program to emulate rolling a…

How do I replace a specific string in a 2d array?

Im making a program that Identifies if a blank tile exists or not. I already have a code in my 2d array which is arr2 = [[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0][0 0 0 0 0 0 0 0 0 0 0 0 0 0 0][0 0 0 0 0 0 0 0 0…

from _dlib_pybind11 import * ModuleNotFoundError: No module named _dlib_pybind11

I actually working on a face recognition project but getting an error such as: from _dlib_pybind11 import * ModuleNotFoundError: No module named _dlib_pybind11Please help Ill appreciate any bits of hel…

Sudoku with user input

I am trying to write a Sudoku game with user input. So the user can choose what row/column it wants to and what number. I have to import it from a text file and have made a save and load function. I ha…

Move user to a textchannel as soon as he has pressed a button

I have a problem. I would like to move a user from one specific textchannel to another specific textchannel as soon as he has pressed a button. Unfortunately I get an error. class MyView(discord.ui.Vie…

scrape the about page of websites with Python [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…