splitting str into int and then turning into a dictionary [duplicate] - python

This question already has answers here:
Product code looks like abcd2343, how to split by letters and numbers?
(6 answers)
Split string based on regex
(3 answers)
Closed 2 years ago.
I am trying to concert a list which has words and and points score next to it into a dictionary
the list looks roughly like this ("oliver34", "jack17" , "jane56")
I want to split this into ("oliver", "34" "jack" , "17" , "jane", "56"
so that I can turn them into a dictionary.
if this can be done without importing anything that would be preferable
sorry if this does make sense first time using stack overflow and im not good at coding

If the variables inside the list always have the same format which is a name followed by a number, then you can just do this:
def text_num_split(item):
for index, letter in enumerate(item, 0):
if letter.isdigit():
return [item[:index],item[index:]]

Related

Checking for 2 different strings in the same list [duplicate]

This question already has answers here:
Check if something is (not) in a list in Python
(4 answers)
Closed 2 months ago.
What I mean by that is, that I want to take a list, let's say"
["i","am","you","pure","fact"],
then check whether this list contains 2 different strings, like "i" and "pure", and then if true, it runs something.
This is what I am looking for, but in PYTHON.
I don't know how to do this sort of check.
Try this out
elements: list = ["i","am","you","pure","fact"]
str1: str = "i"
str2: str = "pure"
if str1 in elements and str2 in elements:
# Execute code
...

Take integer from input with string and integer [duplicate]

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

Subset a string based on variable [duplicate]

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])

Is there a function for replace a value through the list? [duplicate]

This question already has answers here:
Removing Punctuation From Python List Items
(6 answers)
Closed 3 years ago.
Currently, I'm working on a list like this
txt=['S&P 500 Index', '2,905.03', '+4.58', '+0.16%']
Since I need to convert all the numerical value into float rather than str, in order to use float(), I have to get rid of ',' and '%'. Is there a smart way to do this in a line?
Now,I'm doing stuff like this. Change everything by finding the exact index.
txt[1]=txt[1].replace(",","")
txt[3]=txt[3].replace("%","")
I would like to see something like this
['S&P 500 Index', '2905.03', '+4.58', '+0.16']
If the first element may not contain % or , you can apply the replace operations on it too without changing its value.
txt=['S&P 500 Index', '2,905.03', '+4.58', '+0.16%']
txt = [s.replace('%', '').replace(',','') for s in txt]
print(txt)

How can I replace a integer that shows up twice in a random list [duplicate]

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.

Categories