简体   繁体   中英

Search tuple elements within in list

I have a list in Python as

list_data = [('a','b',5),('aa','bb',50)]

and some variables:

a = ('a','b','2')
c = ('aaa','bbb','500')

Now how can I search if a is already there in list_data ?
If yes add 2 to the value of a , if not append to list_data ?

The result should be as

list_data = [('a','b',7),('aa','bb',50),('aaa','bbb','500')]

Actually, this question is a good way to several demonstrate Pythonic ways of doing things. So lets see what we can do.

In order to check if something is in python list you can just use operator in :

if a in list_data:
    do_stuff()

But what you ask is a bit different. You want to do something like a search by multiple keys, if I understand correctly. In this case you can 'trim' your tuple by discarding last entry.

Slicing is handy for this:

value_trimmed = value[:-1]

Now you can make a list of trimmed tuples:

list_trimmed = []

for a in list_data:
    list_trimmed.append(a[:-1])

And then search there:

if a[:-1] in list_trimmed:
    do_smth()

This list can be constructed in a less verbose way using list_comprehension :

list_trimmed = [item[:-1] for item in list_data]

To find where your item exactly is you can use index() method of list:

list_trimmed.index(a[:-1])

This will return index of a[:-1] first occurrence in list_trimmed or throw if it cant be found. We can avoid explicitly checking if item is in the list, and do the insertion only if the exception is caught.

Your full code will look like this:

list_data = [('a','b',5), ('aa','bb',50)]
values_to_find = [('a','b','2'), ('aaa','bbb','500')]

list_trimmed = [item[:-1] for item in list_data]

for val in values_to_find:
    val_trimmed = val[:-1]
    try:
        ind = list_trimmed.index(val_trimmed)
        src_tuple = list_data[ind]   
        # we can't edit tuple inplace, since they are immutable in python
        list_data[ind] = (src_tuple[0], src_tuple[1], src_tuple[2]+2)
    except ValueError:
        list_data.append(val)

print list_data

Of course, if speed or memory-efficiency is your main concern this code is not very appropriate, but you haven't mentioned these in your question, and that is not what python really about in my opinion.

Edit:
You haven't specified what happens when you check for ('aaa','bbb','500') second time - should we use the updated list and increment matching tuple's last element, or should we stick to the original list and insert another copy?

If we use updated list, it is not clear how to handle incrementing string '500' by 2 (we can convert it to integer, but you should have constructed your query appropriately in the first place).

Or maybe you meant add last element of tuple being searched to the tuple in list if found ? Please edit your question to make it clear.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM