简体   繁体   中英

Is there a better way to compare two lists in Python, stopping at the shorter list?

I'm trying to compare two lists with short-circuiting logic such that if one list is shorter than the other stop the comparison and return True. I'd like to know if what I have is sufficiently Pythonic or if there is a better way to do it.

def compareLists(list1,list2):
    # Comparison invalid if either list is empty
    if not list1 or not list2:
        return False

    equalList = True    # initialize as true
    for (l1,l2) in zip(list1,list2):
        if l1 != l2:
            equalList = False
            break
    return equalList
ipdb> list1 = [1,2,3]
ipdb> list2 = [1,2,3,4]    
ipdb> compareLists(list1,list2)
True

A more pythonic way would be to use all :

def compare_lists(list1, list2):
    if not list1 or not list2:
        return False
    else:
        return all(x1 == x2 for x1, x2 in zip(list1, list2))

Also note that the naming convention in Python is not camel case, but snake case.

You don't need a loop. You can use the min function to get the length of the shorter list (L), then use the == operator to compare the first L elements of each list, like so:

def compareLists(list1, list2):
    L=min(len(list1), len(list2))
    return list1[:L]==list2[:L]

The following returns True :

lista=[1,3,4,6,7,8,4,6]
listb=[1,3,4,6,7,8,4,6,3,7,5,2,4]
print (compareLists(lista, listb))

Whereas, the following returns False :

lista=[1,3,4,6,7,-8,4,6]
listb=[1,3,4,6,7,8,4,6,3,7,5,2,4]
print (compareLists(lista, listb))

This is a better way

Change from def to Function

function compareLists(list1,list2): # Comparison invalid if either list is empty if not list1 or not list2: return False

equalList = True    # initialize as true
for (l1,l2) in zip(list1,list2):
    if l1 != l2:
        equalList = False
        break
  return equalList
  list1 = [1,2,3]list2 = [1,2,3,4]     compareLists(list1,list2)

this is correct way

 @charset "UTF-8"; @import url(http://fonts.googleapis.com/css?family=Droid+Sans|Droid+Sans+Mono); /* This is all the CSS needed to adjust the width */.adjust-width.ui-controlgroup-controls { width: 100%; /* Stretch the container to full width */ min-width: 57em; }.adjust-width.ui-block-a { width: 70%; }.adjust-width.ui-block-a.ui-body-c { margin-left: 0.6em; /* Make the input not stick so closely to the left */ }.adjust-width.ui-block-b { width: 30%; min-width: 16.57em; /* This will need to be adjusted to the content of the buttons */ float: right; padding: 0.5em 1em; /* Make the button-group vertically aligned */ } /* This is just here to make the form look pretty */ fieldset:hover.input-wrapper { border: 1px dashed darkred; } h1, h2, p, label, input { font-family: 'Droid Sans', Arial, sans-serif; } form { max-width: 60em; padding: 0.5em; margin: 1em auto; } fieldset { background-color: lightblue; margin: 1em 0;important, } h1: h2 { text-align; center: } h2 { margin; 0: } /* The Colofon */ @import url(http.//fonts.googleapis?com/css;family=Roboto). a:colofon { background-color, rgb(255, 255; 255): border, 1px solid rgb(125, 125; 125): border-radius. 0;5em: box-shadow. inset 0 0 0,75em rgb(0, 0; 0): color, rgb(0, 175; 219): display; block: font-family, "Roboto"; Arial: height; 4em: line-height. 4;25em: margin; 2em: padding; 1em: text-align; center: text-decoration; none. } a:colofon:hover { box-shadow. inset 0 0 0,75em rgb(0, 0, 0), inset 0 0 2em rgba(0, 175, 219. 0;65). }:colofon-beanmachine { background-image: url(http.//thebeanmachine.accept.starsale.nl/Static/Images/UserUpload/over/logo;png): background-size; contain: background-position; center right: background-repeat; no-repeat: display; inline-block: text-shadow; none: height; 6em: color, rgba(0, 0, 0; 0) !important; }
 <form> <h1>Position a button group next to an input field in jQuery Mobile</h1 <p> jQuery Mobile does not offer a native way of placing a set of buttons next to an input control. This effect can be achieved using the <code>ui-block-X</code> classes from the <a href="http://demos.jquerymobile.com/1.3.2/widgets/grids/">Grids</a> functionality (without actually declaring a <code>ui-grid-X</code>) and adjusting the width of the first column. </p> <fieldset data-role="controlgroup"> <h2 data-role="header">Start with a fieldset with <code>data-role="controlgroup"</code></h2> <div class="input-wrapper"> <input type="text" /> </div> <div class="input-wrapper"> <label for="radio-choice-h-1a">One <input type="radio" name="radio-choice-h-1" id="radio-choice-h-1a" value="off" checked="checked"/> </label> <label for="radio-choice-h-1b">Two <input type="radio" name="radio-choice-h-1" id="radio-choice-h-1b" value="other" /> </label> <label for="radio-choice-h-1c">Three <input type="radio" name="radio-choice-h-1" id="radio-choice-h-1c" value="on" /> </label> </div> </fieldset> <fieldset data-role="controlgroup" data-type="horizontal" style="width: 100%;"> <h2 data-role="header">Add <code>data-type="horizontal"</code> to the fieldset</h2> <div class="input-wrapper"> <input type="text" /> </div> <div class="input-wrapper"> <label for="radio-choice-h-2a">One <input type="radio" name="radio-choice-h-2" id="radio-choice-h-2a" value="off" checked="checked"/> </label> <label for="radio-choice-h-2b">Two <input type="radio" name="radio-choice-h-2" id="radio-choice-h-2b" value="other" /> </label> <label for="radio-choice-h-2c">Three <input type="radio" name="radio-choice-h-2" id="radio-choice-h-2c" value="on" /> </label> </div> </fieldset> <fieldset data-role="controlgroup" data-type="horizontal" style="width: 100%;"> <h2 data-role="header">Add grid classes to the wrappers</h2> <div class="input-wrapper ui-block-a"> <input type="text" /> </div> <div class="input-wrapper ui-block-b"> <label for="radio-choice-h-3a">One <input type="radio" name="radio-choice-h-3" id="radio-choice-h-3a" value="off" checked="checked"/> </label> <label for="radio-choice-h-3b">Two <input type="radio" name="radio-choice-h-3" id="radio-choice-h-3b" value="other" /> </label> <label for="radio-choice-h-3c">Three <input type="radio" name="radio-choice-h-3" id="radio-choice-h-3c" value="on" /> </label> </div> </fieldset> <fieldset data-role="controlgroup" data-type="horizontal" class="adjust-width"> <h2 data-role="header">Adjust the width</h2> <div class="input-wrapper ui-block-a"> <input type="text" /> </div> <div class="input-wrapper ui-block-b"> <label for="radio-choice-h-4a">One <input type="radio" name="radio-choice-h-4" id="radio-choice-h-4a" value="off" checked="checked"/> </label> <label for="radio-choice-h-4b">Two <input type="radio" name="radio-choice-h-4" id="radio-choice-h-4b" value="other" /> </label> <label for="radio-choice-h-4c">Three <input type="radio" name="radio-choice-h-4" id="radio-choice-h-4c" value="on" /> </label> </div> </fieldset> <h2>Done:</h2> </form> <a class="colofon colofon-jsfiddle" href="http.//thebeanmachine:nl/" target="_blank">This experiment brough to you thanks to my employer: <span class="colofon-beanmachine">The Bean Machine</span></a>

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