在 Python 列表中查找位於特定字符串之間的字符串 (Find Strings Located Between Specific Strings in List Python)


問題描述

在 Python 列表中查找位於特定字符串之間的字符串 (Find Strings Located Between Specific Strings in List Python)

我正在編寫從網站中提取數據的代碼,並打印出特定標籤之間的所有文本。每次代碼從標籤中提取數據時,我都會將結果存儲到一個列表中,所以我有一個類似於

Warning
Not
News
Legends
Name1
Name2
Name3
Pickle
Stop
Hello

我想查看這個字符串列表並擁有可以找到的代碼關鍵字 legendspickle 並打印它們之間的任何字符串。

為了在進一步的活動中詳細說明,我可能會創建所有可能的完整列表 legend names,然後,如果它們在我生成列表時出現,則打印出那些再次出現的。對這些問題有任何見解嗎?


參考解法

方法 1:

You can use the list.index() method to find the numerical index of an item within a list, and then use list slicing to return the items in your list between those two points:

your_list = ['Warning','Not','News','Legends','Name1','Name2','Name3','Pickle','Stop','Hello']
your_list[your_list.index('Legends')+1:your_list.index('Pickle')]

The caveat is that .index() returns only the index of the first occurrence of the given item, so if your list has two 'legends' items, you'll only return the first index.

方法 2:

Try this:

words = [
    "Warning", "Not", "News", "Legends", "Name1",
    "Name2", "Name3", "Pickle", "Stop", "Hello"
]
words_in_between = words[words.index("Legends") + 1:words.index("Pickle")]
print(words_in_between)

output:

['Name1', 'Name2', 'Name3']

This assumes that both "Legends" and "Pickle" are in the list exactly once.

方法 3:

For the second approach, you could create a regex alternation of expected matching names, then use a list comprehension to generate a list of matches:

tags = ['Warning', 'Not', 'News', 'Legends', 'Name1', 'Name2', 'Name3', 'Pickle', 'Stop', 'Hello']
names = ['Name1', 'Name2', 'Name3']
regex = r'^(?:' + r'|'.join(names) + r')$'
matches = [x for x in tags if re.search(regex, x)]
print(matches)  # ['Name1', 'Name2', 'Name3']

方法 4:

You can use list.index() to get the index of the first occurance of legends and pickle. Then you can use list slicing to get the elements in between

l = ['Warning','Not','News','Legends','Name1','Name2','Name3','Pickle','Stop','Hello']
l[l.index('Legends')+1 : l.index('Pickle')]
['Name1', 'Name2', 'Name3']

方法 5:

numpys function where gives you all occurances of a given item. So first make the lsit a numpy array

my_array = numpy.array(["Warning","Not","News","Legends","Name1","Name2","Name3","Pickle","Stop","Hello","Legends","Name1","Name2","Name3","Pickle",])

From here on you can use methods of numpy:

legends = np.where(my_array == "Legends")
pickle = np.where(my_array == "Pickle")

concatinating for easier looping

stack = np.concatenate([legends, pickle], axis=0)

look for the values between legends and pickle

np.concatenate([my_list[stack[0, i] + 1:stack[1, i]] for i in range(stack.shape[0])] )

The result in my case is:

array(['Name1', 'Name2', 'Name3', 'Name1', 'Name2'], dtype='<U7')

(by triplecutePeptideWitchsararturTim BiegeleisenEpsi95thomas)

參考文件

  1. Find Strings Located Between Specific Strings in List Python (CC BY‑SA 2.5/3.0/4.0)

#string #Python #list #substring






相關問題

VB.net 如何讓流閱讀器忽略某些行? (VB.net how to make stream reader ignore some line?)

Perl Text::CSV_XS 從字符串中讀取 (Perl Text::CSV_XS read from string)

在 D3 中用逗號格式化數字 (Formatting numbers with commas in D3)

我應該使用什麼-String 或 StringBuilder 將 SQL 查詢存儲在使用許多不同 SQL 查詢的代碼中 (what should i use-String or StringBuilder for storing SQL queries in a code which uses many different SQL queries)

在 python 3.5 的輸入列表中添加美元符號、逗號和大括號 (Adding dollar signs, commas and curly brackets to input list in python 3.5)

使用正則表達式處理字符串 (String Manipulation with regular expression)

如何區分數字字符串和字符串? (How do i distinguish between number string and character string?)

如何將單詞的結尾與 Ruby 中的哈希進行比較? (How can I compare the ending of a word with a hash in Ruby?)

在 Python 列表中查找位於特定字符串之間的字符串 (Find Strings Located Between Specific Strings in List Python)

大寫替代字母 (Alternate letters in upper case)

查找 ia strn 列在同一數據框中的列表列中,並創建具有值的第三列 (Find ia a strn column is in a list column in the same data frame and create a 3rd column with a value)

在算術表達式中使用字符串是否安全? (Is it safe to use strings in arithmetic expressions?)







留言討論