튜플 목록을 서로 가까운 여러 항목으로 분할하는 방법

1개월 전 질문 1개월 전 토론 10 views

이런 튜플 목록이 있는데요.

[(305, 312),  (313, 319),  (320, 323),  (324, 331),  (332, 336),  (337, 342),  (343, 350),  (351, 362),  (511, 520),  (928, 932),  (933, 942),  (943, 953),  (954, 957),  (958, 961),  (962, 967),  (968, 973),  (974, 979),  (980, 986),  (987, 991),  (992,  1001),  (1002, 1008),  (1009, 1011),  (1012, 1019),  (1020, 1024),  (1025, 1030),  (1031, 1033),  (1034, 1037),  (1038, 1042),  (1043, 1053),  (1054, 1060),  (1061, 1066),  (1067, 1082),  (1083, 1085),  (1086, 1090),  (1091, 1095),  (1096, 1105),  (1106, 1113),  (1114, 1116),  (1117, 1123),  (1124, 1127),  (1128, 1134),  (1135, 1143),  (1144, 1146),  (1147, 1160),  (1161, 1165),  (1166, 1170),  (1171, 1183),  (6385, 6394)]

 

어떻게 이것을 하나의 값으로 분리된 튜플의 마지막 값과 다른 값의 첫 번째 값을 각각 포함하는 튜플의 여러 목록으로 분리할 수 있나요?

이런 결과를 얻었으면 합니다.

[(305, 312),  (313, 319),  (320, 323),  (324, 331),  (332, 336),  (337, 342),  (343, 350),  (351, 362)]
[(511, 520)]
[(928, 932),  (933, 942),  (943, 953),  (954, 957),  (958, 961),  (962, 967),  (968, 973),  (974, 979),  (980, 986),  (987, 991),  (992,  1001),  (1002, 1008),  (1009, 1011),  (1012, 1019),  (1020, 1024),  (1025, 1030),  (1031, 1033),  (1034, 1037),  (1038, 1042),  (1043, 1053),  (1054, 1060),  (1061, 1066),  (1067, 1082),  (1083, 1085),  (1086, 1090),  (1091, 1095),  (1096, 1105),  (1106, 1113),  (1114, 1116),  (1117, 1123),  (1124, 1127),  (1128, 1134),  (1135, 1143),  (1144, 1146),  (1147, 1160),  (1161, 1165),  (1166, 1170),  (1171, 1183)]
[(6385, 6394)]

 

해본 건 이렇게 해봤습니다.

i = 0
length = len(position)
while i<length:
    values = []
    if position[i+1][0] - position[i][1] == 1:
        values.append(position[i])
    print(values)
    i+=1

python

2022-05-21 15:09

1개의 해답

이렇게 해보세요.

lst = [(305, 312),  (313, 319),  (320, 323),  (324, 331),  (332, 336),  (337, 342),  (343, 350),  (351, 362),  (511, 520),  (928, 932),  (933, 942),  (943, 953),  (954, 957),  (958, 961),  (962, 967),  (968, 973),  (974, 979),  (980, 986),  (987, 991),  (992,  1001),  (1002, 1008),  (1009, 1011),  (1012, 1019),  (1020, 1024),  (1025, 1030),  (1031, 1033),  (1034, 1037),  (1038, 1042),  (1043, 1053),  (1054, 1060),  (1061, 1066),  (1067, 1082),  (1083, 1085),  (1086, 1090),  (1091, 1095),  (1096, 1105),  (1106, 1113),  (1114, 1116),  (1117, 1123),  (1124, 1127),  (1128, 1134),  (1135, 1143),  (1144, 1146),  (1147, 1160),  (1161, 1165),  (1166, 1170),  (1171, 1183),  (6385, 6394)]

new_lst = []
l = []
last = lst[0][1]
for _,e in lst:
    if e-last >=150: # you can change 150 to  difference between last number.
        new_lst.append(l)
        l = []
    last = e
    l.append((_,e))

if l:
    new_lst.append(l)

for a in new_lst:
    print(a)

 

결과

 
[(305, 312), (313, 319), (320, 323), (324, 331), (332, 336), (337, 342), (343, 350), (351, 362)]
[(511, 520)]
[(928, 932), (933, 942), (943, 953), (954, 957), (958, 961), (962, 967), (968, 973), (974, 979), (980, 986), (987, 991), (992, 1001), (1002, 1008), (1009, 1011), (1012, 1019), (1020, 1024), (1025, 1030), (1031, 1033), (1034, 1037), (1038, 1042), (1043, 1053), (1054, 1060), (1061, 1066), (1067, 1082), (1083, 1085), (1086, 1090), (1091, 1095), (1096, 1105), (1106, 1113), (1114, 1116), (1117, 1123), (1124, 1127), (1128, 1134), (1135, 1143), (1144, 1146), (1147, 1160), (1161, 1165), (1166, 1170), (1171, 1183)]
[(6385, 6394)]


2022-05-21 15:10

해결방법이나 팁을 알고 계신다면


© 2022 pinfo. All rights reserved.