简体   繁体   English

如何仅打印python列表中元素的特定部分?

[英]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. 我想做一些事情,例如在每个元素中查找“ over”,然后打印一个从“ over”开始直到下一个逗号的字符串。 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? 如果我直接从文件中读取,我可以使用“ rfind()”来执行此操作,但是由于列表中没有“ rfind()”,我想知道是否还有另一种方法可以执行此操作?

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. 这里的技巧是字符串的find方法返回给定子字符串的开头的索引,如果我们提供第二个参数,则从该整数开始搜索。

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 : 您可以使用split两次,并将结果与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']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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