如何根据子值修改列表列表中的值

时间:2018-05-24 21:48:17

标签: python python-3.x list

我有一个很大的挑战试图解决这个问题。我有这份清单清单:

[['060710080013011', 9],
 ['060710080013011', 9],
 ['060710080013011', 9],
 ['060710080013011', 9],
 ['060710080013011', 9],
 ['060710080013011', 9],
 ['060710080013011', 9],
 ['060710080013011', 9],
 ['060710080013011', 9],
 ['060710080013033', 8],
 ['060710080013033', 8],
 ['060710080013033', 8],
 ['060710080013033', 8],
 ['060710080013033', 8],
 ['060710080013033', 8],
 ['060710080013033', 8],
 ['060710080013033', 8],
 ['060710080021000', 15],
 ['060710080021000', 15],
 ['060710080021000', 15],
 ['060710080021000', 15],
 ['060710080021000', 15],
 ['060710080021000', 15],
 ['060710080021000', 15],
 ['060710080021000', 15],
 ['060710080021000', 15],
 ['060710080021000', 15],
 ['060710080021000', 15],
 ['060710080021000', 15],
 ['060710080021000', 15],
 ['060710080021000', 15],
 ['060710080021000', 15]]

第一个值是ID,第二个值是此ID出现在列表列表中的次数。问题如下:

当第二个值大于7时,我需要更改每个元组中每个第二项的值,这里是所需的输出:

[['060710080013011', 7],
 ['060710080013011', 7],
 ['060710080013011', 7],
 ['060710080013011', 7],
 ['060710080013011', 7],
 ['060710080013011', 7],
 ['060710080013011', 7],
 ['060710080013011_2', 2],
 ['060710080013011_2', 2],
 ['060710080013033', 7],
 ['060710080013033', 7],
 ['060710080013033', 7],
 ['060710080013033', 7],
 ['060710080013033', 7],
 ['060710080013033', 7],
 ['060710080013033', 7],
 ['060710080013033_2', 1],
 ['060710080021000', 7],
 ['060710080021000', 7],
 ['060710080021000', 7],
 ['060710080021000', 7],
 ['060710080021000', 7],
 ['060710080021000', 7],
 ['060710080021000', 7],
 ['060710080021000_2', 7],
 ['060710080021000_2', 7],
 ['060710080021000_2', 7],
 ['060710080021000_2', 7],
 ['060710080021000_2', 7],
 ['060710080021000_2', 7],
 ['060710080021000_2', 7],
 ['060710080021000_3', 1]]

如果你看到我的欲望输出,我需要更改每个数字大于7的子列表的第二项。

如果你需要澄清问我,我的第一语言不是英语,但我可以尽我所能。

4 个答案:

答案 0 :(得分:0)

lista2 = [['100',9]]
for l in lista2:
    v = l[1] - 7
    c = 2
    while v > 0:
        l[1] = 7
        lista2.append([l[0]+"_"+str(c),min(v,7)])
        v -= 7
        c += 1

print(lista2)

应该按照您的要求工作,主要是代码中的一些语法错误和错误名称变量,如果这不起作用,请告诉我。

答案 1 :(得分:0)

一个天真的解决方案是使用 groupby 和一个简单的计数器,这是一个有效的解决方案:

userNumberList = []
counter = 0
while counter < 8:
    userNumber = input("Welcome! Please provide numbers or press q to quit. ")
    if userNumber == 'q':
        print("Entered command to quit!! closing the application")
        break
    else:
        try:
            userNumberList.append(int(userNumber))
            counter += 1
        except ValueError:
            print("Not a number. Closing application.")
            break
print(sum(userNumberList))

答案 2 :(得分:0)

# arr is the input( list of list)
output = []
n = len(arr)
i=0
while i<n:
    ID, f = arr[i]
    mul = 1
    while mul*7 < f:
        if mul!=1:
            newID = ID + '_' + str(mul)
        else:
            newID = ID
        temp = [[newID,7] for j in range(7)]
        mul += 1
        output += temp

    rem = f - ((mul-1)*7)
    newID = ID +  '_' + str(mul)
    temp = [[newID, rem] for j in range(rem)]
    output += temp

    i += f

print(output)

上面的代码按预期提供输出。

答案 3 :(得分:0)

我的解决方案如下:

import pandas as pd
df = pd.DataFrame(lst)
df.columns = ['ID', 'counter']

df.counter = df.groupby('ID').cumcount() // 7

df.loc[df.counter>0, 'ID'] += '_' + (df.counter + 1)[df.counter>0].astype(str)
df.counter = df.applymap(lambda id: (df.ID==id).sum())['ID']

全部 - 完成了。

在下文中,我将解释每一步:

要准备数据以便在pandas中处理,请加载库并将数据放入数据框中:

import pandas as pd
df = pd.DataFrame(lst)
df.columns = ['ID', 'counter']

最初,计数器设置为数据集中ID的累积计数器的模7,可用于索引大小为7的子组:

df['counter'] = df.groupby('ID').cumcount() // 7

现在您的数据集如下所示:

                 ID  counter
0   060710080013011        0
1   060710080013011        0
2   060710080013011        0
3   060710080013011        0
4   060710080013011        0
5   060710080013011        0
6   060710080013011        0
7   060710080013011        1
8   060710080013011        1
9   060710080013033        0
10  060710080013033        0
11  060710080013033        0
12  060710080013033        0
13  060710080013033        0
14  060710080013033        0
15  060710080013033        0
16  060710080013033        1
17  060710080021000        0
18  060710080021000        0
19  060710080021000        0
20  060710080021000        0
21  060710080021000        0
22  060710080021000        0
23  060710080021000        0
24  060710080021000        1
25  060710080021000        1
26  060710080021000        1
27  060710080021000        1
28  060710080021000        1
29  060710080021000        1
30  060710080021000        1
31  060710080021000        2 

现在更改ID,即仅当counter>0追加&#34;计数器+ 1&#34;作为具有现有ID的前面下划线的字符串:

df.loc[df.counter>0, 'ID'] += '_' + (df.counter + 1)[df.counter>0].astype(str)

要将计数器更改回所需的ID总和,请将lambda函数应用于每个元素,该函数返回此元素的数据集中所有出现的总和:

df.counter = df.applymap(lambda id: (df.ID==id).sum())['ID']

然后数据集如下所示:

                   ID  counter
0     060710080013011        7      
1     060710080013011        7      
2     060710080013011        7      
3     060710080013011        7      
4     060710080013011        7      
5     060710080013011        7      
6     060710080013011        7      
7   060710080013011_2        2      
8   060710080013011_2        2      
9     060710080013033        7      
10    060710080013033        7      
11    060710080013033        7      
12    060710080013033        7      
13    060710080013033        7      
14    060710080013033        7      
15    060710080013033        7      
16  060710080013033_2        1      
17    060710080021000        7      
18    060710080021000        7      
19    060710080021000        7      
20    060710080021000        7      
21    060710080021000        7      
22    060710080021000        7      
23    060710080021000        7      
24  060710080021000_2        7      
25  060710080021000_2        7      
26  060710080021000_2        7      
27  060710080021000_2        7      
28  060710080021000_2        7      
29  060710080021000_2        7      
30  060710080021000_2        7      
31  060710080021000_3        1