未在列表中获得所需的输出

2024-05-19 02:53:32 发布

您现在位置:Python中文网/ 问答频道 /正文

_list = ['Adarsh Kumar Goel', 'Pilibhit Tiger Reserve', 'Sheikha Bodour Bint Sultan Al Qasimi', 'Saudi Central Bank', 'Vijay Kumar Sinha', 'Odisha', 'Niger', 'Qatar', '6000 cr', 'Greg Barclay', 'Tripura', 'Geneva', 'Thaawarchand Gehlot', 'V.K. Saraswat', 'SBI']

c = [('Adarsh Kumar Goel', 'Swatanter Kumar', 'Lokeshwar Singh Panta', 'K. Ramakrishnan', 'Saibal Dasgupta'),
     ('Panna Tiger Reserve', 'Rajaji Tiger reserve', 'Pilibhit Tiger Reserve', 'Manas Tiger Reserve', 'Corbett Tiger Reserve'),
     ('Haifaa Al Mansour', 'Nour El Sherbini', 'Sheikha Bodour Bint Sultan Al Qasimi', 'Sheikha Aisha bint Rashid Al Khalifa', 'Sheikha Mozah Al Maktoum'),
     ('Bank of Saudi', 'Saudi Central Bank', 'Central Bank of Saudi', 'Reserve Bank of Saudi Arab', 'Federal Bank of Saudi'),
     ('Sushil Modi', 'Vijay Kumar Sinha', 'Renu Devi', 'Tejashwi Yadav', 'Tar Kishor Prasad'),
     ('Maharashtra', 'Kerala', 'Madhya Pradesh', 'Odisha', 'Tripura'),
     ('Kenya', 'Brazil', 'Ethiopia', 'Niger', 'Rwanda'),
     ('Bahrain', 'Qatar', 'Jordan', 'UAE', 'Saudi Arabia'),
     ('2000 cr', '4000 cr', '6000 cr', '7500 cr', '10,000 cr'),
     ('Shashank Manohar', 'Greg Barclay', 'Imran Khwaja', 'Anil Kumble', 'Manu Sawhney'),
     ('Odisha', 'Madhya Pradesh', 'Tripura', 'Goa', 'Uttar Pradesh'),
     ('Zurich', 'Istanbul', 'Geneva', 'New York', 'Kabul'),
     ('Narendra Modi', 'Thaawarchand Gehlot', 'Ravi Shankar Prasad', 'Narendra Singh Tomar', 'Smriti Irani'),
     ('Jyoti Sinha', 'V.K. Saraswat', 'Vinod Kumar Yadav', 'Amitabh Kant', 'Rajiv Kumar'),
     ('Axis Bank', 'PNB', 'SBI', 'HDFC', 'RBL Bank')]

temp = []
answers = []
for x in _list:
    for z in range(len(c)):
        for y in range(len(c[z])):
            if c[z][y] == x:
                pdb.set_trace()
                temp.append(c[z][y])
                answers.append(y+1)

为什么我会在某些值中得到两次输出?我不知道哪里出错了

temp = ['Adarsh Kumar Goel', 'Pilibhit Tiger Reserve', 'Sheikha Bodour Bint Sultan Al Qasimi', 'Saudi Central Bank', 'Vijay Kumar Sinha', 'Odisha', 'Odisha', 'Niger', 'Qatar', '6000 cr', 'Greg Barclay', 'Tripura', 'Tripura', 'Geneva', 'Thaawarchand Gehlot', 'V.K. Saraswat', 'SBI']

我得到了意外的输出:

answers = [1, 3, 3, 2, 2, 4, 1, 4, 2, 3, 2, 5, 3, 3, 2, 2, 3]

此答案列表是意外的和不需要的

问题是我期望输出

[1, 3, 3, 2, 2, 4, 4, 2, 3, 2, 3, 3, 2, 2, 3]

但是,输出[1, 3, 3, 2, 2, 4, 4, 2, 3, 2, 5, 3, 2, 2, 3]也是有效的,如何修复代码,以便至少获得一个有效输出


Tags: ofcrbankcentralaltigerreservesaudi
3条回答

例如,条目Odisha在数组c内出现两次。一个简单的修复方法是将temp数组改为set。这样你就不会有重复的

temp = set()
...
temp.add(c[z][y])  # Will do nothing if a value is already present

或者,根据您希望如何使用该程序,您可能希望确保根本不处理重复的值

if c[z][y] in temp:
    continue

将您的状况从:

    if c[z][y] == x:
        pdb.set_trace()
        temp.append(c[z][y])
        answers.append(y+1)

致:

      if c[z][y] == x: 
            pdb.set_trace()
            if x not in temp:
               temp.append(c[z][y])
               answers.append(y+1)

仅当临时列表尚未出现时才添加

运行示例:

