Python Forum
help to increment a third list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
help to increment a third list
#1
Hi every one!

I'm really a beginner, so I hope this can be easy for you! I have a list named 'array' :
['B', '494', 'C', '495', '504', 'R', '505', '519', 'S', '519', 'B', '717', 'C', '718', '732', 'L', '733', '742', 'S', '742', 'B', '1008', 'C', '1009', 'R', '1018', '1033', 'S', '1033', 'B', '1236', 'C', '1237', '1242', 'R', '1243', '1261', 'S', '1261', 'B', '1502', 'C', '1503', '1511', 'R', '1512', '1526', 'C', '1527', '1527', 'S', '1527', 'B', '1720', 'C', '1721', '1728', 'R', '1729', '1745', 'S', '1745', 'B', '1941', 'C', '1942', '1951', 'R', '1952', '1966', 'S', '1966', 'B', '2172', 'C', '2173', '2178', 'R', '2179', '2197', 'S', '2197', 'B', '2400', 'R', '2401', '2419', 'C', '2420', 'L', '2421', '2425', 'S', '2425',
'B', '2618', 'R', '2619', '2625', 'C', '2626', '2635', 'R', '2636', '2643', 'S', '2643', 'B', '2846', 'C', '2847', '2854', 'R', '2855', '2864', 'C', '2865', '2865', 'L', '2866', '2871', 'S', '2871', 'B', '3074', 'C', '3075', '3080', 'R', '3081', '3092', 'C', '3093', '3093', 'L', '3094', '3099', 'S', '3099', 'B', '3304', 'C', '3305', '3312', 'R', '3313', '3329', 'S', '3329', 'B', '3530', 'C', '3531', '3537', 'R', '3538', '3548', 'C', '3549', '3555', 'S', '3555', 'B', '3750', 'C', '3751', '3757', 'R', '3758', '3767', 'C', '3768', '3768', 'L', '3769', '3775', 'S', '3775', 'B', '3984', 'C', '3985', '4008', 'R', '4009', '4009', 'S', '4009', 'B', '4234', 'C', '4235', '4241', 'R', '4242', '4252', 'C', '4253', '4253', 'L', '4257', '4259', 'S', '4259', 'B', '4472', 'C', '4473', '4482', 'L', '4483', '4497', 'S', '4497', 'B', '4720', 'C', '4721', '4730', 'L', '4731', '4745', 'S', '4745', 'B', '4962', 'C', '4963', '4986', 'R', '4987', '4987', 'S', '4987', 'B', '5202', 'C', '5203', '5211', 'R', '5212', '5227', 'S', '5227', 'B', '5434', 'C', '5435', '5442', 'S', '5459', 'B', '5658', 'C', '5659', '5666', 'R', '5667', '5683', 'S', '5683', 'B', '5890', 'C', '5891', '5903', 'C', '5904', '5905', 'S', '5905', 'E']

