0

I would like to use for loop and if to find out the elements in emptylists that are also in tollroad and append the elements in tollroad into a new list Tollroads

tollroad = ['Aberdeen Tunnel', 'Lion Rock Tunnel', 'Shing Mun Tunnels','Tseung Kwan O Tunnel', 'Tsing Sha Highway','Cross Harbour Tunnel',
'Eastern Harbour Crossing','Western Harbour Crossing', 'Tate\'s Cairn Tunnel', 'Tai Lam Tunnel', 'Lantau Link']

Emptylists = ['租庇利街 Jubilee Street', '德輔道中 Des Voeux Road Central', '摩利臣街 Morrison Street', '干諾道西 Connaught Road West','西區海底隧道 Western Harbour Crossing', '西九龍公路 West Kowloon Highway', '海寶路 Hoi Po Road', '海寶路 Hoi Po Road','連翔道 Lin Cheung Road','荔寶路 Lai Po Road',]

My code is below but nothing is created:

Emptylists

tollroad = ['Aberdeen Tunnel', 'Lion Rock Tunnel', 'Shing Mun Tunnels','Tseung Kwan O Tunnel', 'Tsing Sha Highway','Cross Harbour Tunnel',
'Eastern Harbour Crossing','Western Harbour Crossing', 'Tate\'s Cairn Tunnel', 'Tai Lam Tunnel', 'Lantau Link']

Tollroads=[]
for Emptylist in Emptylists:
  if Emptylist in tollroad:
    Tollroads.append(tollroad)
Tollroads
0

3 Answers 3

1

Your check if Emptylist in tollroad is incorrect. You should check if Emptylist contains any of the tollroads. Also, your variable names are awkward, do not start variable names with a capital letter.

[road for road in Emptylists if any(tr in road for tr in tollroad)]
#['西區海底隧道 Western Harbour Crossing']
Sign up to request clarification or add additional context in comments.

Comments

0

Alternative approach would be to use data normalization and set operations:

  1. Normalize data

    import re
    def normalize_name(text):
        # remove leading non-latin characters if present
        return ''.join(re.split('([A-Z])', text, 1)[1:]).lower()
    
  2. Then set.intersection operation can be used.

    >>> tollroad = ['Aberdeen Tunnel', 'Lion Rock Tunnel', 'Shing Mun Tunnels','Tseung Kwan O Tunnel', '
    ... Tsing Sha Highway','Cross Harbour Tunnel',
    ... 'Eastern Harbour Crossing','Western Harbour Crossing', 'Tate\'s Cairn Tunnel', 'Tai Lam Tunnel',
    ...  'Lantau Link']
    ...
    ... Emptylists = ['???? Jubilee Street', '???? Des Voeux Road Central', '???? Morrison S
    ... treet', '???? Connaught Road West','?????? Western Harbour Crossing', '????? West
    ...  Kowloon Highway', '??? Hoi Po Road', '??? Hoi Po Road','??? Lin Cheung Road','??? L
    ... ai Po Road',]
    
    >>> tollroad = set(normalize_name(name) for name in tollroad)
    
    >>> Emptylists = set(normalize_name(name) for name in Emptylists)
    
    >>> tollroad.intersection(Emptylists)
    {'western harbour crossing'}
    

Comments

0

This does what you set out to do. I agree, wouldn't mix caps in variables, unless they signify extra importance.

Tollroads = []
for Emptylist in Emptylists:
    head, tail = Emptylist .split(' ', 1)
    if tail in tollroad: Tollroads .append( tail )

print( Tollroads )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.