Hi Python community,
I have a got an example list like
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
T T
and i eventually want to insert items in the given locations
(A shall go between 2 and 3, B shall go between 6 and 7)
Right now i just use index numbers to define the place:
A shall insert in position 2
B shall insert in position 6
However when i insert A in position 2, the index for successful insertion
of B gets wrong
(should now be 7 instead of 6)
No, it's not an option to sort the indexes and start inserting from the
back.
The most elegant option is not to store indexes, but list iterators, which attach to the list element
and would automatically move, especially if an element is inserted before.
I could not find such functionality in python lists of [ 1,2,3 ]
Does python have such functionality ?
if yes, where can i find it ?
A shall insert in position 2
B shall insert in position 6
Hi Python community,
I have a got an example list like
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
T T
and i eventually want to insert items in the given locations
(A shall go between 2 and 3, B shall go between 6 and 7)
Right now i just use index numbers to define the place:
A shall insert in position 2
B shall insert in position 6
However when i insert A in position 2, the index for successful insertion
of B gets wrong
(should now be 7 instead of 6)
No, it's not an option to sort the indexes and start inserting from the
back.
The most elegant option is not to store indexes, but list iterators, which attach to the list element
and would automatically move, especially if an element is inserted before.
I could not find such functionality in python lists of [ 1,2,3 ]
Does python have such functionality ?
if yes, where can i find it ?
Hi Python community,
I have a got an example list like
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
T T
and i eventually want to insert items in the given locations
(A shall go between 2 and 3, B shall go between 6 and 7)
Right now i just use index numbers to define the place:
A shall insert in position 2
B shall insert in position 6
However when i insert A in position 2, the index for successful insertion
of B gets wrong
(should now be 7 instead of 6)
No, it's not an option to sort the indexes and start inserting from the
back.
The most elegant option is not to store indexes, but list iterators, which attach to the list element
and would automatically move, especially if an element is inserted before.
I could not find such functionality in python lists of [ 1,2,3 ]
Does python have such functionality ?
if yes, where can i find it ?
Hi Python community,... y.sort(reverse=True) # ensure order
I have a got an example list like
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
T T
and i eventually want to insert items in the given locations
(A shall go between 2 and 3, B shall go between 6 and 7)
Right now i just use index numbers to define the place:
A shall insert in position 2
B shall insert in position 6
However when i insert A in position 2, the index for successful insertion
of B gets wrong
(should now be 7 instead of 6)
No, it's not an option to sort the indexes and start inserting from the back.
The most elegant option is not to store indexes, but list iterators, which attach to the list element
and would automatically move, especially if an element is inserted before.
I could not find such functionality in python lists of [ 1,2,3 ]
Does python have such functionality ?
if yes, where can i find it ?
def ins(x, y):
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]a = list(range(1, 11))
a
[(2, 'A'), (6, 'B'), (10, 'Z')]b = [(2, 'A'), (6, 'B'), (10, 'Z')]
b
[1, 2, 'A', 3, 4, 5, 6, 'B', 7, 8, 9, 10, 'Z']ins(a, b)
a
'*abc+def=ghi%'
# a litte bit idiotic
s = 'abcdefghi'
ss = [(0, '*'), (3, '+'), (6, '='), (9, '%')]
ls = list(s)
ins(ls, ss)
''.join(ls)
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 546 |
Nodes: | 16 (0 / 16) |
Uptime: | 162:29:05 |
Calls: | 10,385 |
Calls today: | 2 |
Files: | 14,057 |
Messages: | 6,416,502 |