This question already has answers here:
Python slice how-to, I know the Python slice but how can I use built-in slice object for it?
(6 answers)
Closed 3 years ago.
Suppose we have a long string, for example:
s = "The long-string instrument is a musical instrument in which the string is of such a length that the fundamental transverse wave is below what a person can hear as a tone."
Now, We all know how to extract from this string letters based on indexes:
z = s[18:26]
print(z)
strument
But is there any way, how I can assign this indexes to a variable, and then subset the list based on this variable? It should look something like that:
z = [18:26]
print(s.z)
strument
What you look for are the slice objects:
z = slice(18,26)
print(s[z])
Related
This question already has answers here:
How do I split a string into a list of words?
(9 answers)
Split a string by a delimiter in python
(5 answers)
Apply function to each element of a list
(4 answers)
Closed 4 months ago.
This is the input and output I am looking for given the user input
I want to do this using a function. I am assuming you will have to iterate through each set of points to convert them to integers, then back to a list?
Partial answer:
import re
a = '3,4 5,6 7,8 12,8 3,456'
Now you could use:
raw_coords = re.findall(r'[0-9]+,[0-9]+', a)
But then you'd have to use more regex to extract x and y from each "x,y" string
A more straightforward way would be to separately extract the x values and y values from your initial list: look up "lookbehind assertions" and "lookahead assertions".
For example:
xlist = re.findall(r'[0-9]+(?=,)', a)
(explanation: this finds all maximal strings of digits that are placed just before a ',')
For now, I'll let you do some research and try to complete the process.
This question already has answers here:
Pythonic way to find maximum value and its index in a list?
(11 answers)
Getting the index of the returned max or min item using max()/min() on a list
(23 answers)
Closed 1 year ago.
list = [3,5,1,8,9]
I want to find positions of the maximum value in the list
This is pretty simple, but it will give you the index of the first occurrence:
>>> l = [3,5,1,8,9]
>>> l.index(max(l))
4
I strongly suggest you not use the name of built-in functions as list for variables.
This question already has answers here:
Apply function to each element of a list
(4 answers)
Python Splitting Array of Strings
(6 answers)
Split each string in list and store in multiple arrays
(4 answers)
Closed 1 year ago.
I have a list
list = ['a:b:c:d', 'e:f:g:h', 'i:j:k:l']
What I'm trying to do is make a list of each of it's elements. Something like this:
listelement[0] = list[0].split(':')
print(listelement[0][1])
output: b
I can't seem to figure out how to create something that works similarly to this.
You can try list comprehension to do what you want.
new_list = [element.split(':') for element in list].
Also I would advise against the use of list as a name, since it's a reserved word.
This question already has answers here:
Generate 'n' unique random numbers within a range [duplicate]
(4 answers)
Closed 7 years ago.
I have a list that looks like this:
l = [random.randint(0,9),random.randint(0,9),random.randint(0,9),random.randint(0,9)]
but if it outputs something like this[9,0,5,5] what can I do replace the repeating integer?
If the goal is to get four unique items in random order, let Python do the work for you:
l = random.sample(range(10), 4)
random.sample is intended specifically for "random sampling without replacement", which appears to be the goal of your code.
This question already has answers here:
Create a list with initial capacity in Python
(11 answers)
Closed 9 years ago.
I want to do the following :
l = list()
l[2] = 'two'
As expected, that does not work. It returns an out of range exception.
Is there any way to, let say, define a list with a length ?
Try this one
values = [None]*1000
In place of 1000 use your desired number.