How to Extract the N-Th Elements from a List of Tuples

How to extract the n-th elements from a list of tuples

n = 1 # N. . .
[x[n] for x in elements]

Best way to get the nth element of each tuple from a list of tuples in Python

At least the fastest way in Python 2.7 is

t0,t1,t2=zip(*G) for SMALLER lists and [x[0] for x in G] in general

Here is the test:

from operator import itemgetter

G = [(1, 2, 3), ('a', 'b', 'c'), ('you', 'and', 'me')]

def f1():
return tuple(x[0] for x in G)

def f2():
return tuple(map(itemgetter(0), G))

def f3():
return tuple(x for x, y, z in G)

def f4():
return tuple(list(zip(*G))[0])

def f5():
t0,*the_rest=zip(*G)
return t0

def f6():
t0,t1,t2=zip(*G)
return t0

cmpthese.cmpthese([f1,f2,f3,f4,f5,f6],c=100000)

Results:

    rate/sec     f4     f5     f1     f2     f3     f6
f4 494,220 -- -21.9% -24.1% -24.3% -26.6% -67.6%
f5 632,623 28.0% -- -2.9% -3.0% -6.0% -58.6%
f1 651,190 31.8% 2.9% -- -0.2% -3.2% -57.3%
f2 652,457 32.0% 3.1% 0.2% -- -3.0% -57.3%
f3 672,907 36.2% 6.4% 3.3% 3.1% -- -55.9%
f6 1,526,645 208.9% 141.3% 134.4% 134.0% 126.9% --

If you don't care if the result is a list, a list comprehension if faster.

Here is a more extended benchmark with variable list sizes:

from operator import itemgetter
import time
import timeit
import matplotlib.pyplot as plt

def f1():
return [x[0] for x in G]

def f1t():
return tuple([x[0] for x in G])

def f2():
return tuple([x for x in map(itemgetter(0), G)])

def f3():
return tuple([x for x, y, z in G])

def f4():
return tuple(list(zip(*G))[0])

def f6():
t0,t1,t2=zip(*G)
return t0

n=100
r=(5,35)
results={f1:[],f1t:[],f2:[],f3:[],f4:[],f6:[]}
for c in range(*r):
G=[range(3) for i in range(c)]
for f in results.keys():
t=timeit.timeit(f,number=n)
results[f].append(float(n)/t)

for f,res in sorted(results.items(),key=itemgetter(1),reverse=True):
if f.__name__ in ['f6','f1','f1t']:
plt.plot(res, label=f.__name__,linewidth=2.5)
else:
plt.plot(res, label=f.__name__,linewidth=.5)

plt.ylabel('rate/sec')
plt.xlabel('data size => {}'.format(r))
plt.legend(loc='upper right')
plt.show()

Which produces this plot for smaller data sizes (5 to 35):

smaller

And this output for larger ranges (25 to 250):

larger

You can see that f1, a list comprehension is fastest. f6 and f1t trading places as the fastest to return a tuple.

How to extract the n-th elements equal to a value from a list of tuples in python

Try like this,

In [1]: num = [('A15', 2, 'BC', 721.16), ('A21', 3, 'AB', 631.31), 
('A42', 4, 'EE', 245.43),('A15', 2, 'BC', 856.16)]
In [2]: max((i for i in num if i[1] == 2),key=lambda x:x[3])
Out[2]: ('A15', 2, 'BC', 856.16)

How to get 2nd elements from tuples in Python?

Use a list comprehension with a filter.

myList = [...]
output = [x[1] for x in myList if x[0] == 'tuple1elem1']

How to extract the n-th Elements of tuples inside a list in OCaml

There are two problems with the code:

  1. sum takes an argument lst and then it returns a function. You either want to remove lst argument or replace function with match lst with.

  2. You're matching the tail of the list as a tuple instead of a list.

Fixed code:

let rec sum = function
[] -> 0.0
| (a, h) :: tl -> h +. sum tl;;

or, if you want to use match:

let rec sum lst = match lst with
[] -> 0.0
| (a, h) :: tl -> h +. sum tl;;

how get the nth string in a list of tuples through list-comprehension?

If you actually have a list of tuples, like this:

data = {
"col": [
("affect", "the risks"),
("have", "we"),
("breached", "our systems"),
("cease", "our computer"),
("suffer", ""),
("allow", ""),
("damage", "misappropriation"),
("require", "proprietary"),
("incur", "us"),
("remediate", "us"),
("resolve", "us"),
],
}

Then extracting things the way you want is relatively easy:

item0 = [x[0] for x in data["col"]]
item1 = [x[1] for x in data["col"]]

print("item0:", item0)
print("item1:", item1)

That gets us:

item0: ['affect', 'have', 'breached', 'cease', 'suffer', 'allow', 'damage', 'require', 'incur', 'remediate', 'resolve']
item1: ['the risks', 'we', 'our systems', 'our computer', '', '', 'misappropriation', 'proprietary', 'us', 'us', 'us']

Unfortunately, you don't have a list of tuples, and it's not clear
from your question if that's just a typo or if you have simply
mis-described your data. When you write:

df = {
"col": [
"[('affect', 'the risks')]",
"[('have', 'we'), ('breached', 'our systems'), ('cease', 'our computer'), ('suffer', ''), ('allow', ''), ('damage', 'misappropriation'), ('require', 'proprietary'), ('incur', 'us'), ('remediate', 'us'), ('resolve', 'us')]",
]
}

You have a list of two strings. The first is:

>>> df['col'][0]
"[('affect', 'the risks')]"

And the second is:

>>> df['col'][1]
"[('have', 'we'), ('breached', 'our systems'), ('cease', 'our computer'), ('suffer', ''), ('allow', ''), ('damage', 'misappropriation'), ('require', 'proprietary'), ('incur', 'us'), ('remediate', 'us'), ('resolve', 'us')]"

Processing these is going to be a little tricky. Things will be much
easier if you can arrange for your data to be formatted as a list of
tuples instead.

Is there any way to access the second element of every tuple in a list of tuples?

I do not think that you need an in-built method to do that, neither is it available. You can simply scan each element of the list and get your result. Have a look at this:

L = [("cat", "meow"), ("dog", "bark"), ("wolf", "bark"), ("cow", "moo")]
for idx in range(1,len(L)):
if L[idx][1] == "bark":
if L[idx-1][1] in ["bark", "meow"]:
print L[idx-1][0]+", "+L[idx][0]

Hope it helps.



Related Topics



Leave a reply



Submit