_list = ['Adarsh Kumar Goel', 'Pilibhit Tiger Reserve', 'Sheikha Bodour Bint Sultan Al Qasimi', 'Saudi Central Bank', 'Vijay Kumar Sinha', 'Odisha', 'Niger', 'Qatar', '6000 cr', 'Greg Barclay', 'Tripura', 'Geneva', 'Thaawarchand Gehlot', 'V.K. Saraswat', 'SBI']

c = [('Adarsh Kumar Goel', 'Swatanter Kumar', 'Lokeshwar Singh Panta', 'K. Ramakrishnan', 'Saibal Dasgupta'), ('Panna Tiger Reserve', 'Rajaji Tiger reserve', 'Pilibhit Tiger Reserve', 'Manas Tiger Reserve', 'Corbett Tiger Reserve'), ('Haifaa Al Mansour', 'Nour El Sherbini', 'Sheikha Bodour Bint Sultan Al Qasimi', 'Sheikha Aisha bint Rashid Al Khalifa', 'Sheikha Mozah Al Maktoum'), ('Bank of Saudi', 'Saudi Central Bank', 'Central Bank of Saudi', 'Reserve Bank of Saudi Arab', 'Federal Bank of Saudi'), ('Sushil Modi', 'Vijay Kumar Sinha', 'Renu Devi', 'Tejashwi Yadav', 'Tar Kishor Prasad'), ('Maharashtra', 'Kerala', 'Madhya Pradesh', 'Odisha', 'Tripura'), ('Kenya', 'Brazil', 'Ethiopia', 'Niger', 'Rwanda'), ('Bahrain', 'Qatar', 'Jordan', 'UAE', 'Saudi Arabia'), ('2000 cr', '4000 cr', '6000 cr', '7500 cr', '10,000 cr'), ('Shashank Manohar', 'Greg Barclay', 'Imran Khwaja', 'Anil Kumble', 'Manu Sawhney'), ('Odisha', 'Madhya Pradesh', 'Tripura', 'Goa', 'Uttar Pradesh'), ('Zurich', 'Istanbul', 'Geneva', 'New York', 'Kabul'), ('Narendra Modi', 'Thaawarchand Gehlot', 'Ravi Shankar Prasad', 'Narendra Singh Tomar', 'Smriti Irani'), ('Jyoti Sinha', 'V.K. Saraswat', 'Vinod Kumar Yadav', 'Amitabh Kant', 'Rajiv Kumar'), ('Axis Bank', 'PNB', 'SBI', 'HDFC', 'RBL Bank')]

    temp = []
    answers = []
    for x in _list:
        for z in range(len(c)):
            for y in range(len(c[z])):
                if c[z][y] == x:
                    if x not in temp:
                        temp.append(c[z][y])
                        answers.append(y+1)
    
    assert temp == _list
    assert answers == [1, 3, 3, 2, 2, 4, 4, 2, 3, 2, 5, 3, 2, 2, 3]
    print(temp)
    print(answers)

顺便说一句:在我看来,最好不要执行answers.append(y+1),而是执行answers.append(y),因为计算机中的数组是从0开始的,所以可以通过不向索引中添加+1来避免一些误解。除非,否则该索引y+1仅用于向非程序员显示数据

更好的方法:

但是,由于在c列表中有一些重复的条目,因此最好使用映射来保留给定键的多个可能答案:

temp = []
answers = {}
for x in _list:
    for z in range(len(c)):
        for y in range(len(c[z])):
            if c[z][y] == x:
                if x not in temp:
                    temp.append(c[z][y])
                    answers[x] = [y+1]
                elif x in temp:
                    answers[x].append(y + 1)

assert temp == _list 
print(_list)
print(answers)

输出

{'Adarsh Kumar Goel': [1], 'Pilibhit Tiger Reserve': [3], 'Sheikha Bodour Bint Sultan Al Qasimi': [3], 'Saudi Central Bank': [2], 'Vijay Kumar Sinha': [2], 'Odisha': [4, 1], 'Niger': [4], 'Qatar': [2], '6000 cr': [3], 'Greg Barclay': [2], 'Tripura': [5, 3], 'Geneva': [3], 'Thaawarchand Gehlot': [2], 'V.K. Saraswat': [2], 'SBI': [3]}

Why am I getting this output twice in some values

某些值在c中多次出现。例如'Odisha'位于:

>>> c[5][3]
'Odisha'
>>> c[10][0]
'Odisha'

您的内部循环for z in..,: for y in...:c中的所有项中搜索_list中的每个项,这样就可以找到这两个位置,它们满足要包含在temp中的条件(c[z][y] == x

由于您没有解释您试图做什么-您试图实现的规则,因此不清楚如何修复您的代码

一种解决方案可能是在找到匹配项后停止搜索

def g(_list,c):
    temp = []
    answers = []
    for x in _list:
        for sub in c:
            try:
                answers.append(sub.index(x) + 1)
                temp.append(x)
                break    # stop the search
            except ValueError:
                continue
    return temp,answers

相关问题 更多 >

    热门问题