I need this list to become 3 different lists : 'cote' with the letters, 'time' with the first number after the letters, and 'away' with the 'time' numbers but with a lag (away[i] = time[i+1]. This I achieved. Except, when there is a second number in 'array' before the letter, i need this number to get in 'away'.

Here is what I have tried. # are trials that didn't work either. This code result in away[i] being time[i+1], without consideration for the second numbers in 'array'

a = open("essai.txt",'r')
content=a.read()
array=content.split()
b = len(array)
cote,time, away = [],[],[]
for i in range (b):
    if array[i]=='B' or array[i]=='C' or array[i]=='L' or array[i]=='R' or array[i]=='S' or array[i]=='E':
        cote.append(array[i])
    elif array[i-1]=='B' or array[i-1]=='C' or array[i-1]=='L' or array[i-1]=='R' or array[i-1]=='S' or array[i-1]=='E':
        time.append(array[i])
   # else :
       # away.append(array[i])
for i in range (1,len(time)-1):
    away.append(time[i])
#for i in range (b):
#    d=-1
#    if array[i]=='B' :
#        d+=1
#    elif array[i]!='B'and array[i]!='C'and array[i]!='L'and array[i]!='R'and array[i]!='S'and array[i]!='E' and array[i-1]!='B'and array[i-1]!='C'and array[i-1]!='L'and array[i-1]!='R'and array[i-1]!='S'and array[i-1]!='E':
#        away[d]=array[i]
for i in range (b):
    d=-1
    if array[i]=='B' or array[i]=='C' or array[i]=='L' or array[i]=='R' or array[i]=='S' or array[i]=='E':
        d+=1    
    elif array[i-1]=='B' or array[i-1]=='C' or array[i-1]=='L' or array[i-1]=='R' or array[i-1]=='S' or array[i-1]=='E':
        break
    else :
       away[d]=array[i]
print (cote,time,away)
sorry, here is my output
Output:
['B', 'C', 'R', 'S', 'B', 'C', 'L', 'S', 'B', 'C', 'R', 'S', 'B', 'C', 'R', 'S', 'B', 'C', 'R', 'C', 'S', 'B', 'C', 'R', 'S', 'B', 'C', 'R', 'S', 'B', 'C', 'R', 'S', 'B', 'R', 'C', 'L', 'S', 'B', 'R', 'C', 'R', 'S', 'B', 'C', 'R', 'C', 'L', 'S', 'B', 'C', 'R', 'C', 'L', 'S', 'B', 'C', 'R', 'S', 'B', 'C', 'R', 'C', 'S', 'B', 'C', 'R', 'C', 'L', 'S', 'B', 'C', 'R', 'S', 'B', 'C', 'R', 'C', 'L', 'S', 'B', 'C', 'L', 'S', 'B', 'C', 'L', 'S', 'B', 'C', 'R', 'S', 'B', 'C', 'R', 'S', 'B', 'C', 'S', 'B', 'C', 'R', 'S', 'B', 'C', 'C', 'S', 'E'] ['494', '495', '505', '519', '717', '718', '733', '742', '1008', '1009', '1018', '1033', '1236', '1237', '1243', '1261', '1502', '1503', '1512', '1527', '1527', '1720', '1721', '1729', '1745', '1941', '1942', '1952', '1966', '2172', '2173', '2179', '2197', '2400', '2401', '2420', '2421', '2425', '2618', '2619', '2626', '2636', '2643', '2846', '2847', '2855', '2865', '2866', '2871', '3074', '3075', '3081', '3093', '3094', '3099', '3304', '3305', '3313', '3329', '3530', '3531', '3538', '3549', '3555', '3750', '3751', '3758', '3768', '3769', '3775', '3984', '3985', '4009', '4009', '4234', '4235', '4242', '4253', '4257', '4259', '4472', '4473', '4483', '4497', '4720', '4721', '4731', '4745', '4962', '4963', '4987', '4987', '5202', '5203', '5212', '5227', '5434', '5435', '5459', '5658', '5659', '5667', '5683', '5890', '5891', '5904', '5905'] ['495', '505', '519', '717', '718', '733', '742', '1008', '1009', '1018', '1033', '1236', '1237', '1243', '1261', '1502', '1503', '1512', '1527', '1527', '1720', '1721', '1729', '1745', '1941', '1942', '1952', '1966', '2172', '2173', '2179', '2197', '2400', '2401', '2420', '2421', '2425', '2618', '2619', '2626', '2636', '2643', '2846', '2847', '2855', '2865', '2866', '2871', '3074', '3075', '3081', '3093', '3094', '3099', '3304', '3305', '3313', '3329', '3530', '3531', '3538', '3549', '3555', '3750', '3751', '3758', '3768', '3769', '3775', '3984', '3985', '4009', '4009', '4234', '4235', '4242', '4253', '4257', '4259', '4472', '4473', '4483', '4497', '4720', '4721', '4731', '4745', '4962', '4963', '4987', '4987', '5202', '5203', '5212', '5227', '5434', '5435', '5459', '5658', '5659', '5667', '5683', '5890', '5891', '5904']
Reply
#2
Where does this problem come from? Is this homework?
Reply
#3
(Nov-29-2022, 11:01 AM)Gribouillis Wrote: Where does this problem come from? Is this homework?

No, thoose are DATA from an experiment I have to analyze
Reply
#4
Why are there sometimes 2 numbers after 1 letter and sometimes only one number?

Why are there no numbers after the last letter of the list?

What do these letters and numbers represent?
Reply
#5
Sorry, I thought thoose informations would have no interest.
The letters are side someone looked at. B is the beginning of the trial, C, L and R are sides and S means end of the trial. The first number is the frame of beginning of the look and second number is the end frame of the look, before the person look away to an other direction. The final E means end of the experiment.
Reply
#6
hermine Wrote:Sorry, I thought thoose informations would have no interest.
In order to be analyzed, any data needs to be complete and accurate.
There's an old saying that will always remain True: 'Garbage in, Garbage out'.

Also, avoid using names that may prove to be reserved, such as array, etc.
ndc85430 likes this post
Reply
#7
Perhaps it will help if we insert None value, so that every letter is followed by 2 values
data = ['B', '494', 'C', '495', '504', 'R', '505', '519', 'S', '519', 'B', '717', 'C', '718', '732', 'L', '733', '742', 'S', '742', 'B', '1008', 'C', '1009', 'R', '1018', '1033', 'S', '1033', 'B', '1236', 'C', '1237', '1242', 'R', '1243', '1261', 'S', '1261', 'B', '1502', 'C', '1503', '1511', 'R', '1512', '1526', 'C', '1527', '1527', 'S', '1527', 'B', '1720', 'C', '1721', '1728', 'R', '1729', '1745', 'S', '1745', 'B', '1941', 'C', '1942', '1951', 'R', '1952', '1966', 'S', '1966', 'B', '2172', 'C', '2173', '2178', 'R', '2179', '2197', 'S', '2197', 'B', '2400', 'R', '2401', '2419', 'C', '2420', 'L', '2421', '2425', 'S', '2425',
'B', '2618', 'R', '2619', '2625', 'C', '2626', '2635', 'R', '2636', '2643', 'S', '2643', 'B', '2846', 'C', '2847', '2854', 'R', '2855', '2864', 'C', '2865', '2865', 'L', '2866', '2871', 'S', '2871', 'B', '3074', 'C', '3075', '3080', 'R', '3081', '3092', 'C', '3093', '3093', 'L', '3094', '3099', 'S', '3099', 'B', '3304', 'C', '3305', '3312', 'R', '3313', '3329', 'S', '3329', 'B', '3530', 'C', '3531', '3537', 'R', '3538', '3548', 'C', '3549', '3555', 'S', '3555', 'B', '3750', 'C', '3751', '3757', 'R', '3758', '3767', 'C', '3768', '3768', 'L', '3769', '3775', 'S', '3775', 'B', '3984', 'C', '3985', '4008', 'R', '4009', '4009', 'S', '4009', 'B', '4234', 'C', '4235', '4241', 'R', '4242', '4252', 'C', '4253', '4253', 'L', '4257', '4259', 'S', '4259', 'B', '4472', 'C', '4473', '4482', 'L', '4483', '4497', 'S', '4497', 'B', '4720', 'C', '4721', '4730', 'L', '4731', '4745', 'S', '4745', 'B', '4962', 'C', '4963', '4986', 'R', '4987', '4987', 'S', '4987', 'B', '5202', 'C', '5203', '5211', 'R', '5212', '5227', 'S', '5227', 'B', '5434', 'C', '5435', '5442', 'S', '5459', 'B', '5658', 'C', '5659', '5666', 'R', '5667', '5683', 'S', '5683', 'B', '5890', 'C', '5891', '5903', 'C', '5904', '5905', 'S', '5905', 'E']


def insert_none(data):
    pk = 0
    for k, x in enumerate(data):
        if x.isalpha():
            if k - pk == 1:
                yield None
                yield None
            if k - pk == 2:
                yield None
            pk = k
        yield x
    yield None
    yield None

newdata = list(insert_none(data))

print(newdata)
Output:
['B', '494', None, 'C', '495', '504', 'R', '505', '519', 'S', '519', None, 'B', '717', None, 'C', '718', '732', 'L', '733', '742', 'S', '742', None, 'B', '1008', None, 'C', '1009', None, 'R', '1018', '1033', 'S', '1033', None, 'B', '1236', None, 'C', '1237', '1242', 'R', '1243', '1261', 'S', '1261', None, 'B', '1502', None, 'C', '1503', '1511', 'R', '1512', '1526', 'C', '1527', '1527', 'S', '1527', None, 'B', '1720', None, 'C', '1721', '1728', 'R', '1729', '1745', 'S', '1745', None, 'B', '1941', None, 'C', '1942', '1951', 'R', '1952', '1966', 'S', '1966', None, 'B', '2172', None, 'C', '2173', '2178', 'R', '2179', '2197', 'S', '2197', None, 'B', '2400', None, 'R', '2401', '2419', 'C', '2420', None, 'L', '2421', '2425', 'S', '2425', None, 'B', '2618', None, 'R', '2619', '2625', 'C', '2626', '2635', 'R', '2636', '2643', 'S', '2643', None, 'B', '2846', None, 'C', '2847', '2854', 'R', '2855', '2864', 'C', '2865', '2865', 'L', '2866', '2871', 'S', '2871', None, 'B', '3074', None, 'C', '3075', '3080', 'R', '3081', '3092', 'C', '3093', '3093', 'L', '3094', '3099', 'S', '3099', None, 'B', '3304', None, 'C', '3305', '3312', 'R', '3313', '3329', 'S', '3329', None, 'B', '3530', None, 'C', '3531', '3537', 'R', '3538', '3548', 'C', '3549', '3555', 'S', '3555', None, 'B', '3750', None, 'C', '3751', '3757', 'R', '3758', '3767', 'C', '3768', '3768', 'L', '3769', '3775', 'S', '3775', None, 'B', '3984', None, 'C', '3985', '4008', 'R', '4009', '4009', 'S', '4009', None, 'B', '4234', None, 'C', '4235', '4241', 'R', '4242', '4252', 'C', '4253', '4253', 'L', '4257', '4259', 'S', '4259', None, 'B', '4472', None, 'C', '4473', '4482', 'L', '4483', '4497', 'S', '4497', None, 'B', '4720', None, 'C', '4721', '4730', 'L', '4731', '4745', 'S', '4745', None, 'B', '4962', None, 'C', '4963', '4986', 'R', '4987', '4987', 'S', '4987', None, 'B', '5202', None, 'C', '5203', '5211', 'R', '5212', '5227', 'S', '5227', None, 'B', '5434', None, 'C', '5435', '5442', 'S', '5459', None, 'B', '5658', None, 'C', '5659', '5666', 'R', '5667', '5683', 'S', '5683', None, 'B', '5890', None, 'C', '5891', '5903', 'C', '5904', '5905', 'S', '5905', None, 'E', None, None]
Reply
#8
(Nov-29-2022, 10:14 AM)hermine Wrote: 'B', '494', 'C', '495', '504', 'R', '505', '519', 'S', '519', 'B', '717', 'C', '718', '732', 'L', '733', '742', 'S', '742',....

B is the beginning of the trial, C, L and R are sides and S means end of the trial. The first number is the frame of beginning of the look and second number is the end frame of the look, before the person look away to an other direction. The final E means end of the experiment.

There is value between end of trial ('S') and start of trial ('B'). Should it be disregarded?
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  mysql id auto increment not working tantony 10 2,453 Oct-18-2022, 11:43 PM
Last Post: Pedroski55
  Character Increment AnokhiRaaz 1 2,513 Apr-22-2021, 04:29 AM
Last Post: buran
  Increment text files output and limit contains Kaminsky 1 3,211 Jan-30-2021, 06:58 PM
Last Post: bowlofred
  Increment formula Kristenl2784 4 2,902 Jul-20-2020, 10:14 PM
Last Post: Kristenl2784
  [openpyxl] Increment cells being pasted into Template Kristenl2784 4 3,593 Jul-16-2020, 10:00 PM
Last Post: Kristenl2784
  How can I increment a List item with in a "for in" msteffes 4 3,579 Aug-14-2019, 08:55 AM
Last Post: DeaD_EyE
  How define iteration interval increment SriMekala 5 4,375 Jun-01-2019, 01:06 PM
Last Post: ichabod801
  SQlite3 quickly increment INT value? jmair 1 2,459 Mar-04-2019, 08:03 PM
Last Post: stranac
  increment variable in while loop Naito 3 2,987 Jan-20-2019, 12:30 PM
Last Post: Naito
  auto increment ambush 7 14,462 Nov-02-2018, 10:52 PM
Last Post: LeSchakal

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020