Python Forum
List comprehension, I think(?)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
List comprehension, I think(?)
#1
Hey guys. When I run this, it properly writes to a new text file any line of the "full" list that doesn't have a corresponding line in the "emit" list. Unfortunately, if the entries aren't exact, it falters. Can anyone assist me in how i would go about removing any line in the "full" list that has any of the corresponding strings in the "emit" list?


        full = [hi, turtle, hello, [email protected], bye(hhhh), etc ...] ### actual program has a lot more entrees
        emit = [hi, howdy, bye, etc ...] ###Actual program has a lot more entrees
        ### what i want = [hello,turtle etc...]
        ### what i get = [hello, turtle, [email protected], bye(hhh), etc ...]      


        
        new_list = []
        for item in full:
            if item in (emit):
                new_list.append(item)
        item_list = new_list
        f = open("test.txt", "w")
        f.write(("\n".join(map(lambda x: str(x), new_list))))
        f.close()
Reply
#2
list1 = ['alfa', 'bravo', 'charlie',  'delta', 'foxtrot']
list2 = ['alfa', 'charlie', 'echo']

# using comprehension
lists3 = [item for item in list1 if item in list2]
print(list3)

# using built-in filter()
list4 = list(filter(lambda w: w in list2, list1)) # you may skip converting to list if not neccessary
print(list4)

#if all elements in list1 are unique and order in result is not important:
set5 = set(list1) & set(list2) # you may want to convert it to list
print(set5)
Reply
#3
Thanks for your assistance, but none of your solutions are working. I am trying to emit all the strings from the 'emit list'. This is what I got instead:

FULL LIST ===[['[email protected]'], ['Cindy'], ['Blaire'], ['Chuck'], ['Sandy'], ['Bill'], ['Tuck'], ['Smith']]
EMIT LIST === [['Ann'], ['Cindy'], ['Blaire'], ['Bill'], ['Tuck'], ['Smith']]
YOUR SOLUTIONS == [['Cindy'], ['Blaire'], ['Bill'], ['Tuck'], ['Smith']]
WHAT I WANT == [Chuck] [Sandy]
BEFORE I WAS GETTING [[email protected]], [chuck] [sandy]
Reply
#4
Sorry, I misunderstood you - that you want the opposite. Adjust my example accordingly - use not in list2 instead of in list2
also, it looks like your 'previous' output was correct.
Reply
#5
This still arrives me upon the same dilemma as where I began. --- It's working w/o error, but still gives me ["[email protected]], [sandy], [chuck] when I run it --- I want it to just show Sandy and Chuck, because the name "Ann" is on a single line in the "emit" list. Am I explaining this, okay?
Reply
#6
(Nov-14-2017, 08:41 AM)zykbee Wrote: Am I explaining this, okay?
not very well, obviously

Is it correct to assume that these as lists of single-element lists? Or it is possible to have multi-element sub-lists? will 'Ann' for example will always be in the start of the string?, etc..
Reply
#7
No. Ann will not always be at the start of the string. I want it to remove 'Ann' (and the string) no matter where it is in the list. For instance, this is what i want:

list1= [hi], [bye], [ann], [[email protected]], [HHHannHHH] ---- old list
list 2=[ann] [apple] -------emitted words
list 3 = [hi], [bye] --- new list

AND YES, all single element list.
Reply
#8
list1 = [['[email protected]'], ['Cindy'], ['Blaire'], ['Chuck'], ['Sandy'], ['Bill'], ['Tuck'], ['Smith'], ['HHHannHHH']]
list2 =  [['Ann'], ['Cindy'], ['Blaire'], ['Bill'], ['Tuck'], ['Smith']]

# using comprehension
list3 = [item for item in list1 if all(item2[0].lower() not in item[0].lower() for item2 in list2)]
print(list3)
Reply
#9
As a side note, a more pythonic (readable) would be to define separate function
 
list1 = [['[email protected]'], ['Cindy'], ['Blaire'], ['Chuck'], ['Sandy'], ['Bill'], ['Tuck'], ['Smith'], ['HHHannHHH']]
list2 =  [['Ann'], ['Cindy'], ['Blaire'], ['Bill'], ['Tuck'], ['Smith']]


def validate_item(item, check_list):
    return all(check_item[0].lower() not in item[0].lower() for check_item in check_list)
# using comprehension
list3 = [item for item in list1 if validate_item(item, list2)]
print(list3)

from functools import partial
validator =  partial(validate_item, check_list=list2)
# using built-in filter()
list4 = list(filter(validator, list1)) # you may skip converting to list if not neccessary
print(list4)
Reply
#10
YOU'RE INCREDIBLE! Thank You! If you have the chance, can you give me a quick rundown of what this did? --- all(item2[0].lower() not in item[0].lower() ---
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  List Comprehension Issue johnywhy 5 440 Jan-14-2024, 07:58 AM
Last Post: Pedroski55
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 427 Oct-17-2023, 09:46 AM
Last Post: tomciodev
  Using list comprehension with 'yield' in function tester_V 5 1,175 Apr-02-2023, 06:31 PM
Last Post: tester_V
  list comprehension 3lnyn0 4 1,360 Jul-12-2022, 09:49 AM
Last Post: DeaD_EyE
  List comprehension used differently coder_sw99 3 1,679 Oct-03-2021, 04:12 PM
Last Post: coder_sw99
  How to invoke a function with return statement in list comprehension? maiya 4 2,750 Jul-17-2021, 04:30 PM
Last Post: maiya
  List comprehension and Lambda cametan 2 2,196 Jun-08-2021, 08:29 AM
Last Post: cametan
  What is the difference between a generator and a list comprehension? Pedroski55 2 2,176 Jan-02-2021, 04:24 AM
Last Post: Pedroski55
  For Loop with List Comprehension muzikman 25 6,408 Dec-18-2020, 10:45 PM
Last Post: muzikman
  Using recursion instead of for loops / list comprehension Drone4four 4 3,072 Oct-10-2020, 05:53 AM
Last Post: ndc85430

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020