-3

I need some help with getting numbers after a certain text For example, I have a list:

  ['Martin 9', '    Leo 2 10', '    Elisabeth 3']

Now I need to change the list into variables like this:

Martin = 9
Leo2 =10
Elisabeth = 3

I didn't tried many things, because I'm new to Python.

Thanks for reading my question and probably helping me

Laurenz Z
  • 81
  • 7
  • 2
    What have you tried already? – DeepSpace Feb 13 '19 at 21:13
  • 2
    So your question has nothing to do with [tag:Selenium], it's purely a String parsing problem? – SiKing Feb 13 '19 at 21:34
  • Yeah but the list is out of selenium so I wrote it down there sorry – Laurenz Z Feb 13 '19 at 21:45
  • Edit your question and post the relevant HTML, the code you've tried, and a better description of what you are actually trying to accomplish. – JeffC Feb 13 '19 at 22:09
  • 1
    The HTML is a different language. I didn't tried any working code and I want to get a the "Leo 10" to a variables Leo = 10 – Laurenz Z Feb 13 '19 at 22:17
  • It sounds like you want some type of list of tuples or a dictionary, where you can get the number based on the name. Maybe that'll help in googling :) If this is still unanswered tomorrow I may try to find some time at a solution. Welcome to stack! – mrfreester Feb 13 '19 at 23:16
  • Thanks for trying to help me. I edited the question a bit, so I hope it's easier to understand. – Laurenz Z Feb 14 '19 at 07:16
  • I have the solution, i'll post it in an answer – Laurenz Z Feb 28 '19 at 16:19

4 Answers4

1

I suppose you get some list with using selenium so getting integer from list works without using re like :

list = ["Martin 9","Leo 10","Elisabeth 3"]
for a in list:

    print ''.join(filter(str.isdigit, a))

Output :

9
10
3
Omer Tekbiyik
  • 4,255
  • 1
  • 15
  • 27
  • I don't have the list "word","word", it's written in Line by line. And then I only want the number after Martin not everyone. To specify it more: I'm trying to automate a Webgame with selenium and there I have to send cars. How many cars and what type of car is written in a list there. – Laurenz Z Feb 13 '19 at 21:34
  • `print type(value)` if is string remove for condition just use `' '.join... `is list or array use the above code – Omer Tekbiyik Feb 13 '19 at 21:36
  • I'll try to say what variables I need there out of this list. I need Martin = 9 Leo =10 and Elisabeth =3 – Laurenz Z Feb 13 '19 at 21:46
0

Lets assume you loaded it from a file and got it line by line:

numbers = []
for line in lines:
   line = line.split()
   if line[0] == "Martin": numbers.append(int(line[1]))
JohnTanner
  • 66
  • 4
0

I don't think you really want a bunch of variables, especially if the list grows large. Instead I think you want a dictionary where the names are the key and the number is the value.

I would start by splitting your string into a list as described in this question/answer.

s=
  "Martin 9
  Leo 10
  Elisabeth 3"
listData = data.splitlines()

Then I would turn that list into a dictionary as described in this question/answer.

myDictionary = {}
for listItem in listData:
    i = listItem.split(' ')
    myDictionary[i[0]] = int(i[1])

Then you can access the number, in Leo's case 10, via:

myDictionary["Leo"]

I have not tested this syntax and I'm not used to python, so I'm sure a little debugging will be involved. Let me know if I need to make some corrections.

I hope that helps :)

mrfreester
  • 1,981
  • 2
  • 17
  • 36
  • Or, maybe you really want a list of person [objects](https://www.w3schools.com/python/python_classes.asp) that stores the name and number so you don't have to deal with typos. – mrfreester Feb 14 '19 at 18:20
  • Looks like the original question was changed to add additional spaces and characters (ie. `Leo 2 10`). That makes things more complicated and this answer is now only a partial solution. At this point you might want to research how to split the last word off of a line (instead of by space in this answer from this line `listItem.split(' ')`) to get the number separate and combine that with this answer. – mrfreester Feb 15 '19 at 15:21
  • Additional note... turning the data into variables (ie. `"Leo"` becoming a variable `leo`) is not really how variables are used in programming, which is why nobody is really giving any insight in how to do that. Variables are typically more abstract representations of the data they hold. While that's not strictly true, it's hard to imagine how your data set would be an exception to that. Hope that helps :) – mrfreester Feb 15 '19 at 15:29
0
s="""Leo 1 9
Leo 2 2"""

I shortened the list here

look for the word in the list: if "Leo 1" in s: then get the index of it:

i = s.index("Leo 1")

then add the number of character of the word

lenght=len("Leo 1")

And then add it: (+1 because of the space)

b = i + lenght + 1

and then get the number on the position b:

x = int(s[b]

so in total:

if "Leo 1" in s:
  i = s.index("Leo 1")
  lenght = len("Leo 1")
  b = i + lenght + 1
  x = int(s[b])
Laurenz Z
  • 81
  • 7