How does this line look as a regular 'for' loop?
inputList = [int(a) for a in inputList]
What are the advantages to writing a 'for' loop this way as opposed to the more vanilla way?
You are talking about Python syntax which is known as list comprehension. Check out this link
See the following example:
No list comprehension:
list = []
for i in range (0, 9):
if i > 5:
list.append(i)
Output: [6, 7, 8]
List comprehension:
newList = [i for i in list if i>=5]
Output: [6, 7, 8]
Why use list comprehension
List comprehension compressed 4 lines of code into 1 line of code
new_list = []
for a in inputList:
new_list.append(int(a))
inputList = new_list
Probably something like this.
I did something like this and it worked:
enteredList = []
for a in inputList:
enteredList.append(int(a))
Related
I have this list and I want to sort the list. This is just a smaller example of what I want to do, but I get the same error. I dont understand why I can't make this work.
I have tried using google to solve the problem but without luck.
lst = [3, 4, 5, 6]
if lst < 4:
lst.pop()
print(lst)
How can i do this it shows
TypeError:'<' not supported between instances of 'list' and 'in
I think that your goal is to remove all elements in the list that are lesser than 4. You can use this simple list comprehension in order to achieve what you want:
lst = [3, 4, 5, 6]
lst = [elem for elem in lst if elem >= 4]
print(lst)
Output:
[4, 5, 6]
I am stuck on a problem involving adding two lists together.
For example, if list1 was [1,2,3,4] and list2 was [2,4] I would have to return [3,5,3,4]. Or if list1=[0] and list2=[1] I would return [1]
def addsum(list1,list2):
new_list = []
list1[0]+list2[0] = new_list[0]
and so on. This was my first approach but I'm getting a lot of errors. I'm new to lists so I can't use index or lambda functions. I am only allowed to use len(). Would appreciate the help.
You may want to check out 2 python concepts: list comprehension (http://www.secnetix.de/olli/Python/list_comprehensions.hawk) and condensed form of if-else (https://stackoverflow.com/a/2802748/1045285).
l1 = [1, 2, 4, 5]
l2 = [2, 4]
min_len = min(len(l1), len(l2))
rem = l1[min_len:] if len(l1) > len(l2) else l2[min_len:]
res = [e1 + e2 for e1, e2 in zip(l1[:min_len], l2[:min_len])] + rem
I need to change elements in a list, I have the following code:
change_aux(_,_,[],[]).
change_aux(X,Y,[X|T],[Y|S]):-!,change_aux(X,Y,T,S).
change_aux(X,Y,[Z|T],[Z|S]):-change_aux(X,Y,T,S).
flatten2([], []) :- !.
flatten2([L|Ls], FlatL) :-
!,
flatten2(L, NewL),
flatten2(Ls, NewLs),
append(NewL, NewLs, FlatL).
flatten2(L, [L]).
change(X,Y,[X1|Y1],[X2,Y2]):-
flatten([X1|Y1],L),
change_aux(X,Y,L,[X2|Y2]).
Input: change(2,5,[1,[2,[3,2],1]],R).
Print: R = [1, [5, 3, 5, 1]] .
But I need R to be printed like this: R = [1,[5,[3,5],1]]
Could you help me, please?
There are some problems in the code above like in definition change(X,Y,[X1|Y1],[X2,Y2]):- I don't think that the output list should always consists of two elements. Besides that the change_aux predicate needs some work since now it's just traversing the list and not building the nested output list. You could try something that would build recursively the nested levels of the list like:
change(_,_,[],[]).
change(X,Y,[H|T],[H|T1]):- \+is_list(H),dif(H,X),change(X,Y,T,T1).
change(X,Y,[X|T],[Y|T1]):- change(X,Y,T,T1).
change(X,Y,[H|T],[L|T1]):- is_list(H),change(X,Y,H,L),change(X,Y,T,T1).
Note that in the above predicate there is no need to use flatten/2 predicate since we take advantage of the nested levels of input list to build output list.
Example:
?- change(2,5,[1,[2,[3,2],1]],R).
R = [1, [5, [3, 5], 1]] ;
false.
I have an array [1,2,3,4,5,6,1,2,3,4] and I would like to return 5, 6.
If I use set(my_array) I get 1,2,3,4,5,6. Is there a pythonic way to do this. Thanks.
Any help is much appreciated.
#List of data which has every item repeated except for 5 and 6
lst= [1,2,3,4,5,6,1,2,3,4]
#This list comprehension prints a value in the list if the value only occurs once.
print [x for x in lst if lst.count(x)==1]
#Output
[5, 6]
You can use the appropriately named filter method:
>>> i = [1,2,3,4,5,6,1,2,3,4]
>>> filter(lambda x: i.count(x) == 1, i)
[5, 6]
I have two lists that I need to merge into a new list, but the new list needs to contain merged indexes of the original lists. For example:
List1 = [1, 2, 3]
List2 = [a, b, c]
I need the output to be:
finalList = [1a, 2b, 3c]
I need to be able to do this in groovy. I appreciate any help you can provide.
Assuming both lists are the same size, in Groovy 2.4+,
list1 = [1, 2, 3]
list2 = ['a', 'b', 'c']
assert ['1a', '2b', '3c'] == list1.withIndex().collect { it, index -> it + list2[index] }
Alternatively and a bit more simply in Groovy 1.5+,
assert ['1a', '2b', '3c'] == [list1, list2].transpose()*.sum()
The following is very close to doelleri's solution:
In Groovy 2.4+
println ([list1, list2].transpose().collect{it -> it[0] + it[1]})
OUTPUT
[1a, 2b, 3c]