【问题标题】:Accessing/using elements from lists of tuples python 3.x访问/使用元组列表中的元素 python 3.x
【发布时间】:2017-02-26 23:26:12
【问题描述】:

我是 python 新手,我正在尝试弄清楚如何访问和使用存储在元组对 (a, b) 列表中的整数,这样我就可以将 a 除以 b,如果它符合条件,我将元组附加到一个新列表并计算元组。我想只使用基本函数和 for 循环来做到这一点。

我从另一个 stackoverflow 问题中借用了一些代码,该问题用于从两个不同大小的单独整数列表中创建元组列表:

list_a = list(range(10, 51))

list_b = list(range(1, 21))

new_tuple_list = []
new_tuple_count = 0

for i, a in enumerate(list_a):
    new_tuple_list.append((a, list_b[i % len(list_b)]))
    divisors_count += 1

print ("New tuple count: ", new_tuple_count)
print (new_tuple_list)

这给了我: 新元组数:41

[(10, 1), (11, 2), (12, 3), (13, 4), (14, 5), (15, 6), (16, 7), (17, 8), (18, 9), (19, 10), (20, 11), (21, 12), (22, 13), (23, 14), (24, 15), (25, 16), (26, 17), (27, 18), (28, 19), (29, 20), (30, 1), (31, 2), (32, 3), (33, 4), (34, 5), (35, 6), (36, 7), (37, 8), (38, 9), (39, 10), (40, 11), (41, 12), (42, 13), (43, 14), (44, 15), (45, 16), (46, 17), (47, 18), (48, 19), (49, 20), (50, 1)]

但我想知道,如果我将 (10, 1) 中的 10 除以 1、11 除以 2 等,我会得到一个整数,如果是这样,我想将它添加到一个新列表中并计算元组对的数量这是正确的。 我试过这个:

tuple_test = [(10,1), (11,2)]
def find_divisors (x):

    NUM_tuples = []
    tuples_count = 0

    for x[0] in pairs:
        for x[1] in pairs:
            if x[0] / x[1] % 2 == 0:
                NUM_tuples.append(pairs)
                tuples_count += 1
                return (x[0] / x[1] % 2)
    return NUM_tuples
    return tuples_count

 find_divisors(tuple_test)

我也试过这样的:

def divisors(list_a, list_b):

    test_int = 0
    new_divisors = []

    for a in list_a:
        for b in list_b:
            if a/b % 2 == 0:
                test_int += 1
                new_divisors += (a,b)

    return new_divisors
    return test_int

    NUM_tuples = []
    tuples_count = 0


    for i, c in enumerate(list_a):
        NUM_tuples.append((c, list_b[i % len(list_b)]))
        tuples_count += 1
    return tuples_count
    return NUM_tuples

divisors(list_a, list_b)

任何帮助将不胜感激!

【问题讨论】:

  • 使用list(zip(list_a, list_b)) 组合列表。这样就更基本了。
  • 请仔细检查您的缩进。例如,正如所写的那样,您似乎已经在连续的行上编写了两个 return 语句,然后无论如何都继续执行该函数。如果这实际上是您编写的代码,那么您需要查看教程。
  • @MadPhysicist zip 在他的情况下不起作用,因为 zip 将在 (29,20) 停止,因为他的 list_b 只有 20 个值。但是如果你检查他的结果,它会在(30,1)之后继续。
  • 我需要评估 list_a 和 list_b 的每个组合,即使列表有两种不同的长度。

标签: python list python-3.x for-loop tuples


【解决方案1】:

尝试以下方法:

a = range(1,20)
b = range(1,5)
c = [(x,y) for x in a for y in b if x%y==0]
print(c)

它给出以下输出

[(1, 1), (2, 1), (2, 2), (3, 1), (3, 3), (4, 1), (4, 2), (4, 4), (5, 1), (6, 1), (6, 2), (6, 3), (7, 1), (8, 1), (8, 2), (8, 4), (9, 1), (9, 3), (10, 1), (10, 2), (11, 1), (12, 1), (12, 2), (12, 3), (12, 4), (13, 1), (14, 1), (14, 2), (15, 1), (15, 3), (16, 1), (16, 2), (16, 4), (17, 1), (18, 1), (18, 2), (18, 3), (19, 1)]

我认为您可以轻松地将其调整为您的程序!

PS:上面c的长度会给你元组的数量。

【讨论】:

  • 这太棒了,简直优雅!谢谢!
【解决方案2】:

我还想补充一点,您可以使用itertools.cycle 减少代码以获取元组的原始列表,如下所示。

from itertools import cycle

# a and b don't have to be a list unless you need them as a list elsewhere.
a = range(10, 51)
b = range(1, 21)
# cycle the shorter one
tuple_lst = list(zip(a, cycle(b)))

print("New tuple count:", len(tuple_lst))
print(tuple_lst)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-21
    • 2018-09-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多