在二维列表中搜索(位置)

huangapple go评论69阅读模式
英文:

Search (position) in two-dimensional List

问题

这是您要翻译的内容:

There is a list:

Aliances=[(1, 'Star Aliance'), (2, 'OneWorld'), (3, 'SkyTeam'), (4, 'Unknown'), (5, 'U-FLY Alliance'), (6, 'SkyTeam Cargo'), (7, 'WOW'), (8, 'Vanilla Alliance'), (9, 'Value Alliance'), (10, 'American Eagle'), (11, 'American Airlines'), (12, 'Sirius Aero'), (10012, 'ASL Aviation Holdings DAC')]


There is a number that is on this list:
```python
PK = 10012
Aliances = S.QueryAliances()
print("Aliances=" + str(Aliances))
myDialog.comboBox_Alliance.clear()
if Aliances:
    for Aliance in Aliances:
        myDialog.comboBox_Alliance.addItem(str(Aliance[1]))
    PKs = []
    for PK in Aliances:
        PKs.append(PK[0])
    print("PKs=" + str(PKs))
    quantity = myDialog.comboBox_Alliance.count()
    index = PKs.index(A.Aliance)
    print("index=" + str(index))
    myDialog.comboBox_Alliance.setCurrentIndex(index)

请告诉我是否有人知道如何直接从列表“Aliances”中获取“index”(PK = 10012的位置)?

英文:

There is a list:

Aliances=[(1, 'Star Aliance'), (2, 'OneWorld'), (3, 'SkyTeam'), (4, 'Unknown'), (5, 'U-FLY Alliance'), (6, 'SkyTeam Cargo'), (7, 'WOW'), (8, 'Vanilla Alliance'), (9, 'Value Alliance'), (10, 'American Eagle'), (11, 'American Airlines'), (12, 'Sirius Aero'), (10012, 'ASL Aviation Holdings DAC')]

There is a number that is on this list:
PK = 10012

Aliances = S.QueryAliances()
print("Aliances=" + str(Aliances))
myDialog.comboBox_Alliance.clear()
if Aliances:
    for Aliance in Aliances:
        myDialog.comboBox_Alliance.addItem(str(Aliance[1]))
    PKs = []
    for PK in Aliances:
        PKs.append(PK[0])
    print("PKs=" + str(PKs))
    quantity = myDialog.comboBox_Alliance.count()
    index = PKs.index(A.Aliance)
    print("index=" + str(index))
    myDialog.comboBox_Alliance.setCurrentIndex(index)

Tell me please if anyone knows how to get "index" (position of PK=10012) directly from list "Aliances"?

Link to Source Code on my GitHub

答案1

得分: 0

这实际上不是一个二维列表,而是一个元组列表。然而,以下代码应该可以工作:

Aliances = [(1, 'Star Aliance'), (2, 'OneWorld'), (3, 'SkyTeam'), (4, 'Unknown'), (5, 'U-FLY Alliance'), (6, 'SkyTeam Cargo'), (7, 'WOW'), (8, 'Vanilla Alliance'), (9, 'Value Alliance'), (10, 'American Eagle'), (11, 'American Airlines'), (12, 'Sirius Aero'), (10012, 'ASL Aviation Holdings DAC')]

index = next((i for i, x in enumerate(Aliances) if x[0] == 10012), None)

print(index)

输出:

12

如果没有找到匹配项,它也会返回 None。改编自此解决方案

英文:

This is actually not a two-dimensional list, but a list of tuples. However, the following should work:

Aliances=[(1, 'Star Aliance'), (2, 'OneWorld'), (3, 'SkyTeam'), (4, 'Unknown'), (5, 'U-FLY Alliance'), (6, 'SkyTeam Cargo'), (7, 'WOW'), (8, 'Vanilla Alliance'), (9, 'Value Alliance'), (10, 'American Eagle'), (11, 'American Airlines'), (12, 'Sirius Aero'), (10012, 'ASL Aviation Holdings DAC')]

index = next((i for i, x in enumerate(Aliances) if x[0] == 10012), None)

print(index)

Output:

12

This also returns None if no match is found. Adapted from this solution.

huangapple
  • 本文由 发表于 2023年2月19日 05:26:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75496493.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定