简体   繁体   中英

comparing strings and replacing words

list A, represents the text that will be replaced with the links and labels of list B

the idea is to compare the text of the list A with the tag of the list B , now ok. Here the inconvenience arises the condition only says whether or not there is the text [0] in list B , now as a replacement for that word (words, because the text [0]" The client's clock works fine. " have the word "watch, customer") so each word represents a different link

listA = ['Text with label A', 'Label B with text', 'Text without label', 'Only text']
listB = [('urlA', 'label A'), ('urlB', 'label B'), ('urlC', 'label C')]

for uri, label in listB:
    print(uri, label)
    
if any('label A' in label for uri, label in listB):
    print("contains something")
else:
    print("nothing")

conditional is same (in theory noo?), i don't know why not find something

for datail in listA:
    print(datail)
    if any(datail in label for url, label in listB):
        # condition is bad
        print("contains something")
        # how to replace that word with the tag and its url
        detalle = detalle.replace('', '')
    else:
        print("nothing")

To summarize I am trying to perform semantic annotation, suddenly some library or something more efficient

It is unclear exactly what you are trying to do, whether it is using regular expressions if the phrases may be part of a list of values or if you are trying to find a good way to iterate over your options and see if the word exists in the look up set.

For the first option check out the library re, which helps do regular expressions and do something like

re.search(my_pattern, string_to_check)

For the second case I would recommend a dictionary, since you can easily look if a value exists in the keys of a dictionary and then get the corresponding output.

my_lookup_table = {"a": 1, "b": 2, "c": 3}
test_values = ["a", "a", "d", "c"]

for value in test_values:
    if value in my_lookup_table.keys():
        print(my_lookup_table[value])
# prints 1, 1, 3

Based on the your question, it looks like you want to check if any of the items in listA is part of listB .

Let's first take listA and convert it into a tuple to look like listB .

listA = ['abc - 123', 'def - 456', 'ghi - 789', 'abc - 456']

#this will convert listA into a tuple like listB
listX = [tuple(i.split(' - ')) for i in listA]

Now that both listA and listB look the same, you can compare them against each other.

The below if statement will compare each element of listX against listB . If any of them is true, then it will print 'contains something'

if any(True for i, j in zip(listX, listB) if i == j):
    print("contains something")
else:
    print("nothing")

However, if you want to know all the items that matched between listA and listB , then you can use the below two lines

temp = [x for x in listX for b in listB if x == b]       
print (temp)

The full code is listed below:

listA = ['abc - 123', 'def - 456', 'ghi - 789', 'abc - 456']
listB = [('abc', '123'), ('def', '456'), ('ghi', '789')]

#convert listA into a tuple to compare with listB
listX = [tuple(i.split(' - ')) for i in listA]

#check if any item in listX matches with listB
if any(True for i, j in zip(listX, listB) if i == j):
    print("contains something")
else:
    print("nothing")

#for each item that matches from listA with listB, store into temp
temp = [x for x in listX for b in listB if x == b]

#temp contains all matched items betwen listA and listB
print (temp)

Output:

contains something
[('abc', '123'), ('def', '456'), ('ghi', '789')]

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