在 python 中创建元组的排名

发布于 2024-11-03 06:39:29 字数 676 浏览 0 评论 0原文

我该怎么做? 从这个示例开始,

         Id,  Id2    CCC
        [  (A123 A120 '2011-03'), 
  LL=     (A133 A123 '2011-03'),
         ( D123 D120 '2011-04'),
          (D140 D123 '2011-04'),]

我试图得到这个,为每个元组添加排名。

     [(A123, A120 ,'2011-03',1), 
  LL=     (A133, A123, '2011-03',2),
         ( D123, D120, '2011-04',3),
          (D140, D123, '2011-04',4),]


    for i in range(len(LL)):
        RowId = i+1
        LL.append(RowId)   

我得到这样的东西:

        [(A123, A120 ,'2011-03'), 
  LL=     (A133, A123, '2011-03),
         ( D123, D120, '2011-04),
          (D140, D123, '2011-04),1,2,3,4]

How do i do this?
starting with this sample

         Id,  Id2    CCC
        [  (A123 A120 '2011-03'), 
  LL=     (A133 A123 '2011-03'),
         ( D123 D120 '2011-04'),
          (D140 D123 '2011-04'),]

I.m trying to get this, Adding rank to each tuple.

     [(A123, A120 ,'2011-03',1), 
  LL=     (A133, A123, '2011-03',2),
         ( D123, D120, '2011-04',3),
          (D140, D123, '2011-04',4),]


    for i in range(len(LL)):
        RowId = i+1
        LL.append(RowId)   

I get something like this:

        [(A123, A120 ,'2011-03'), 
  LL=     (A133, A123, '2011-03),
         ( D123, D120, '2011-04),
          (D140, D123, '2011-04),1,2,3,4]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

单挑你×的.吻 2024-11-10 06:39:29
import pprint
LL= [ ('A123', 'A120', '2011-03'),
      ('A133', 'A123', '2011-03'),
      ('D123', 'D120', '2011-04'),
      ('D140', 'D123', '2011-04'),]
LL = [row+(i,) for i,row in enumerate(LL,1)]
pprint.pprint(LL)

这里有

[('A123', 'A120', '2011-03', 1),
 ('A133', 'A123', '2011-03', 2),
 ('D123', 'D120', '2011-04', 3),
 ('D140', 'D123', '2011-04', 4)]

一些解释:

我们从这样定义的 LL 开始:

In [28]: LL
Out[28]: 
[('A123', 'A120', '2011-03'),
 ('A133', 'A123', '2011-03'),
 ('D123', 'D120', '2011-04'),
 ('D140', 'D123', '2011-04')]

第一个技巧是使用 enumerate

In [30]: list(enumerate(LL))
Out[30]: 
[(0, ('A123', 'A120', '2011-03')),
 (1, ('A133', 'A123', '2011-03')),
 (2, ('D123', 'D120', '2011-04')),
 (3, ('D140', 'D123', '2011-04'))]

这与您想要的很接近,只是“排名”从 0 开始计数,并且放置在行的前面而不是末尾。我们可以使用 enumerate(LL,1) 告诉 enumerate 从 1 开始计数,并且我们可以使用 列表理解

In [31]: [row+(i,) for i,row in enumerate(LL,1)]
Out[31]: 
[('A123', 'A120', '2011-03', 1),
 ('A133', 'A123', '2011-03', 2),
 ('D123', 'D120', '2011-04', 3),
 ('D140', 'D123', '2011-04', 4)]

在列表理解中,row 是一个元组喜欢('A123', 'A120', '2011-03'),
row+(i,) 是元组之和:

In [32]: ('A123', 'A120', '2011-03')+(1,)
Out[32]: ('A123', 'A120', '2011-03', 1)

这就是列表推导式的每一行的构造方式。

import pprint
LL= [ ('A123', 'A120', '2011-03'),
      ('A133', 'A123', '2011-03'),
      ('D123', 'D120', '2011-04'),
      ('D140', 'D123', '2011-04'),]
LL = [row+(i,) for i,row in enumerate(LL,1)]
pprint.pprint(LL)

yields

[('A123', 'A120', '2011-03', 1),
 ('A133', 'A123', '2011-03', 2),
 ('D123', 'D120', '2011-04', 3),
 ('D140', 'D123', '2011-04', 4)]

Here's a bit of explanation:

We start with LL defined like this:

In [28]: LL
Out[28]: 
[('A123', 'A120', '2011-03'),
 ('A133', 'A123', '2011-03'),
 ('D123', 'D120', '2011-04'),
 ('D140', 'D123', '2011-04')]

The first trick is to use enumerate:

In [30]: list(enumerate(LL))
Out[30]: 
[(0, ('A123', 'A120', '2011-03')),
 (1, ('A133', 'A123', '2011-03')),
 (2, ('D123', 'D120', '2011-04')),
 (3, ('D140', 'D123', '2011-04'))]

which is close to what you want, except that the "rank" starts counting at 0, and is placed in front of the row instead of at the end. We can tell enumerate to start counting with 1, using enumerate(LL,1), and we can place the rank at the end of the row using a list comprehension:

In [31]: [row+(i,) for i,row in enumerate(LL,1)]
Out[31]: 
[('A123', 'A120', '2011-03', 1),
 ('A133', 'A123', '2011-03', 2),
 ('D123', 'D120', '2011-04', 3),
 ('D140', 'D123', '2011-04', 4)]

In the list comprehension, row is a tuple like ('A123', 'A120', '2011-03'),
and row+(i,) is a sum of tuples:

In [32]: ('A123', 'A120', '2011-03')+(1,)
Out[32]: ('A123', 'A120', '2011-03', 1)

This is how each row of the list comprehension is constructed.

轮廓§ 2024-11-10 06:39:29

您可以使用 enumerate 创建排名变量(从 1 开始添加 1),并创建新元组的新列表,因为元组是不可变的,这就是我们创建新元组的原因。

例如,也应该适用于您的列表:

In [1]: LL=[(1,2,3),(1,2,3)]

In [2]: [j+(i+1,) for i,j in enumerate(LL)]
Out[2]: [(1, 2, 3, 1), (1, 2, 3, 2)]

You can use enumerate to create rank variables(added 1 for starting from 1), and create a new list of new tuples, because tuples are immutable, thats why we are creating new tuples.

Example, should work on your list too:

In [1]: LL=[(1,2,3),(1,2,3)]

In [2]: [j+(i+1,) for i,j in enumerate(LL)]
Out[2]: [(1, 2, 3, 1), (1, 2, 3, 2)]
给不了的爱 2024-11-10 06:39:29
for i in range(len(LL)):
        RowId = i+1
        LL[i].append(RowId)  

请测试一下。

for i in range(len(LL)):
        RowId = i+1
        LL[i].append(RowId)  

Please test it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文