Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
167 views
in Technique[技术] by (71.8m points)

Restoring original order of python list after processing?

Note: This is a toy example that will hopefully illustrate what I am trying to achieve.

I have a list of strings that I separate into two sub-lists in order to perform different preprocessing steps on. Lets say I have the following list of strings:

mylist = ['a1','a','a2','b','b2','b3','c','c1','c2']

For simplicity, I want to add a particular sub-string to the beginning of each element depending on whether that element contains a number (in reality, I have a multiple preprocessing steps that necessitates splitting the original list):

import re
withNum = [[i,'numPresent_'+i] for i in mylist if re.compile(r'd').search(i)]
noNum = [[i,'noNum_'+i] for i in mylist if not re.compile(r'd').search(i)]

Now that I have my two sub-lists, how can I combine them in a data-frame in a manner that they reflect their original order? Clearly, if I use df.append it will simply stack one on top of the other...

df = pd.DataFrame().append(withNum).append(noNum)

Returns:

-------------------------
    0              1
-------------------------
 a1         numPresent_a1
 a2         numPresent_a2
 b2         numPresent_b2
 b3         numPresent_b3
 c1         numPresent_c1
 c2         numPresent_c2
 a          noNum_a
 b          noNum_b
 c          noNum_c
--------------------------

How can I re-order the data-frame so that it reflects the order of the original list?

Intended Outcome:

-------------------------
    0              1
-------------------------
 a1         numPresent_a1
 a          noNum_a
 a2         numPresent_a2
 b          noNum_b
 b2         numPresent_b2
 b3         numPresent_b3
 c          noNum_c
 c1         numPresent_c1
 c2         numPresent_c2
--------------------------

I cannot rely on the content of the string itself to inform its position (so sorting alphabetically is out). I can only rely on its original position in the list. I'm hoping there is someway I can create an index that I can sort by after I have merged the two sub-lists.

question from:https://stackoverflow.com/questions/65601470/restoring-original-order-of-python-list-after-processing

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could modify your list comprehension as follows:

test = [[i,'numPresent_'+i] if re.compile(r'd').search(i) else [i,'noNum_'+i] for i in mylist]
df = pd.DataFrame().append(test)

Returns

    0              1
0  a1  numPresent_a1
1   a        noNum_a
2  a2  numPresent_a2
3   b        noNum_b
4  b2  numPresent_b2
5  b3  numPresent_b3
6   c        noNum_c
7  c1  numPresent_c1
8  c2  numPresent_c2

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...