I have two lists : _bufferCarnetList and _deletedWords.
When the user hits OK: I want to remove from _bufferCarnetList the items contained in _deletedWords.
I have tried using the .remove method, but nothing happens.
_bufferCarnetList.remove(_deletedWords).
Is there a way to do it in a direct way, or should I iterate the list and remove each item separately ?
Use the removeWhere method:
_bufferCarnetList.removeWhere((e) => _deletedWords.contains(e));
Related
Number,IceCream
1,Chocolate
2,Vanilla
3,Mixed
Say if I
Number = input("Flavor?:")
I know that I need to index [0] because the numbers are on the first column. I also know that I will need to use .split(",") to remove the commas and to create a list.
Some assistance would be greatly appreciated!
It's confusing whether you plan to include integers in the list with the strings or not
Method 1: including integers with strings(flavor), create a list of tuples
icecream=[(1,'choc'),(2,'mix'),(3,'blueberry')]
print(icecream[0][1])
print(icecream[2][1])
Note: tuples are immutable
Method 2: I believe the best way to do this would be to use a dictionary instead of list. As dictionary has (Key, value) pairs, you could assign key(integer) to values(flavor), which then would make it easy accessing items just by keys(integers in your case) ex.
Ice_cream_flavors={1:"chocolate", 2:"vanilla", 3:"mixed"} #dictionary
to access values, you could use methods available in dictionary use get(), items()
Note: items() returns a tuple for each key,value pair.
ex.
Ice_cream_flavors={1:"chocolate", 2:"vanilla", 3:"mixed"}
new=Ice_cream_flavors.items()
for k,v in new:
if input==k:
print(v)
I have a collection A of let's say 100 items. From that list I want to perform a where clause which can rule out let's say 20 of items.
Is there a way to use Select clause or something else on items in which I could use external method that returns 2 items.
I would need to end up with 160 objects from the original list.
What I currently have is
public List<A> ToAList(B item)
{
return new List<A> {new A(), new A()};
}
If I make this call
originalList.Where(x => true).Select(y => ToAList(y)).ToList();
I end up having a list of 80 (from pseudo example) two-item A lists instead of a list containing 160 objects A.
I am looking for a way to avoid loops. Just plain Select or AddRange trick that could result in one list.
You can use SelectMany:
originalList.Where(x => true).SelectMany(y => ToAList(y)).ToList();
I thought that [] and list() were two equal ways to create a list. But if you want a list with dictionnary keys,
var = [a_dict.keys()]
doesn't work since type(var) is [dict_keys], correct syntax is :
var = list(a_dict.keys())
I couldn't find an good explanation on this behaviour. Do you have one ?
TL;DR:
list() is the same as []
list(obj) is not the same as [obj]
a_dict.keys() is a dictionary view object, it returns an object which can be iterated to yield the keys of a_dict. So this line:
[a_dict.keys()]
is saying in python "I'm making a list with one element in it" and that one element is the dict keys iterator. It's a list literal in the syntax.
Now this line:
list(a_dict.keys())
is a call to the list builtin function. This function list attempts to iterate the argument and produce a list. It's a function call in the grammar.
The equivalent list literal (actually list comprehension) would be instead:
[key for key in a_dict.keys()]
Finally, note that dictionary objects iterate by keys anyway,
list(a_dict.keys()) would usually be written more simply as as list(a_dict) instead.
Hope this helps.
[a_dict.keys()]
This one puts a single element in the list. Just as if you were to write [1]. In this case that one element is going to be a list.
list(a_dict.keys())
The constructor accepts a sequence and will add all elements of the sequence to the container.
My objective is to search for the word after "IDENTIFIER" in the list. I have input as list which can have list inside list inside list or just list inside list. Basically we have to find how many list inside lists are present and then perform the check.
I have lists inside lists as shown below
[['CLASS', 'class'], ['CLASS', 'animal'], ['LCURLY', '{'], [[['INT', 'int'], ['IDENTIFIER', 'a'], ['SEMICOLON', ';']], [['BOOL', 'bool'], ['IDENTIFIER', 'x'], ['SEMICOLON', ';']]], ['RCURLY', '}']]
I want to search for next element after the word IDENTIFIER. that is i am looking for "a" , and "x". This is case of list inside list inside list. That is two groups of nested list.
But I also have a list as follows:
[['INT', 'int'], ['IDENTIFIER', 'e'], ['SEMICOLON', ';']]
This is case of only list inside list. Now i want to search for word after IDENTIFIER that is "e".
So, Basically all I want to know how to search if there is list inside list and if there is no list inside list then perform a check. All I could think is write a function where i would pass my list and then check if there exist a list inside the list if there is still list then call the function again ( recursively ) and once there is no such list present then perform the check.
Is this the right way to go ahead ? Are there any better suggestions than this .
I would like to check all list values in a list and change them if necessary.
p.e.
I want to check the next lists if there are values higher or lower then the next values:
min-value = 6
max-value = 22
mylist = ['4-8','25','16-19','21-32']
if one of the list values is below the min-value or higher then the max-value, the list values must be changed to the min-value and max-value. p.e. in example, the new list must be:
mylist = ['6-8','22','16-19','21-22']
if the entire value of the list item is below the min-value or higher then the max-value the list item can be removed.
How can I check my list values and change them?
There are two approaches. In the procedural one, you iterate over the list items and modify or skip the element:
let newlist = []
for element in mylist
" Parse element.
if ! OutsideBounds(element)
call add(newlist, AdjustBounds(element))
endif
endfor
In the functional programming approach, you use the built-in map() to modify elements (i.e. adjust the bounds), but that one cannot remove elements. So just empty those elements and then do a second pass with filter() to remove them. Note that both functions modify the original lists, so use copy() if you need to keep the original.
call filter(map(mylist, 'AdjustBounds(v:val)'), '! OutsideBounds(v:val)')
I hope I don't need to tell you how to write the AdjustBounds() and OutsideBounds() functions...