Extracting Elements from a List in Python
Suppose you have a list containing elements separated by a delimiter, such as a tab character. To extract only the elements before the delimiter, you can utilize Python's list comprehension feature.
Specific Problem:
You have a list:
my_list = ['element1\t0238.94', 'element2\t2.3904', 'element3\t0139847']
How can you remove the tab character (\t) and everything after it to obtain the following result:
['element1', 'element2', 'element3']
Solution:
<code class="python">[i.split('\t', 1)[0] for i in my_list]</code>
Explanation:
Das obige ist der detaillierte Inhalt vonWie extrahiere ich Elemente vor einem Trennzeichen in einer Python-Liste?. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!