Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
items = {
'flour': [20, 10, 15, 8, 32, 15],
'beef': [3,4,2,8,2,4],
'bread': [2, 3, 3],
'cc': [0.3, 0.5, 0.8, 0.3, 1]
}
I ned to get the third element on each of the ingredients' bracket,
the answer will be (15, 2, 3, 0.8)
Please, thanks for your help!
You can do :
op = [v[2] for v in items.values()]
print(op)
Note: It won't maintain the same order in prior versions of python3.7
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 months ago.
Improve this question
I want to split the time into individual digits.
For Eg. We usually look at time in HH: MM: SS
I want to break it down and access both the H, M, and S separately. Like HH should be accessible as H1 [the first digit] and H2 [the second digit] and so on for minutes and seconds.
Please note I am looking for real-time values of the broken variables.
It is possible like this:
from datetime import datetime
# We get the current time.
d = datetime.now()
time_now = d.strftime("%H:%M:%S")
print("time:", time_now)
# We divide the received time into elements.
time_split = [int(t) for t in time_now if t != ":"]
print(time_split)
time: 17:08:39
[1, 7, 0, 8, 3, 9]
Where H1= time_split[0] etc.
Or you can create a dictionary with the variables you need:
var_list = ("h1", "h2", "", "m1", "m2", "", "s1", "s2")
time_dict = {v: int(t) for v, t in zip(var_list, time_now) if t != ":"}
print(time_dict)
{'h1': 1, 'h2': 7, 'm1': 0, 'm2': 8, 's1': 3, 's2': 9}
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
I have a list of tuples of data and a datetime object.
..[(datetime.datetime(2020, 1, 3, 3, 22, 36), 43.988269794721404), (datetime.datetime(2020, 1, 3, 3, 22, 36), 43.988269794721404), (datetime.datetime(2020, 1, 3, 3, 22, 36), 43.988269794721404), (datetime.datetime(2020, 1, 3, 3, 22, 36),..
I am trying to create a dictionary from this list of tuples. So I call
d = dict(tuple_list)
BUT, the output is simply the last element in the list , {datetime.datetime(2020, 1, 3, 3, 22, 36): 43.988269794721404}.
What is going on here? I am using python 2.7. I also tried
new_dict = {k: v for k, v in zip(keys, values)} but that didn't work either! Thanks for the help!
First of all NO python 2.7
Your getting the last element because you list of tuples all have the same first value.
A dict MUST have unique keys.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I receive an input int that I don't previously know. How can I create a new list of integer from 0 to that int? An easy way please. Thank you in advance.
I mean that:
k = n
...
list = [0, ... , k]
numbers = list(range(k+1)) # For Python 2, it is just range(k+1).
Given an integer k, which you can get with input.
The range creates an iterator (in Python 3) with those numbers (k+1 because it is not inclusive, but you need it to be), then we make it into a list.
Demo:
>>> k = int(input())
16
>>> numbers = list(range(k+1))
>>> numbers
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
A simple function would look like this, assuming you've got your input number n:
For python2.x:
def get_range(n):
return range(n + 1)
For python3.x:
def get_range(n):
return list(range(n + 1))
In either case:
int_list = get_range(10)
>>> print(int_list)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a list of lists with data like credit pays schedule:
[ [pay_number, pay_date, pay_amount], [pay_number, pay_date, pay_amount], e.t.c ]
how can I get next pay_date from it ?
You just need to iterate over the list:
thelist = [ [pay_number, pay_date, pay_amount], [pay_number, pay_date, pay_amount], ... ]
for sub_list in thelist:
current_pay_date = sub_list[1]
# do something with current_pay_date
If you want to access the next pay_date:
for i in range(len(thelist)):
sub_list = thelist[i]
current_pay_date = sub_list[1]
next_sub_list = thelist[i+1]
next_pay_date = next_sub_list[1]
Hope this was what you were looking for.
You can use the min function to get the element from your list which is the closest to today by using a custom key function:
>>> items = [[1, datetime.date(2014, 2, 28), 1], [2, datetime.date(2014, 2, 7), 2], [3, datetime.date(2014, 2, 14), 3]]
>>> min(items, key=lambda x: x[1] - datetime.date.today())
[2, datetime.date(2014, 2, 7), 2]
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
I am doing a problem in which i have to code about this problem-
>>> getNumbers(10)
[100, 64, 36, 16, 4, 0, 4, 16, 36, 64, 100]
>>> getNumbers(9)
[81, 49, 25, 9, 1, 1, 9, 25, 49, 81]
I am getting answers with my code but i am not satisfied with my code,please suggest some options to improve this code.
def getNumbers(num):
myList=[]
mylist=[]
if num%2==0:
for numbers in range(num,-2,-2):
myList.append(numbers**2)
for numbers in range(2,num+2,2):
mylist.append(numbers**2)
print myList+mylist
elif num%3==0:
for numbers in range(num,-1,-2):
myList.append(numbers**2)
for numbers in range(1,num+2,2):
mylist.append(numbers**2)
print myList+mylist
else:
print(mylist)
4 for loops!!! this is what teasing me here!!!
Like this?
def getNumbers(n):
return [i * i for i in range(-n, n + 1, 2)]