This question already has answers here:
Numpy taking only first character of string
(2 answers)
Closed 2 months ago.
Why do I get only the first character in the result?
enter image description here
label = np.empty([17,2],dtype=str)
label[1][1]="asd"
label[2][1]="asd"
print(label)
I don't know if np.empty can input a string
you can use dtype='object' instead of 'str'
test = np.empty((2,2), dtype='object')
test[0,0] = "ashok"
print(test)
Explanation : actually string is object which hold the character array values. so by default indexing in string location the first char not whole string.
example
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:
How to extract numbers from a string in Python?
(19 answers)
Closed 1 year ago.
I want to figure out how to take the integer from a input when there is both a integer and string.
For example, if I input "hello 3", is there a way I can separate the "3" to another variable aside from the "hello"?
Would this work for you:
myInput=input() # Get Input
myString,myIntStr=myInput.split(" ") # Split in to list based on where the spaces are
myInt=int(myIntStr) # Convert to int
This question already has answers here:
How can I convert a string with dot and comma into a float in Python
(9 answers)
Closed 2 years ago.
I have a problem with turning a string into a float value. I'm screaming a website and trying to get the prices in to float values, but the problem is that the prices can look like this:
$2,549.98
$2,262.64
$999.00
marketprice = driver.find_element_by_xpath('/html/body/app/content-holder/marketplace-detail/landfield-detail/div/div/div[2]/div/div[1]/div[2]/span')
userprice = driver.find_element_by_xpath('/html/body/app/content-holder/marketplace-detail/landfield-detail/div/div/div[2]/div/div[1]/div[6]/span')
print(marketprice.text, userprice.text)
imarketprice = float(marketprice.text[1:])
iuserprice = float(userprice.text[1:])
When I try to convert the error I get:
ValueError: could not convert string to float: '2,549.98'
Is the problem with it that there are a comma and a dot?
Just remove the commas using:
imarketprice = float(marketprice.text[1:].replace(",", ""))
This question already has answers here:
Check if a string contains a number
(20 answers)
Closed 2 years ago.
I have a working function that takes an input as string and returns a string. However, I want my function to return an empty string ("") if the input contains any number in whatever position in the string.
For example :
>>> function("hello")
works fine
>>> function("hello1")
should return ""
The main thing you need for that is the method "isdigit()" that returns True if the character is a digit.
For example:
yourstring="hel4lo3"
for char in yourstring:
if char.isdigit():
print(char)
Will output 4 and 3.
I think it is a good exercise for you trying to do your code from that!
This question already has answers here:
How to use Python sets and add strings to it in as a dictionary value
(2 answers)
Closed 4 years ago.
I am trying to convert a string to a set containing that string. How can I do this without splitting?
when I write:
set("abc")
the result is:
{'a','b','c'}
but I want it to be:
{"abc"}
Doku set(iterable) will create a set of each element of an iterable - strings are iterable - hence you create a set of the characters of the string.
If you want whole strings, use
k = {"mystring",}
or
k = set( ["mystring"] ) # wrap your string into another iterable