英文:
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"?
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论