【问题标题】:relation between tuple values in a list according to their position列表中元组值之间根据其位置的关系
【发布时间】:2017-02-17 08:28:17
【问题描述】:

考虑一个包含如下元组的列表:

tuplelist = [('a','b', 'c', 'd'), (6, 3, 9, 11), (0, 4, 5, 6)]

如何匹配相应的元组位置。例如'd'

(d, 'has_val', 11)
(d, 'has_val', 6)

我尝试了以下方法:

str = 'has_val'
for i in tuplelist:
    rel = (i[3],str,i[3])

但这并没有给我想要的输出

【问题讨论】:

  • 您想对第一个和第二个元组中的所有值执行此操作吗?
  • 不,我只是想将第一个元组中位置 4 的“d”与第二个和第三个元组中的位置 4 关联起来。

标签: python list python-3.x tuples


【解决方案1】:

zip() 可以解决您的问题

tuplelist = [('a','b', 'c', 'd'), (6, 3, 9, 11), (0, 4, 5, 6)]

tuplelist_withposition=zip(tuplelist[0],tuplelist[1],tuplelist[2])

s = 'has_val'
for i in tuplelist_withposition:
     rel = (i[0],s,i[1])
     print rel
     rel=(i[0],s,i[2])
     print rel

输出:

('a', 'has_val', 6)
('a', 'has_val', 0)
('b', 'has_val', 3)
('b', 'has_val', 4)
('c', 'has_val', 9)
('c', 'has_val', 5)
('d', 'has_val', 11)
('d', 'has_val', 6)

【讨论】:

  • 1.不要添加“试试这个”作为答案,因为它们无助于解释 OP。 2. 不要使用str 作为变量名,因为它们会影响内置函数。
【解决方案2】:

首先,不要将变量命名为str,因为它会影响内置函数。

tuplelist = [('a','b', 'c', 'd'), (6, 3, 9, 11), (0, 4, 5, 6)]
s = 'has_val'

现在谈到您的问题,将第一行作为标题行很美观。另请注意,我使用index 函数来查找索引,而不是直接硬编码3。如果列数发生变化,这将很有帮助。

header_row = tuplelist[0]
column_name = 'd'
column_index = header_row.index(column_name)

现在是逻辑,使用切片从第二个元素循环。

for i in tuplelist[1:]:
    print(column_name,s,i[column_index])

这将为您提供所需的输出。

【讨论】:

    【解决方案3】:

    只需简单地使用元组和列表索引。如果您只想要容器中某个特定位置的一个值,请不要使用循环,只需使用索引即可。

    res = (tuplelist[0][3],'has_val', tuplelist[1][3])
    

    完整程序:

    tuplelist = [('a', 'b', 'c', 'd'), (6, 3, 9, 11), (0, 4, 5, 6)]
    res = (tuplelist[0][3],'has_val', tuplelist[1][3])
    
    print(res)
    

    解释:

    • (tuplelist[0][3]:使用( 开始一个元组。从tuplelist 中的第一个元组获取该元组中的最后一项并将其插入到我们当前的元组中。
    • ,'has_val',:将字符串 'has_val' 添加到我们的元组中......
    • tuplelist[1][3]):对于当前元组中的最后一个元素,获取tuplelist中第二个元组的最后一个元素,并以)结束我们的元组。

    【讨论】:

      【解决方案4】:

      听起来zip 是你的朋友:

      >>> zip(*tuplelist)
      [('a', 6, 0), ('b', 3, 4), ('c', 9, 5), ('d', 11, 6)]
      

      您可以使用zip 有点像数学中的矩阵转置。无论如何,对于您的情况,我们可以这样做:

      for x in zip(*tuplelist):
          for y in x[1:]:
              print (x[0], 'has_val', y)
      

      给予:

      ('a', 'has_val', 6)
      ('a', 'has_val', 0)
      ('b', 'has_val', 3)
      ('b', 'has_val', 4)
      ('c', 'has_val', 9)
      ('c', 'has_val', 5)
      ('d', 'has_val', 11)
      ('d', 'has_val', 6)
      

      你也可以在一个巨大的单线中做到这一点:

      >>> [(x[0], 'has_val', y) for x in zip(*tuplelist) for y in x[1:]]
      [('a', 'has_val', 6),
       ('a', 'has_val', 0),
       ('b', 'has_val', 3),
       ('b', 'has_val', 4),
       ('c', 'has_val', 9),
       ('c', 'has_val', 5),
       ('d', 'has_val', 11),
       ('d', 'has_val', 6)]
      

      【讨论】:

      • 如果我只想为 'd' 而不是为 a、b 或 c 这样做,那么我该怎么做。
      • 如果你知道'd'在第四位,你可以从zip中挑出来:zip(*tuplelist)[3]('d', 11, 6)
      • 如果你不知道'd'的位置,你可以过滤:[(x[0], 'has_val', y) for x in zip(*tuplelist) for y in x[1:] if x[0] == 'd']给你[('d', 'has_val', 11), ('d', 'has_val', 6)]
      猜你喜欢
      • 1970-01-01
      • 2021-08-18
      • 2017-06-24
      • 2013-08-29
      • 1970-01-01
      • 2022-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多