Python:交换两个元素并保持其他特殊字符在列表中的位置? 源数据 预期结果: 问题:
我想交换两个元素并保持特殊字符在列表中的位置 交换1和12,3和b,5和a。(我想提高效率)Python:交换两个元素并保持其他特殊字符在列表中的位置? 源数据 预期结果: 问题:,python,Python,我想交换两个元素并保持特殊字符在列表中的位置 交换1和12,3和b,5和a。(我想提高效率) 在从左侧和右侧遍历列表的同时,如果这两个元素不是特殊字符,则这两个元素将交换位置?此方法会产生预期的结果: import string lst = [1, '^', 3, 5, '!', 'a', '%', 'b', '.', 12, '*'] # i = Start_Position, j = End_Position i, j = 0, len(lst)-1 # Traverse while
在从左侧和右侧遍历列表的同时,如果这两个元素不是特殊字符,则这两个元素将交换位置?此方法会产生预期的结果:
import string
lst = [1, '^', 3, 5, '!', 'a', '%', 'b', '.', 12, '*']
# i = Start_Position, j = End_Position
i, j = 0, len(lst)-1
# Traverse while Start_Position < End_Position
while i < j:
# Swap values at Start_Position and End_Position if not special characters and update indexes
if str(lst[i]) not in string.punctuation and str(lst[j]) not in string.punctuation:
lst[i], lst[j] = lst[j], lst[i]
i += 1
j -= 1
# Decrease End_Position as special character found
elif str(lst[i]) not in string.punctuation and str(lst[j]) in string.punctuation:
j -= 1
# Increase Start_Position as special character found
elif str(lst[i]) in string.punctuation and str(lst[j]) not in string.punctuation:
i += 1
# Both values are special characters , update indexes
else:
i += 1
j -= 1
print(lst)
这种方法会产生您期望的结果:
import string
lst = [1, '^', 3, 5, '!', 'a', '%', 'b', '.', 12, '*']
# i = Start_Position, j = End_Position
i, j = 0, len(lst)-1
# Traverse while Start_Position < End_Position
while i < j:
# Swap values at Start_Position and End_Position if not special characters and update indexes
if str(lst[i]) not in string.punctuation and str(lst[j]) not in string.punctuation:
lst[i], lst[j] = lst[j], lst[i]
i += 1
j -= 1
# Decrease End_Position as special character found
elif str(lst[i]) not in string.punctuation and str(lst[j]) in string.punctuation:
j -= 1
# Increase Start_Position as special character found
elif str(lst[i]) in string.punctuation and str(lst[j]) not in string.punctuation:
i += 1
# Both values are special characters , update indexes
else:
i += 1
j -= 1
print(lst)
这是我的方法。其主要思想是交换仅包含非特殊字符的子列表,然后填充输出列表,保留特殊字符的位置 输入:
lst = [1, '^', 3, 5, '!', 'a', '%', 'b', '.', 12, '*']
查找特殊字符的位置:
import string
spec_pos = [idx for idx, el in enumerate(lst) if str(el) in string.punctuation]
获取非特殊值:
to_swap = [el for idx, el in enumerate(lst) if str(el) not in string.punctuation]
使用递归定义交换列表中元素的通用函数:
def rec_swap(l):
if len(l) == 1:
return l
if len(l)==2:
l[0], l[1] = l[1], l[0]
return l
else:
return [l[-1]] + rec_swap(l[1:-1]) + [l[0]]
交换元素:
swapped = rec_swap(sublist)
创建输出列表:
out = []
_ = [out.append(swapped.pop(0)) if idx not in spec_pos else out.append(lst[idx]) for idx, el in enumerate(lst)]
这将给出预期的输出:
out
Out[60]: [12, '^', 'b', 'a', '!', 5, '%', 3, '.', 1, '*']
这是我的方法。其主要思想是交换仅包含非特殊字符的子列表,然后填充输出列表,保留特殊字符的位置 输入:
lst = [1, '^', 3, 5, '!', 'a', '%', 'b', '.', 12, '*']
查找特殊字符的位置:
import string
spec_pos = [idx for idx, el in enumerate(lst) if str(el) in string.punctuation]
获取非特殊值:
to_swap = [el for idx, el in enumerate(lst) if str(el) not in string.punctuation]
使用递归定义交换列表中元素的通用函数:
def rec_swap(l):
if len(l) == 1:
return l
if len(l)==2:
l[0], l[1] = l[1], l[0]
return l
else:
return [l[-1]] + rec_swap(l[1:-1]) + [l[0]]
交换元素:
swapped = rec_swap(sublist)
创建输出列表:
out = []
_ = [out.append(swapped.pop(0)) if idx not in spec_pos else out.append(lst[idx]) for idx, el in enumerate(lst)]
这将给出预期的输出:
out
Out[60]: [12, '^', 'b', 'a', '!', 5, '%', 3, '.', 1, '*']
您知道要交换的元素的位置吗?如果lst=[1',^',3,5',!','a','%','b','','12,11',*'],预期的输出是什么?您关于如何交换的请求不清楚,因为您在第二个示例中交换了第一个和最后一个值……字母和数字的总和应为偶数。lst=[1'^',3,5'!','a','b','11',],预期结果:[11'^','b','a','!',5','3','1']您知道要交换的元素的位置吗?如果lst=[1',^',3,5',!','a','%','b','','12,11',*'],预期的输出是什么?您关于如何交换的请求不清楚,因为您在第二个示例中交换了第一个和最后一个值……字母和数字的总和应该是偶数。如果我们假设函数
是特殊的,则lst=[1'^',3,5'!','a','b','b','11',],预期结果:[11'^',b','a','!',5','%%',3','1',那我就把它写成if!isSpecial(lst[i])和!isSpecial(lst[j]):else:如果isSpecial(lst[i]):i+=1如果isSpecial(lst[j])j-=1
非常感谢,现在我已经理解了@binbjz评论了解决方案代码,逻辑很简单,我有两个索引,一个在开始,另一个在结束,根据条件更新索引和交换。如果我们假设一个函数isSpecial
,那么我会把它写成If!isSpecial(lst[i])和!isSpecial(lst[j]):else:如果isSpecial(lst[i]):i+=1如果isSpecial(lst[j])j-=1
非常感谢,现在我已经理解了@binbjz评论了解决方案代码,逻辑很简单,我有两个索引,一个在开始,另一个在结束,根据条件更新索引和交换。