gsb
gsb

Reputation: 5640

Deleting the first item of every element in a string in Python

I have a list in python. This list has sublist in it. for example:

 [['85.', 'Document', 'will', 'discuss', 'allegations,', 'or', 'measures', 'being', 'taken', 'against,', 'corrupt', 'public', 'officials', 'of', 'any', 'governmental', 'jurisdiction', 'worldwide.'], ['56.', 'Document', 'will', 'include', 'a', 'prediction', 'about', 'the', 'prime', 'lending', 'rate,', 'or', 'will', 'report', 'an', 'actual', 'prime', 'rate', 'move.'], and so on]

when i print mylist[0], i get the following:

['85.', 'Document', 'will', 'discuss', 'allegations,', 'or', 'measures', 'being', 'taken', 'against,', 'corrupt', 'public', 'officials', 'of', 'any', 'governmental', 'jurisdiction', 'worldwide.']

when i print mylist[0][0] i get 85.

I am new to python and i dont understand how to access these values(85, 56, and so on) inside a for loop, so that i can delete all the numbers. i.e 85, 56, and so on.

  1. Also I have a similar list [[1, 23], [2, 34], [3, 45], [1, 45], [2, 44]] and i want to add all the second elements where the 1st element is the same. i.e i want to add 23 + 45 (because both have 1 as their 1st element). I understand i need a for loop, but i am new to python, and i am not able to understand the loops.

Upvotes: 1

Views: 217

Answers (3)

Roman Bodnarchuk
Roman Bodnarchuk

Reputation: 29727

To get all the first elements:

zip(*your_list)[0]

zip(*some_iterable) does some kind of matrix inversion - you should play a bit with it to get an idea.

To remove all the first values from a set of iterables you may choose a few ways. E.g.:

[item[1:] for item in your_list]  # probably the best
zip(*zip(*your_list)[1:])  # tricky and probably slow one

To sum your values you'd need a dictionary:

>>> from collections import defaultdict
>>> l = [[1, 23], [2, 34], [3, 45], [1, 45], [2, 44]]
>>> d = defaultdict(int)
>>> for item in l:
    d[item[0]] += item[1]

>>> d.items()
[(1, 68), (2, 78), (3, 45)]

We use defaultdict here to be able to perform this d[item[0]] += item[1] assigment. With simple dict we'd get a KeyError, since our d is empty. But defaultdict in this case just returns default value - int(), which is 0.

Upvotes: 1

Akavall
Akavall

Reputation: 86178

For your first problem:

first_values = [int(sublist[0]) for sublist in data]

For your second problem:

x = [[1, 23], [2, 34], [3, 45], [1, 45], [2, 44]]

dicto = {}

for sublist in x:
    try:
        dicto[sublist[0]] = dicto[sublist[0]] + sublist[1]
    except KeyError:
        dicto[sublist[0]] = sublist[1]

Upvotes: 1

Joel Cornett
Joel Cornett

Reputation: 24788

Basically, python treats each part of mylist[0][0] as 2 separate commands. The first call: mylist[0] returns

    ['85.', 'Document', 'will', 'discuss', 'allegations,', 'or', 'measures', 'being', 'taken', 'against,', 'corrupt', 'public', 'officials', 'of', 'any', 'governmental', 'jurisdiction', 'worldwide.']

which is the first item in your original list. The second part gets the <first call>[0] or the 0th element of THAT list. It should return 85.

In order to access the first element of the next list, you would use mylist[1][0] (get the 2nd element, return the 1st element from that list...

To obtain a list of all the first elements in the list, use a list comprehension:

    first_items = [item[0] for item in mylist]
    print(first_items)
    ...
    [85, 56,... and so on]

To 'delete' all the first elements of list, you would do something called slicing. You can create a new list using only the second element (and so on) of each list:

    new_list = [item[1:] for item in mylist]

Upvotes: 1

Related Questions