Home > OS >  Slicing Tuples in a List of Lists in Python
Slicing Tuples in a List of Lists in Python

Time:10-14

I have tuples in a list of lists and would like to extract only some elements in the tuple. Sample of the input data is below.

# input 
[[('ab', 0.026412873688749918), ('dc', 0.016451082731822664), ('on', 0.014278088125928066),
  ('qc', 0.009752817881775656), ('mn', 0.008332886637563352), ('nt', 0.008250535392602258),
  ('nsw', 0.006874273287824427), ('bar', 0.005878684829852004), ('tor', 0.005741627328513831),
  ('wds', 0.004119216502907735)],
 [('nb', 0.03053649661493629), ('ns', 0.01925207174326825), ('ham', 0.016207228280183325),
  ('bra', 0.013390785663058102), ('nia', 0.00878166482558038), ('knxr', 0.004648856466085521),
  ('nwm', 0.004463444159552605), ('md', 0.004377821331080258), ('ut', 0.004165890522922745),
  ('va', 0.0037484060754341083)]]

What I am trying to do is get the first items in the tuples.

# output
[['ab', 'dc', 'on', 'qc', 'mn', 'nt', 'nsw', 'bar', 'tor', 'wds'],
['nb', 'ns', 'ham', 'bra', 'nia', 'knxr', 'nwm', 'md', 'ut', 'va']]

CodePudding user response:

input = [
          [('ab', 0.026412873688749918), ('dc', 0.016451082731822664), ('on', 0.014278088125928066),
          ('qc', 0.009752817881775656), ('mn', 0.008332886637563352), ('nt', 0.008250535392602258),
          ('nsw', 0.006874273287824427), ('bar', 0.005878684829852004), ('tor', 0.005741627328513831),
          ('wds', 0.004119216502907735)],
         
         [('nb', 0.03053649661493629), ('ns', 0.01925207174326825), ('ham', 0.016207228280183325),
          ('bra', 0.013390785663058102), ('nia', 0.00878166482558038), ('knxr', 0.004648856466085521),
          ('nwm', 0.004463444159552605), ('md', 0.004377821331080258), ('ut', 0.004165890522922745),
          ('va', 0.0037484060754341083)]
        ]

As illustrated in the comments you can use list comprehensions to achieve this:

[[idx for idx, val in x] for x in input]

# Result
[['ab', 'dc', 'on', 'qc', 'mn', 'nt', 'nsw', 'bar', 'tor', 'wds'],
 ['nb', 'ns', 'ham', 'bra', 'nia', 'knxr', 'nwm', 'md', 'ut', 'va']]

A more complex way to achieve this would be to use zip() to separate the first elements from the second elements of the tuples as shown below:

[('ab', 'dc', 'on', 'qc', 'mn', 'nt', 'nsw', 'bar', 'tor', 'wds'),
(0.026412873688749918,0.016451082731822664,0.014278088125928066,0.009752817881775656,0.008332886637563352,0.008250535392602258,0.006874273287824427,0.005878684829852004,0.005741627328513831,0.004119216502907735)]

This approach can be done using:

[list(list(zip(*x))[0]) for x in input]

# Result
[['ab', 'dc', 'on', 'qc', 'mn', 'nt', 'nsw', 'bar', 'tor', 'wds'],
 ['nb', 'ns', 'ham', 'bra', 'nia', 'knxr', 'nwm', 'md', 'ut', 'va']]

CodePudding user response:

You can use loop or list comprehension to do this.

The input data is list of lists that contains tuples. Access the first element of the tuple by using tuple[0] and save it into an empty list like this:-

input_data = [
        [('ab', 0.026412873688749918), ('dc', 0.016451082731822664), ('on', 0.014278088125928066),
          ('qc', 0.009752817881775656), ('mn', 0.008332886637563352), ('nt', 0.008250535392602258),
          ('nsw', 0.006874273287824427), ('bar', 0.005878684829852004), ('tor', 0.005741627328513831),
          ('wds', 0.004119216502907735)],
         [('nb', 0.03053649661493629), ('ns', 0.01925207174326825), ('ham', 0.016207228280183325),
          ('bra', 0.013390785663058102), ('nia', 0.00878166482558038), ('knxr', 0.004648856466085521),
          ('nwm', 0.004463444159552605), ('md', 0.004377821331080258), ('ut', 0.004165890522922745),
          ('va', 0.0037484060754341083)]
    ]
    
data_list = []
for x in input_data:
    d_list = []
    for y in x:
        d_list.append(y[0])
        
    data_list.append(d_list)

# Result...
[['ab', 'dc', 'on', 'qc', 'mn', 'nt', 'nsw', 'bar', 'tor', 'wds'],
 ['nb', 'ns', 'ham', 'bra', 'nia', 'knxr', 'nwm', 'md', 'ut', 'va']]

        

Using list comprehension:- It is a shorthand way to write the for loop above by removing append() method and the initial empty lists.

data_list = [ [y[0] for y in x] for x in input_data ]
    
# Result...
[['ab', 'dc', 'on', 'qc', 'mn', 'nt', 'nsw', 'bar', 'tor', 'wds'],
['nb', 'ns', 'ham', 'bra', 'nia', 'knxr', 'nwm', 'md', 'ut', 'va']]
  • Related