简体   繁体   中英

How to print only a certain part of elements from a list in python?

So I have a list:

['13:57:09.273 0,Type=IsXover Count=4,mcuTs=0x000265C7,lp-isD=1',
 '13:57:09.341 1,Type=Xover Count=47,mcuTs=0x0002660A,lp-isD=0',
 '13:57:15.389 0,Type=Xover Count=48,mcuTs=0x00027D87,lp-isD=1']

I wanna do something like looking for 'over' in each element and then printing a string starting from 'over' until the next comma. so the result should look like this:

['over Count=4','over Count=47','over Count=48']

I could do this using 'rfind()' if I was directly reading from a file but since lists don't have 'rfind()' I was wondering if there is another way of doing this?

A simple way would be:

list = ['13:57:09.273 0,Type=IsXover Count=4,mcuTs=0x000265C7,lp-isD=1', 
    '13:57:09.341 1,Type=Xover Count=47,mcuTs=0x0002660A,lp-isD=0',
    '13:57:15.389 0,Type=Xover Count=48,mcuTs=0x00027D87,lp-isD=1']

result = []
for string in list:
    for index, sub_string in enumerate(string.split(',')):
        if index == 1:
           result.append(sub_string[sub_string.find('over'):])

print(result) 

A list comprehension version of this code would not be as readable as the code above, but here it is either way:

result = [sub_string[sub_string.find('over'):] for string in list for index, sub_string in enumerate(sub_string.split(',')) if index == 1]
print(result)

Both code snippets will give the desired result of

['over Count=4','over Count=47','over Count=48']

You can use regular expression to solve the problem:

import re

Structure = ['13:57:09.273 0,Type=IsXover Count=4,mcuTs=0x000265C7,lp-isD=1', 
        '13:57:09.341 1,Type=Xover Count=47,mcuTs=0x0002660A,lp-isD=0',
        '13:57:15.389 0,Type=Xover Count=48,mcuTs=0x00027D87,lp-isD=1']

Counts = []
for element in Structure:
    sub_array = element.split(",")
    valid = re.match(r"^.*(over Count=(\d+))", sub_array[1].strip())
    Counts.append(valid.group(1))

print(Counts)

Which would print:

['over Count=4', 'over Count=47', 'over Count=48']

Without using regex, we can use string methods:

a = ['13:57:09.273 0,Type=IsXover Count=4,mcuTs=0x000265C7,lp-isD=1',
     '13:57:09.341 1,Type=Xover Count=47,mcuTs=0x0002660A,lp-isD=0',
     '13:57:15.389 0,Type=Xover Count=48,mcuTs=0x00027D87,lp-isD=1']
b = []
for s in a:
    i1 = s.find('over')
    i2 = s.find(',', i1)
    b.append(s[i1:i2])
print(b)

gives

['over Count=4', 'over Count=47', 'over Count=48']

Or, if you want a one-line list comprehension:

[s[s.find('over'):s.find(',', s.find('over'))] for s in a]

The trick here is that the find method of the string returns the index of the start of the given substring, and if we provide a second argument, the search starts from that integer.

May this help you out

itemList  = ['13:57:09.273 0,Type=IsXover Count=4,mcuTs=0x000265C7,lp-isD=1',
 '13:57:09.341 1,Type=Xover Count=47,mcuTs=0x0002660A,lp-isD=0',
 '13:57:15.389 0,Type=Xover Count=48,mcuTs=0x00027D87,lp-isD=1']

for item in itemList:
    if ‘over’ in item:
        indexi = item.find(‘over’)
        indexy = item.find(‘,mcuTs’)
        print(item[indexi:indexy]

You can use split twice and concatenate the result with over :

inList = ['13:57:09.273 0,Type=IsXover Count=4,mcuTs=0x000265C7,lp-isD=1',
 '13:57:09.341 1,Type=Xover Count=47,mcuTs=0x0002660A,lp-isD=0',
 '13:57:15.389 0,Type=Xover Count=48,mcuTs=0x00027D87,lp-isD=1']

newList = ['over' + elem.split(',')[1].split('over')[1] for elem in inList]
print(newList)

Output:

['over Count=4', 'over Count=47', 'over Count=48']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM