比较2个字符串列表索引与for循环python

时间:2017-07-25 04:36:28

标签: python python-2.7 for-loop

我试图在python中比较2个字符串列表,假设我有这两个列表:

list_one = ['con good', 'con good', 'tech', 'retail', 'con good',
           'con good', 'retail', 'finance', 'finance', 'retail',
           'retail', 'finance', 'tech', 'retail', 'tech',
           'finance', 'con good', 'tech', 'con good', 'tech']

list_two =      ['yes', 'yes', 'yes', 'no', 'no',
                 'no', 'no', 'yes', 'yes', 'yes',
                 'yes', 'yes', 'yes', 'no', 'no',
                 'no', 'no', 'yes', 'yes', 'no']

如果在索引y list_one中有项x并且在list_two中列出索引y,如何正确迭代变量

例如,我如何检查是否“好”'在list_one中与'是'处于相同位置在list_two中(均在索引0中)

我尝试像这样使用for循环

tech = 0

for i in list_one:
    for j in list_two:
        if i == 'tech' and j == 'yes':
            tech = tech+1

print tech

但是当它应该返回时它返回55.请帮助我

7 个答案:

答案 0 :(得分:3)

如果您利用python提供的工具,可以将其缩减为单行,例如:

>>> from collections import Counter
>>> counts = Counter(one for one, two in zip(list_one, list_two) if two == 'yes')

>>> print(counts)
Counter({'con good': 3, 'tech': 3, 'finance': 3, 'retail': 2})

>>> print(counts['tech'])
3

Zip会将两个列表组合在一起,Counter会计算一个可迭代的项目。

答案 1 :(得分:2)

$("#container").load(Selected + ".php", function(){
  $("#box").css("background-color", "red");
});

测试在纸张上手动运行代码。每当您点击tech = 0 for i in range(len(list_one)): if list_one[i] == 'tech' and list_two[i] == 'yes': tech = tech+1 print tech 中的tech时,您的代码所执行的操作基本上就是计算整个list_oneyes的数量。

更多Pythonic的方法是:

list_two

使用列表理解:

tech = 0
for i in zip(list_one, list_two):
    if i == ('tech', 'yes'):
        tech = tech + 1

答案 2 :(得分:2)

正如所指出的那样zip是要走的路,例如:

>>> tech = sum(e == ('con good', 'yes') for e in zip(list_one, list_two))
>>> tech
3

注意:您要求'con good',但您的代码正在检查'tech',这两个代码都会返回3

答案 3 :(得分:0)

我的解释

match = sum([1 for k,v in zip(list_one, list_two) if k == 'con good' and v == 'yes'])

答案 4 :(得分:0)

假设您想计算列表中所有项目的出现次数

你也可以在这里使用枚举

# Initialize counts dict, Or you can use Counter here as well.
counts = {}
for i,item in enumerate(list_one):
    counts[item] = 0

# counts => {'con good': 0, 'tech': 0, 'finance': 0, 'retail': 0}

# Count actual occurrences
for i, item in enumerate(list_one):
    if(list_two[i] == 'yes'):
        counts[item] += 1

# counts => {'con good': 3, 'tech': 3, 'finance': 3, 'retail': 2}

答案 5 :(得分:0)

for i,j in enumerate(list_one):
    for k,l in enumerate(list_two):
        if i==k:
            if j=='con good' and l=='yes':
                print(i)

你也可以使用列表理解并使用enumerate()迭代列表的索引和值:

result = [i for i,j in enumerate(list_one) for k,l in enumerate(list_two) if i==k if j=='con good' and l=='yes']

答案 6 :(得分:0)

# get unique values from the list by converting it to set, then to list again
# this will output ['con good', 'tech', 'finance', 'retail']
list_three = list(set(list_one))

# for every word in list_three, find it in list_one
# check if it's a yes in list_two 
for i in list_three:
  tech = 0
  for j in range(len(list_one)):
    if list_one[j] == i and list_two[j] == "yes":
      tech += 1
  print i, ":", tech

此代码的输出为:

con good : 3
tech : 3
finance : 3
retail : 2