[英]replaced list back to original list in python
I have a list
like below in python/pyspark
like below.我在
python/pyspark
有一个如下所示的list
。 I want to convert the special characters in the list to something else.我想将列表中的特殊字符转换为其他字符。
I have done like below.我做了如下。
cols = ['abc test', 'test*abc', 'eng)test', 'abc_&test']
reps = [(' ', '_&'), ('(', '*_'), (')', '_*'), ('{', '#_'), ('}', '_#'), (';', '_##'), ('.', '_$'), (',', '_$$'), ('=', '_**')]
replacedCols = []
for col in cols:
for x in reps:
col = col.replace(x[0], x[1])
replacedCols.append(col)
checkCols = replacedCols[:]
for index, col in enumerate(replacedCols):
checkCols[index] = ''
replacedCols[index]
if col in checkCols:
replacedCols[index] = col.replace('_', '__')
The new list is like below:新名单如下:
New_cols = ['abc__&test', 'test*abc', 'eng_*test', 'abc_&test']
Now I want to convert this list back to the original list:现在我想将此列表转换回原始列表:
new_cols = ['abc__&test', 'test*abc', 'eng_*test', 'abc_&test']
reps = (('_&', ' '), ('*_', '('), ('_*', ')'), ('#_', '{'), ('_#', '}'), ('_##', ';'), ('_$', '.'), ('_$$', ','), ('_**', '='))
replaced_ColsCols = []
for col in new_cols:
for x in reps:
col = col.replace(x[0], x[1])
replaced_Cols.append(col)
check_Cols = replaced_Cols[:]
for index, col in enumerate(replaced_Cols):
check_Cols[index] = ''
replaced_Cols[index]
if col in check_Cols:
replaced_Cols[index] = col.replace('__', '_')
print(replaced_Cols)
I got the result like below which is not the same as the original list我得到如下结果,与原始列表不同
old_cols = ['abc_ test', 'test*abc', 'eng)test', 'abc test']
What do I need to do to get the the desired result我需要做什么才能得到想要的结果
I see that it is the continued post from this and you are trying to reverse the idea presented as answer in there .我看到这是这篇文章的续篇,您正试图扭转在那里作为答案提出的想法。
In that post you had changed ['abc test', 'test*abc', 'eng)test', 'abc_&test']
to ['abc__&test', 'test*abc', 'eng_*test', 'abc_&test']
and you want to reverse the changes.在
['abc test', 'test*abc', 'eng)test', 'abc_&test']
文章中,您已将['abc test', 'test*abc', 'eng)test', 'abc_&test']
更改为['abc__&test', 'test*abc', 'eng_*test', 'abc_&test']
并且您想要撤销更改。
You don't have to reverse the changes as you already have the original list您不必撤消更改,因为您已经拥有原始列表
But in case you want to learn new stuffs here's the working code for you但如果你想学习新东西,这里有适合你的工作代码
new_cols = ['abc__&test', 'test*abc', 'eng_*test', 'abc_&test']
reps = (('_&', ' '), ('*_', '('), ('_*', ')'), ('#_', '{'), ('_#', '}'), ('_##', ';'), ('_$', '.'), ('_$$', ','), ('_**', '='))
for index, col in enumerate(new_cols):
if '__' in col:
new_cols[index] = col.replace('__', "_")
replaced_ColsCols = []
checkCols = new_cols[:]
for col in new_cols:
if new_cols.count(col) > 1:
checkCols.remove(col)
if col in checkCols:
for x in reps:
col = col.replace(x[0], x[1])
replaced_ColsCols.append(col)
print replaced_ColsCols
which should give you这应该给你
['abc test', 'test*abc', 'eng)test', 'abc_&test']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.