简体   繁体   中英

How to arrange a list in new order in Python

I have a list which I am getting as a response from an API:

mylist = ['Production Name', 'Receipe Name', 'Cp', 'Target Thickness', 'Average Thickness', '2 Sigma',
      'Maximum Thickness', 'Minimum Thickness', 'Out of Limits', 'Variation Coefficient', 'Web Width', 'Length',
      'Roll Number', 'Roll Stop Time', 'Roll Start Time', 'Tics', 'Cycle Time', 'Mold Time', 'Open Time', 'Close Time']

Items inside the list are not fix at a particular index and they keep on changing. There are few items which I have to arrange in a particular order like a below list:

req_list = [ 'Receipe Name', 'Production Name', 'Roll Number', 'Roll Stop Time', 'Roll Start Time', 'Target Thickness', 'Average Thickness', '2 Sigma',
'Maximum Thickness', 'Minimum Thickness', 'Out of Limits', 'Variation Coefficient', 'Cp', 'Web Width', 'Length' ]

Extra items like 'Tics', 'Cycle Time', 'Mold Time', 'Open Time', 'Close Time' then can be stored after Length

So the final list will look like:

req_list = [ 'Receipe Name', 'Production Name', 'Roll Number', 'Roll Stop Time', 'Roll Start Time', 'Target Thickness', 'Average Thickness', '2 Sigma',
          'Maximum Thickness', 'Minimum Thickness', 'Out of Limits', 'Variation Coefficient', 'Cp', 'Web Width', 'Length', 
          'Tics', 'Cycle Time', 'Mold Time', 'Open Time', 'Close Time' ]

How can I do this.?

For this program you should use random.shuffle(listname) . Then list = reqlist + randomlist . In your case, I think it would look something like this:

import random             
extra_list = ['Tics', 'Cycle Time', 'Mold Time', 'Open Time', 'Close Time']
req_list = [ 'Recipe Name', 'Production Name', 'Roll Number', 'Roll Stop Time', 
'Roll Start Time', 'Target Thickness', 'Average Thickness', '2 Sigma',
'Maximum Thickness', 'Minimum Thickness', 'Out of Limits', 'Variation Coefficient', 'Cp', 'Web Width', 'Length' ]
random.shuffle(extra_list)
list = req_list + extra_list
print(extra_list)   
print(req_list)
print(list)

In my example, I used random.shuffle to simulate mylist .

Create a set , use a list-comprehension to check whether the element in the set.

Try code below:

import random

mylist = ['Production Name', 'Receipe Name', 'Cp', 'Target Thickness', 'Average Thickness', '2 Sigma',
      'Maximum Thickness', 'Minimum Thickness', 'Out of Limits', 'Variation Coefficient', 'Web Width', 'Length',
      'Roll Number', 'Roll Stop Time', 'Roll Start Time', 'Tics', 'Cycle Time', 'Mold Time', 'Open Time', 'Close Time']

random.shuffle(mylist)

print(mylist)

req_list = ['Receipe Name', 'Production Name', 'Roll Number', 'Roll Stop Time', 'Roll Start Time', 'Target Thickness', 'Average Thickness', '2 Sigma',
'Maximum Thickness', 'Minimum Thickness', 'Out of Limits', 'Variation Coefficient', 'Cp', 'Web Width', 'Length']

tmp = set(req_list)

print(req_list + [i for i in mylist if i not in tmp])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM