English 中文(简体)
我如何在2个不同的假日名单上对座标加以区分。
原标题:How do I differentiate the strings in 2 different python lists
  • 时间:2023-07-17 02:21:25
  •  标签:
  • python

Is there a way to compare 2 different lists. Specifically, I want to compare if the first 2 alphabets in the first list exist as the first 2 alphabets in the second list. Example list1=[ dog , cat , chicken ] list2=[ drew , car , table ] So I want to know if the first 2 alphabets in each of the list1 is also the first 2 alphabets in each of list 2.

问题回答

它非常直截了当。 将这两个清单合在一起,从每个清单中获取相应词语,然后对其进行比较,以确保所有语文都符合你们的标准。

>>> list1=[ dog , cat , chicken ]
>>> list2=[ drew , car , table ]
>>> all(x[:2] == y[:2] for x, y in zip(list1, list2))
False

我们可以同时利用zip()的功能,同时在两个名单上公布这些数值,同时在一旁边使用插座标语,以比较每个乳头两种特性:

list_a = [ dog ,  cat ,  chicken ]
list_b = [ drew ,  car ,  table ]

all_match = True
for word_a, word_b in zip(list_a, list_b):
    if word_a[:2] != word_b[:2]:
        print(f The following words do not match in their first two characters: {word_a} - {word_b} )
        all_match = False
        break
    
if all_match:
    print( All word pairs match in their first two characters. )

Edit: Seeing that you want to get the results as a list of boolean values, an easy modification to the above is:

list_a = [ dog ,  cat ,  chicken ]
list_b = [ drew ,  car ,  table ]
results = []

for word_a, word_b in zip(list_a, list_b):
    results.append(word_a[:2] == word_b[:2])

采用list comprehension:

list_a = [ dog ,  cat ,  chicken ]
list_b = [ drew ,  car ,  table ]

results = [a[:2] == b[:2] for (a, b) in zip(list_a, list_b)]




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签