Python Forum
Need help for list of list of tuple ! - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Need help for list of list of tuple ! (/thread-4367.html)



Need help for list of list of tuple ! - webdef - Aug-11-2017

Quite newbie, I need help to search and modify some elements of a tuple (Python 2.7).
For example, how to replace "aaa" in first element into "bbb" ?

list=[('aaastring',245878L,2475L,'anotherstring', ...),
      ('bbbstring',7894578L,456897L,'yetanother',...),
      ('aaaform',2445L,325478L,'dummy',...),
      (...)
      ]



RE: Need help for list of list of tuple ! - DeaD_EyE - Aug-11-2017

# you should avoid names which are used for
# bult-in function/classes
# list is one of them

data = [('aaastring', 245878L, 2475L, 'anotherstring'),
('bbbstring', 7894578L, 456897L, 'yetanother'),
('aaaform', 2445L, 325478L, 'dummy')]

new_list = []

for row in data:
    # row is a tuple
    # a tuple is imuatble
    # you need to create a new one
    # or exchange the tuple with a list
    row = list(row)
    row[0] = 'Replacement for col0 of all rows'
    new_list.append(row)
Content of new_list
Output:
[['Replacement for col0 of all rows', 245878L, 2475L, 'anotherstring'], ['Replacement for col0 of all rows', 7894578L, 456897L, 'yetanother'], ['Replacement for col0 of all rows', 2445L, 325478L, 'dummy']]
Instead of overwriting the first column blindly, you can pass them into a function, which does the work for manipulation based on original data of the object in the first column.

By the way, use Python 3. It gives you super powers.


RE: Need help for list of list of tuple ! - webdef - Aug-12-2017

(Aug-11-2017, 06:27 AM)DeaD_EyE Wrote:
# you should avoid names which are used for
# bult-in function/classes
# list is one of them

data = [('aaastring', 245878L, 2475L, 'anotherstring'),
('bbbstring', 7894578L, 456897L, 'yetanother'),
('aaaform', 2445L, 325478L, 'dummy')]

new_list = []

for row in data:
    # row is a tuple
    # a tuple is imuatble
    # you need to create a new one
    # or exchange the tuple with a list
    row = list(row)
    row[0] = 'Replacement for col0 of all rows'
    new_list.append(row)
Content of new_list
Output:
[['Replacement for col0 of all rows', 245878L, 2475L, 'anotherstring'], ['Replacement for col0 of all rows', 7894578L, 456897L, 'yetanother'], ['Replacement for col0 of all rows', 2445L, 325478L, 'dummy']]
Instead of overwriting the first column blindly, you can pass them into a function, which does the work for manipulation based on original data of the object in the first column.

By the way, use Python 3. It gives you super powers.
Thanks, work great !
However, new-list is not the same as data ?
data is of the form [(...),(...)]
new-list is of the form [[...],[...]]
Do I miss something ?


RE: Need help for list of list of tuple ! - webdef - Aug-12-2017

(Aug-12-2017, 06:43 AM)webdef Wrote:
(Aug-11-2017, 06:27 AM)DeaD_EyE Wrote:
# you should avoid names which are used for
# bult-in function/classes
# list is one of them

data = [('aaastring', 245878L, 2475L, 'anotherstring'),
('bbbstring', 7894578L, 456897L, 'yetanother'),
('aaaform', 2445L, 325478L, 'dummy')]

new_list = []

for row in data:
    # row is a tuple
    # a tuple is imuatble
    # you need to create a new one
    # or exchange the tuple with a list
    row = list(row)
    row[0] = 'Replacement for col0 of all rows'
    new_list.append(row)
Content of new_list
Output:
[['Replacement for col0 of all rows', 245878L, 2475L, 'anotherstring'], ['Replacement for col0 of all rows', 7894578L, 456897L, 'yetanother'], ['Replacement for col0 of all rows', 2445L, 325478L, 'dummy']]
Instead of overwriting the first column blindly, you can pass them into a function, which does the work for manipulation based on original data of the object in the first column.

By the way, use Python 3. It gives you super powers.
Thanks, work great !
However, new-list is not the same as data ?
data is of the form [(...),(...)]
new-list is of the form [[...],[...]]
Do I miss something ?
Solution could be something like:
row=list(row)
mywork = row[0]
mywork = mywork.replace("aaa","bbb",1)
row[0] = mywork
row2=tuple(row)
new_list.append(row2)
Is this correct for Python ?


RE: Need help for list of list of tuple ! - ichabod801 - Aug-12-2017

That would work, but you can modify the first item in the row directly:

row[0].replace('aaa', 'bbb', 1)
You can also tuple and append at the same time:

new_list.append(tuple(row))