Example #1
0
def remove_list_from_list(input_list,
                          list_toremove_src,
                          warning=True,
                          debug=True):
    '''
	remove a list "list_toremove_src" from a list "list_src" if found, skip if not found

	parameteters:
		input_list:				a list to be removed from
		list_toremove_src:		a list to be removed

	outputs:
		list_remained:			a list of remaining elements after removal
		list_removed:			a list of elements to be successfully removed 
								(as some elements in list_toremove_src may not found in list_src, where the removal fails)
	'''
    list_remained = safe_list(input_list, warning=warning, debug=debug)
    list_toremove = safe_list(list_toremove_src, warning=warning, debug=debug)
    list_removed = []
    for item in list_toremove:
        try:
            list_remained.remove(item)
            list_removed.append(item)
        except ValueError:
            if warning:
                print(
                    'Warning!!!!!! Item to remove is not in the list. Remove operation is not done.'
                )

    return list_remained, list_removed
Example #2
0
def remove_unique_item_from_list(input_list, item, warning=True, debug=True):
    '''
	remove all instances of a single item from a list

	parameters:
		input_list:				a list to be removed from

	outputs:
		list_remained:			a list of remaining elements after removal
		count_removal:			number of times the requested item to be removed
	'''
    list_remained = safe_list(input_list, warning=warning, debug=debug)
    count_removal = 0
    while 1:
        try:
            list_remained.remove(item)
            count_removal += 1
        except ValueError:
            if warning and count_removal == 0:
                print(
                    'Warning!!!!!! Item to remove is not in the list. Remove operation is not done.'
                )
            break

    return list_remained, count_removal
Example #3
0
def find_unique_common_from_lists(input_list1,
                                  input_list2,
                                  warning=True,
                                  debug=True):
    '''
	find common items from 2 lists, the returned elements are unique. repetitive items will be ignored
	if the common items in two elements are not in the same order, the outputs follows the order in the first list

	parameters:
		input_list1, input_list2:		two input lists

	outputs:
		list_common:	a list of elements existing both in list_src1 and list_src2	
	'''
    input_list1 = safe_list(input_list1, warning=warning, debug=debug)
    input_list2 = safe_list(input_list2, warning=warning, debug=debug)

    common_list = list(set(input_list1).intersection(input_list2))
    return common_list
Example #4
0
def remove_list_from_index(list_src, list_index_src, warning=True, debug=True):
    '''
	remove a list "list_to_remove" from a list "list_all_src" based on value
	'''
    if debug:
        assert islist(list_src) and islist(
            list_index_src), 'input lists are not valid'

    input_list = safe_list(list_src, warning=warning, debug=debug)
    index_list = safe_list(list_index_src, warning=warning, debug=debug)
    index_list.sort(reverse=True)
    for item_index in index_list:
        try:
            del input_list[item_index]
        except ValueError:
            if warning:
                print(
                    'Warning!!!!!! Item to remove is not in the list. Remove operation is not done.'
                )

    return input_list
Example #5
0
def reverse_list(input_list, warning=True, debug=True):
    '''
	reverse the order of a list

	parameters:
		input_list:		a list

	outputs:
		reversed_list:	the list in a reverse order
	'''
    input_list = safe_list(input_list, warning=warning, debug=debug)
    reversed_list = input_list[::-1]
    return reversed_list