Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I want to know how Python programmers would write the following snippet:
for i in range(10):
indexVector[i] = empAvg[i] + upperBound(t, pullCount[i])
Here t is a constant. As you can see, I am used to C/C++ style code but want to use Python the right way.
You can zip the lists empAvg and pullCount to iterate through them element-wise without the need for the i counter, then use a list comprehension to create indexVector
indexVector = [emp + upperBound(t, pull) for emp, pull in zip(empAvg, pullCount)]
If you want to use list comprehension to create indexVector (assuming it does not have any other value outside the 10 indexes that you entered in your snippet) , you can use -
indexVector = [empAvg[i] + upperBound(t, pullCount[i]) for i in range(10)]
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
this is dictionary I don't know how to use sort and reverse.
for some reason don't print the dictionary
disc: {'clave1': ['a','b','c'],
'clave2': ['ship','car','house'],
'clave3': [2,1,3]}
I get that the 'disc' is a variable holding the dictionary.
In this case just sort and reverse the values that are lists.
disc = {'clave1': ['a','b','c'],
'clave2': ['ship','car','house'],
'clave3': [2,1,3]}
for l in disc.values():
l.reverse()
for l in disc.values():
l.sort()
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed last year.
Improve this question
d = {1:'a', 2:'b'}
#s = '1|a;2|b'
s = ';'.join([str(k)+'|'+d[k] for k in d])
Is there a better way to do this conversion?
I'd only make two small changes:
Use f-strings
Use a generator expression instead of a list comprehension, which would remove the need to hold all the values in memory prior to joining. It's not really a big deal unless you have thousaaaands of key/value pairs, though.
s = ';'.join(f'{k}|{d[k]}' for k in d)
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 1 year ago.
Improve this question
Below is my list,
['pending/', 'pending/2021-08-01/', 'pending/2021-06-01/', 'pending/2021-06-18/']
And I need to sort the list and filter it to a below format. Please suggest a quicker way to achieve it
['pending/2021-06-01/', 'pending/2021-06-18/', 'pending/2021-08-01/']
When your format is fixed and always starts with "pending" you can use the normal sorted function and count the / in a list comprehension.
>>> values = ['pending/', 'pending/2021-08-01/', 'pending/2021-06-01/', 'pending/2021-06-18/']
>>> sorted(x for x in values if x.count('/') == 2)
['pending/2021-06-01/', 'pending/2021-06-18/', 'pending/2021-08-01/']
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
eg-In this instead of using 21 (a value), I want to use a variable to generalize it
print("{:-^21}".format(".|."*(2*(i+1)-1)))
I want to use something like this
print("{:-^M}".format(".|."*(2*(i+1)-1)))
That can easily enough be done. For example:
M = 40
i = 3
print("{val:-^{width}}".format(width=M, val=".|."*(2*(i+1)-1)))
Outputs:
---------.|..|..|..|..|..|..|.----------
You could also do it with f-strings (note the outer ' because " is used on the inner expression):
print(f'{".|."*(2*(i+1)-1):-^{M}}')
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 4 years ago.
Improve this question
I'm having trouble understanding a particular list comprehension example. In the sample code below a list comprehension is used for the parameter of the set method. Most examples I've seen usually look like this "x for x in y". I've read documentation but it isn't clearing it up for me. What exactly is [marks for name] doing. Shouldn't it be [marks for marks]?
marksheet = []
for _ in range(0,int(input())):
marksheet.append([input(), float(input())])
second_highest = sorted(list(set([marks for name, marks in marksheet])))[1]
print('\n'.join([a for a,b in sorted(marksheet) if b == second_highest]))
Your code is using Python's list comprehension to create a list from another list based on the condition.
See more about list comprehensions here:
https://www.programiz.com/python-programming/list-comprehension
The [1] is accessing the item at index 1 (2nd item) of your sorted list.
As for .join, each line in the sorted list is being printed out with a newline (\n) between each one.