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:
以上がPython リストの区切り文字の前の要素を抽出するにはどうすればよいですか?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。