2

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.

Chaotic
  • 35
  • 5
  • don't use `list` as a variable name and can you explain this statement "basically change the index of the element to wherever I want" – deadshot Jan 18 '21 at 11:37
  • i meant as in changing its position to anywhere in the list without actually deleting the elements of the list – Chaotic Jan 18 '21 at 11:47

5 Answers5

3

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

Jakub Szlaur
  • 1,852
  • 10
  • 39
2

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. list is a bad variable name.

Sayandip Dutta
  • 15,602
  • 4
  • 23
  • 52
1

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:

lst.insert(x, lst.pop(y))
Michał89
  • 87
  • 8
1

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:

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.

0
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

deceze
  • 510,633
  • 85
  • 743
  • 889