Python Forum
Need help for list of list of tuple !
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help for list of list of tuple !
#4
(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 ?
Reply


Messages In This Thread
Need help for list of list of tuple ! - by webdef - Aug-11-2017, 05:10 AM
RE: Need help for list of list of tuple ! - by webdef - Aug-12-2017, 03:19 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Strange behavior list of list mmhmjanssen 3 506 May-09-2024, 11:32 AM
Last Post: mmhmjanssen
  using > < for tuple , list,... akbarza 3 616 Feb-05-2024, 01:18 PM
Last Post: deanhystad
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 588 Oct-17-2023, 09:46 AM
Last Post: tomciodev
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 1,398 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  Change font in a list or tuple apffal 4 2,828 Jun-16-2023, 02:55 AM
Last Post: schriftartenio
  Delete strings from a list to create a new only number list Dvdscot 8 1,781 May-01-2023, 09:06 PM
Last Post: deanhystad
  List all possibilities of a nested-list by flattened lists sparkt 1 1,028 Feb-23-2023, 02:21 PM
Last Post: sparkt
  Сheck if an element from a list is in another list that contains a namedtuple elnk 8 2,055 Oct-26-2022, 04:03 PM
Last Post: deanhystad
Question Keyword to build list from list of objects? pfdjhfuys 3 1,733 Aug-06-2022, 11:39 PM
Last Post: Pedroski55
  search a list or tuple for a specific type ot class Skaperen 8 2,117 Jul-22-2022, 10:29 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020