Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
What is the correct syntax for using Sum() function in python for lists?
Say we have a list called list_1. When I tried these two syntax I get the same result:
total_1 = Sum(list_1)
total_2 = list_1.Sum()
What is the best syntax for that?
To sum a list of integers or floats, the correct method is:
list_1 = [1, 2, 3]
total = sum(list_1)
print(total)
which results in the output:
6
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 months ago.
Improve this question
import math
import random
possibility_1=0
possibility_2=0
while True:
list = [0, 1]
number_1=random.choice(list)
number_2=random.choice(list)
number_3=random.choice(list)
number_total=str(number_1)+","+str(number_2)+","+str(number_3)
if number_total==(1,1,0) or number_total==(0,0,1):
possibility_1 +=1
if number_total==(1,1,1) or number_total==(0,0,0):
possibility_2 +=1
print(str(number_total)+" possibility_11= "+ str(possibility_1)+" possibility_12= "+ str(possibility_2))
guys the possibility_1 and possibility_2 doesn!t changing. Please help me I want to make a heads or tails code and simulate it then check the possibilities.
Looks like number_total is a string so the output would be "1,1,0" you are comparing with a tuple. This means the if statements will not be invoked.
if number_total=="1,1,0" or number_total=="0,0,1":
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
list1 = [''ABC'',''XYZ'',''PQR'',''MNS'']
to
list1 = ['ABC','XYZ','PQR','MNS']
#I am using this code but it is not working
for i in range(0,len(list1)):
list1[i] = list1[i].replace("''","")
I find list comprehension to be the cleanest solution:
list2 = [s.replace("'", "") for s in list1]
In the snippet above, I iterate through the list binding s to each of the characters (as we traverse the list). In each iteration, I do the replacement.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
def conversion(putlist):
if(len(putlist)>len(set(putlist))):
putlist = set(putlist)
putlist = list(putlist)
print("{}have duplicates".format(putlist))
else:
putlist = set(putlist)
putlist = list(putlist)
print(putlist)
for i in range[brent, camden, redbridge, southwark]:
conversion(i)
error
1
Neglect the indentation
I want to pass 4 lists as arguments to function conversion() with the help of for loop so I ain't have to pass the arguments 4 times.
Replace:
for i in range[brent, camden, redbridge, southwark]: <-------getting error
with
for i in [brent, camden, redbridge, southwark]:
https://www.w3schools.com/python/ref_func_range.asp
range - generator, this function makes a list for you
You are not needed in this function if you already have a list
wiki page about generator: https://wiki.python.org/moin/Generators
for i in [brent, camden, redbridge, southwark]:
conversion(*i)
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I am trying to write a one-line list comprehension that takes a list of tuples and adds them to a new list if the second element of the tuple is not equal to 0
my_list = [x in my_list if x[1] != 0]
this is the code I wrote, says I have invalid syntax
The correct way
my_list = [x for x in my_list if x[1] != 0]
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I'm trying to sort each string inside a list. For example, if I have the list ['bca', 'fed'] then I want my function to return ['abc', 'def'].
Currently trying to do this like so:
lines = [''.join.(sorted(e)) for e in lines]
What's wrong with this syntax?
Just remove the redundant dot (.):
lines = [''.join(sorted(e)) for e in lines]