python - Modifying list elements based on key word of the element -
i have many lists want operations on specific elements. if have like:
list1 = ['list1_itema', 'list1_itemb', 'list1_itemc', 'list1_itemd'] list2 = ['list2_itema', 'list2_itemc','list2_itemb']
what interest me item 'itemc' wherever occurs in lists , need isolate element contain itemc next manipulations on it. thought sorting lists in such way itemc occupies first index achieved list[0] method.
but in case itema, itemb, itemc , itemd biological species names , dont know how force list element occupy first index (that element string e.g 'cow' in analysis or 'itemc' here). possible python?
you can extract items containing "itemc"
without ordering, or worrying how many there are, "generator expression":
itemcs = [] lst in (list1, list2): itemcs.extend(item item in lst if "itemc" in item)
this gives itemcs == ['list1_itemc', 'list2_itemc']
.
Comments
Post a Comment