This question already has answers here:
Zip lists in Python
(10 answers)
Closed 2 years ago.
I have two variables
odds=[1,3,5]
evens=[2,4,6]
and i want to print them in a loop like this:
for i in range(0,2):
print(odds[i])
print(evens[i])
to get an output like
1,2,3,4,5,6.
However the above loop doesn't feel very pythonic. Is there way of doing this more pythonicly? perhaps with a loop thats defined via a line like "for odd in odd and for even in even:"?
for x,y in zip(odds,evens):
print(x,",",y)
Related
This question already has answers here:
Sum a list of numbers in Python
(26 answers)
Closed 5 months ago.
I am using a for loop to fill a list with 100 random numbers. I want to add those numbers once the list is complete and I cannot figure out how to do that. I am pretty sure it is something simple that I have just overlooked but it is driving me crazy that I can't get it to work.
Use sum(list)
list = [random.randint(1,9) for i in range(100)]
print(sum(list))
Python has a built in sum function
list_sum = sum(your_list)
This question already has answers here:
Why does "x = x.append(...)" not work in a for loop?
(8 answers)
Closed 2 years ago.
I am trying to re-write this for loop into a function to make it applicable to different time series. It is pretty straightforward but for some reason there are errors with the function. The goal is to subtract the current value with its preceding one:
append doesn't return the modified list, it modifies it in place. This means that
diff = diff.append(value)
will be assigning None to diff, causing your problem.
You just need
diff.append(value)
as you had in the original loop.
This question already has answers here:
str.startswith with a list of strings to test for
(3 answers)
Closed 4 years ago.
I am working on python script that splits text in different blocks based on keywords used in text.
Currently I split text into blocks with sth like this (for 1 block, others have pretty much the same strucure):
if (line.strip().lower().startswith('ключевые навыки')
or line.strip().lower().startswith('дополнительная информация')
or line.strip().lower().startswith('знания')
or line.strip().lower().startswith('личные качества')
or line.strip().lower().startswith('профессиональные навыки')
or line.strip().lower().startswith('навыки')):
But, it is possible that list of keywords is going to expand. Is there a possibility to generate multiple or statements based on some array of possible keywords?
Try this code
values=['ключевые навыки','дополнительная информация','знания']
val=True
#enter any words you want to check
while val
for i in values:
if (line.strip().lower().startswith(i)):
#whatever code you want to implement
val=False
#to exit loop
Hope it helps :)
This question already has answers here:
How do I iterate through two lists in parallel?
(8 answers)
Closed 4 years ago.
Basically, I want something along the lines of:
for coin in all_coins["result"] and coin2 in all_coins2["result"]:
specifiedlist.append(Crypto(coin, coin2))
Any help pointing me into the direction of a proper function or formatting would be appreciated.
You can use itertools.product to create your permutations in a list comprehension
from itertools import product
specifiedlist = [Crypto(coin, coin2) in product(all_coins["result"], all_coins2["result"])]
This question already has answers here:
Iterating over dictionaries using 'for' loops
(15 answers)
Closed 5 years ago.
I want to be able to print out just a single character from this dictionary but I haven't been able to figure out the syntax or find it anywhere. Is there a way to do this in vanilla Python 2.7.x given the code below?
dct = {"c":[["1","1","0"],["0","0","0"]], "d":[["1","1","0"],["1","0","0"]],}
for x in dct:
print [x][0][0]
I want the output to be: 11
Any help is much appreciated!
for x in dct:
print(dct[x][0][0])