简体   繁体   English

循环遍历 Python 中的列表项

[英]looping through the items of a list in Python

A simple problem, I am trying to loop through each element of this list, the list itself is formed as a list of lists..一个简单的问题,我试图遍历这个列表的每个元素,列表本身形成为一个列表列表..

Price = [500, 300, 100, 200]
list_of_pairs = [[1, 1], [2, 2], [1, 1], [2, 2]]

p_11 = []
p_22 = []
p_33 = []

for i in list_of_pairs:
    if i[0] == 1 and i[1] == 1:
        p_11.append(Price[list_of_pairs.index(i)])

    elif i[0] == 2 and i[1] == 2:
        p_22.append(Price[list_of_pairs.index(i)])

    elif i[0] == 3 and i[1] == 3:
        p_33.append(Price[list_of_pairs.index(i)])


print(list_of_pairs)

print(p_11)
print(p_22)

The problem is when I loop through the list and try to classify each element(sublist) by it's first and second value, It looks like this:问题是当我遍历列表并尝试通过它的第一个和第二个值对每个元素(子列表)进行分类时,它看起来像这样:

#the output:
[[1, 1], [2, 2], [1, 1], [2, 2]]
[500, 500]
[300, 300]

#what I expected(wanted) for the output:
[[1, 1], [2, 2], [1, 1], [2, 2]]
[500, 100]
[300, 200]

You could use enumerate instead of list_of_pairs.index to get the index of the current element:您可以使用enumerate而不是list_of_pairs.index来获取当前元素的索引:

for j, i in enumerate(list_of_pairs):
    if i[0] == 1 and i[1] == 1:
        p_11.append(Price[j])

    elif i[0] == 2 and i[1] == 2:
        p_22.append(Price[j])

    elif i[0] == 3 and i[1] == 3:
        p_33.append(Price[j])

This is however not really a nice solution, perhaps a better way of doing the same thing could be like this:然而,这并不是一个很好的解决方案,也许做同样事情的更好方法可能是这样的:

price = [500, 300, 100, 200]
list_of_pairs = [(1, 1), (2, 2), (1, 1), (2, 2)]

# Dictionary that stores the prices for all pairs
p = {}

# Iterate over all prices and pairs at the same time
for price, pair in zip(price, list_of_pairs):
    if pair not in p:
        p[pair] = [price]
    else:
        p[pair].append(price)

print(p[(1,1)]) # [500, 100]
print(p[(2,2)]) # [300, 200]

Try using enumerate to get the index directly it is easier and faster:尝试使用enumerate直接获取索引,它更容易、更快:

Price = [500, 300, 100, 200]
list_of_pairs = [[1, 1], [2, 2], [1, 1], [2, 2]]

p_11 = []
p_22 = []
p_33 = []

for index,i in enumerate(list_of_pairs):
    if i[0] == 1 and i[1] == 1:
        p_11.append(Price[index])

    elif i[0] == 2 and i[1] == 2:
        p_22.append(Price[index])

    elif i[0] == 3 and i[1] == 3:
        p_33.append(Price[index])


print(list_of_pairs)

print(p_11)
print(p_22)

Because index returns the index of the first value in the list that matches that, since you have duplicates, you get wrong values, use enumerate to get the index:因为index返回list一个匹配的值的索引,因为你有重复,你得到错误的值,使用enumerate获取索引:

Price = [500, 300, 100, 200]
list_of_pairs = [[1, 1], [2, 2], [1, 1], [2, 2]]

p_11 = []
p_22 = []
p_33 = []

for i, (a,b) in enumerate(list_of_pairs):
    if a == 1 and b == 1:
        p_11.append(Price[i])

    elif a == 2 and b == 2:
        p_22.append(Price[i])

    elif a == 3 and b == 3:
        p_33.append(Price[i])


print(list_of_pairs)
>>> [[1, 1], [2, 2], [1, 1], [2, 2]]

print(p_11)
>>> [500, 100]

print(p_22)
>>> [300, 200]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM