Twilight6 发表于 2022-5-17 20:57:11

2228294101 发表于 2022-5-17 20:52
sn = [['1230','1'],['1230','2'],['1230','3'],['2345','1'],['2345','2'],['2345','3']]
unit = []
u ...


你这代码不对呀,你就举例子 什么样才是你想要的结果吧

2228294101 发表于 2022-5-17 21:15:29

Twilight6 发表于 2022-5-17 20:57
你这代码不对呀,你就举例子 什么样才是你想要的结果吧

输出 = [['1230','1'],['1230','2'],['2345','1'],['2345','2'],['1111','1'],['1111','2'],['2222','1'],['2222','2'],['6547','1'],['6547','2'],['9852','1'],['9852','2']]

Twilight6 发表于 2022-5-17 21:21:28

2228294101 发表于 2022-5-17 21:15
输出 = [['1230','1'],['1230','2'],['2345','1'],['2345','2'],['1111','1'],['1111','2'],['2222','1' ...

sn = [['1230','1'],['1230','2'],['1230','3'],['1230','4'],
      ['2345','1'],['2345','2'],['2345','3'],['1111','1'],
      ['1111','2'],['1111','3'],['1111','4'],['1111','5'],
      ['2222','1'],['2222','2'],['2222','3'],['6547','1'],
      ['6547','2'],['6547','3'],['9852','1'],['9852','2']]

tn = []
for x in sn:
    if len(x) == 4 and (x == '1' or x == '2'):
      tn.append(x)
print(tn)

2228294101 发表于 2022-5-17 21:39:25

Twilight6 发表于 2022-5-17 21:21


这几个元素不是特定的值,{:10_266:},我只是用来形容一下。

Twilight6 发表于 2022-5-17 21:43:18

2228294101 发表于 2022-5-17 21:39
这几个元素不是特定的值,,我只是用来形容一下。



只能说你目的和需求描述的太模糊了,没办法

看看有没其他大佬能理解,说实话我到现在都不知道你的需求到底是啥

qq1151985918 发表于 2022-5-17 21:45:49

sn = [['1230','1'],['1230','2'],['1230','3'],['1230','4'],['2345','1'],['2345','2'],['2345','3'],['1111','1'],['1111','2'],['1111','3'],['1111','4'],['1111','5'],['2222','1'],['2222','2'],['2222','3'],['6547','1'],['6547','2'],['6547','3'],['9852','1'],['9852','2']]

data = {}
for i in sn:
    if i not in data:
      data] =
    else:
      data].append(i)

for x in data:
    s = ','.join( for y in data])
    print(f'{x}相同,提取复制{s}')

2228294101 发表于 2022-5-17 22:16:59

Twilight6 发表于 2022-5-17 21:43
只能说你目的和需求描述的太模糊了,没办法

看看有没其他大佬能理解,说实话我到现在都不知道你的 ...

在这个例子是输出是这样,就是我要输出每个前面元素一样的,['xxxx','n'],前面的xxxx,跟后面的n,都不是特指某一个或者某一串数字,然后我要列表的每个不同的‘xxxx’ 的前两个数组{:10_266:}这样能懂吗

xfmiao 发表于 2022-5-17 22:31:12


import pandas as pd
sn=[['1230','1'],['1230','2'],['1230','3'],['1230','4'],['2345','1'],['2345','2'],['2345','3'],['1111','1'],['1111','2'],
    ['1111','3'],['1111','4'],['1111','5'],['2222','1'],['2222','2'],['2222','3'],['6547','1'],['6547','2'],['6547','3'],['9852','1'],['9852','2']]
aa=pd.DataFrame(sn)
print (aa[ (aa == '2' ) | (aa == '1' ) ])

2228294101 发表于 2022-5-17 23:35:39

Twilight6 发表于 2022-5-17 21:43
只能说你目的和需求描述的太模糊了,没办法

看看有没其他大佬能理解,说实话我到现在都不知道你的 ...

>>> sn = [['1230','1'],['1230','2'],['1230','3'],['1230','4'],['2345','1'],['2345','2'],['2345','3'],['1111','1'],['1111','2'],['1111','3'],['1111','4'],['1111','5'],['2222','1'],['2222','2'],['2222','3'],['6547','1'],['6547','2'],['6547','3'],['9852','1'],['9852','2']]
>>> unit = []
>>> unit2=[]
>>> s = 0
>>> z = []
>>> for i in sn:
...   n = i
...   unit.append(i)
...   while i not in unit:
...         unit.append(i)
...         break
...
>>> for x in unit:
...   for y in sn:
...         while s >= 3:
...             if ] == z:
...               s = s+1
...             elif ] != z:
...               s = 0
...             break
...         while x == y and s < 2:
...             unit2.append(y)
...             z = ]
...             break
...         s =s+ 1
...
>>> print(unit2)
[['1230', '1'], ['1230', '2'], ['1230', '1'], ['1230', '1'], ['1230', '1'], ['2345', '1'], ['2345', '2'], ['1111', '1'], ['1111', '2'], ['2222', '1'], ['2222', '2'], ['6547', '1'], ['6547', '2'], ['9852', '1'], ['9852', '2']]


我解决了哥{:10_266:}!!!

2228294101 发表于 2022-5-17 23:36:50

>>> sn = [['1230','1'],['1230','2'],['1230','3'],['1230','4'],['2345','1'],['2345','2'],['2345','3'],['1111','1'],['1111','2'],['1111','3'],['1111','4'],['1111','5'],['2222','1'],['2222','2'],['2222','3'],['6547','1'],['6547','2'],['6547','3'],['9852','1'],['9852','2']]
>>> unit = []
>>> unit2=[]
>>> s = 0
>>> z = []
>>> for i in sn:
...   n = i
...   unit.append(i)
...   while i not in unit:
...         unit.append(i)
...         break
...
>>> for x in unit:
...   for y in sn:
...         while s >= 3:
...             if ] == z:
...               s = s+1
...             elif ] != z:
...               s = 0
...             break
...         while x == y and s < 2:
...             unit2.append(y)
...             z = ]
...             break
...         s =s+ 1
...
>>> print(unit2)
[['1230', '1'], ['1230', '2'], ['1230', '1'], ['1230', '1'], ['1230', '1'], ['2345', '1'], ['2345', '2'], ['1111', '1'], ['1111', '2'], ['2222', '1'], ['2222', '2'], ['6547', '1'], ['6547', '2'], ['9852', '1'], ['9852', '2']]

问题已解决 ,麻烦各位了!{:10_254:}{:10_254:}{:10_254:}

xfmiao 发表于 2022-5-18 11:13:18

5行代码就可以了,不用来回循环。。。。
import pandas as pd
import numpy as np
sn=[['1230','1'],['1230','2'],['1230','3'],['1230','4'],['2345','1'],['2345','2'],['2345','3'],['1111','1'],['1111','2'],['1111','3'],['1111','4'],['1111','5'],['2222','1'],['2222','2'],['2222','3'],['6547','1'],['6547','2'],['6547','3'],['9852','1'],['9852','2']]
aa=pd.DataFrame(sn)
print (np.array(aa[ (aa == '2' ) | (aa == '1' ) ]).tolist())
页: 1 [2]
查看完整版本: 列表元素取出求助