简体   繁体   English

如何在不删除/更改原始元素及其值的情况下将列表/数组中元素的索引更改为另一个位置/索引

[英]How to change the index of an element in a list/array to another position/index without deleting/changing the original element and its value

For example lets say I have a list as below,例如,假设我有一个如下列表,

list = ['list4','this1','my3','is2'] or [1,6,'one','six']

So now I want to change the index of each element to match the number or make sense as I see fit (needn't be number) like so, (basically change the index of the element to wherever I want)所以现在我想更改每个元素的索引以匹配数字或在我认为合适的情况下有意义(不需要是数字),(基本上将元素的索引更改为我想要的任何位置)

list = ['this1','is2','my3','list4'] or ['one',1,'six',6]

how do I do this whether there be numbers or not?无论有没有数字,我该怎么做?

Please help, Thanks in advance.请帮助,在此先感谢。

If you don't wanna use regex and learn it's mini language use this simpler method:如果您不想使用正则表达式并学习它的迷你语言,请使用这种更简单的方法:

list1 = ['list4','this1', 'he5re', 'my3','is2']

def mySort(string):
    if any(char.isdigit() for char in string): #Check if theres a number in the string
        return [float(char) for char in string if char.isdigit()][0] #Return list of numbers, and return the first one (we are expecting only one number in the string)

list1.sort(key = mySort)

print(list1)

Inspired by this answer: https://stackoverflow.com/a/4289557/11101156受此答案的启发: https://stackoverflow.com/a/4289557/11101156

For the first one, it is easy:对于第一个,这很容易:

>>> lst = ['list4','this1','my3','is2']
>>> lst = sorted(lst, key=lambda x:int(x[-1]))
>>> lst
['this1', 'is2', 'my3', 'list4']

But this assumes each item is string, and the last character of each item is numeric.但这假设每个项目都是字符串,每个项目的最后一个字符是数字。 Also it works as long as the numeric parts in each item is single digit.只要每个项目中的数字部分是个位数,它也可以工作。 Otherwise it breaks.否则它会破裂。 For the second one, you need to define "how you see it fit", in order to sort it in a logic.对于第二个,您需要定义“您认为它如何适合”,以便按逻辑对其进行排序。

If there are multiple numeric characters:如果有多个数字字符:

>>> import re
>>> lst = ['lis22t4','th2is21','my3','is2']
>>> sorted(lst, key=lambda x:int(re.search(r'\d+$', x).group(0)))
['is2', 'my3', 'list4', 'this21']
# or,
>>> ['is2', 'my3', 'lis22t4', 'th2is21']

But you can always do:但你总是可以这样做:

>>> lst = [1,6,'one','six']
>>> lst = [lst[2], lst[0], lst[3], lst[1]]
>>> lst
['one', 1, 'six', 6]

Also, don't use python built-ins as variable names.另外,不要使用 python 内置作为变量名。 list is a bad variable name. list是一个错误的变量名。

If you just want to move element in position 'y' to position 'x' of a list, you can try this one-liner, using pop and insert:如果您只想将 position 'y' 中的元素移动到列表的 position 'x' 中,您可以使用 pop 和 insert 尝试这个单行:

lst.insert(x, lst.pop(y))

If you know the order how you want to change indexes you can write simple code:如果您知道更改索引的顺序,则可以编写简单的代码:

old_list= ['list4','this1','my3','is2']
order = [1, 3, 2, 0]
new_list = [old_list[idx] for idx in order]

If you can write your logic as a function, you can use sorted() and pass your function name as a key:如果您可以将逻辑编写为 function,则可以使用sorted()并将您的 function 名称作为键传递:

old_list= ['list4','this1','my3','is2']
def extract_number(string):
    digits = ''.join([c for c in string if c.isdigit()])
    return int(digits)
    
new_list = sorted(old_list, key = extract_number)

This case list is sorted by number, which is constructed by combining digits found in a string.此案例列表按数字排序,数字由字符串中的数字组合而成。

a = [1,2,3,4]

def rep(s, l, ab):
    id = l.index(s)
    q = s
    del(l[id])
    l.insert(ab, q)
    return l

l = rep(a[0], a, 2)
print(l)

Hope you like this Its much simpler希望你喜欢它更简单

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 随机播放数组,使得元素的预期索引将是其原始索引 - Shuffle array such that the expected index of an element will be its original index 如何在不更改 python 中的原始值的情况下将二维数组中的元素分配给新变量? - How to assign an element from a 2D array to a new variable without changing its original value in python? 如何在不使用嵌套 for 循环的情况下根据其索引值划分 2d numpy 数组中的每个元素? - How to divide each element in a 2d numpy array in relation to its index value without the use of nested for loops? 如何在不删除元素索引的情况下删除元素? - How to remove an element without deleting element's index? 如何删除列表的元素并保存已删除元素的原始索引? - How delete a element of a list and save the original index of deleted element? 给定元素的索引,更改嵌套列表内的元素的值 - Changing the value of an element that is inside of a nested list, given the index of that element 如何通过索引列表更改矩阵的元素 - how to change element of matrix by a list of index 如何在python中从位置找到列表中的元素(我使用index()找到) - How to find an element in a list from its position(which i found using index()) in python 使用列表中的索引来获取另一个列表中的另一个值/元素 - Using index from a list to get another value/element in another list python从列表中删除元素而不更改其他元素的索引 - python remove element from list without changing index of other elements
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM