append tuples to a list

前端 未结 4 1874
鱼传尺愫
鱼传尺愫 2021-02-13 00:46

How can I append the content of each of the following tuples (ie, elements within the list) to another list which already has \'something\' in it? So, I want to append the follo

4条回答
  •  无人共我
    2021-02-13 01:09

    You will need to unpack the tuple to append its individual elements. Like this:

    l = [('AAAA', 1.11), ('BBB', 2.22), ('CCCC', 3.33)]
    
    for each_tuple in l:
      result = ['something']
      for each_item in each_tuple:
        result.append(each_item)
        print result
    

    You will get this:

    ['something', 'AAAA', 1.1100000000000001]
    ['something', 'BBB', 2.2200000000000002]
    ['something', 'CCCC', 3.3300000000000001]
    

    You will need to do some processing on the numerical values so that they display correctly, but that would be another question.

提交回复
热议问题