英文:
Obtaining a list of number from a list of string
问题
从字符串列表或数组中检索每个字符串中的数字,并将这些数字存储在另一个列表中。
在这里,我想获取“values=”后面的数字,并将其存储在另一个数组中。
我是初学者,已经查看了类似的问题,但都没有解决。
如果需要,我可以提供数组。
[56616, 59501, 109562, 306290, 11854, 3433, 55551, 40241, 50313, 20733, 75596, 19958, 43132, 3558, 36972, 38991, 12793, 306360, 8320, 54925, 7756, 18249, 6375, 125554, 30524, 5077, 24678, 3179, 1714, 7797, 27459]
更新 1.
使用打印命令后,这是我的数组的样子。
英文:
I have a list or array of string, from which I have to retrieve a number from each string in list and store it in another list with these numbers only.
Here I want to obtain the number after "values=" and store in another array.
I am beginner and reviewed similar questions, but none of them worked.
I can provide the array if that helps you in any way.
[<Achievement name='Friend in Need' stars=3 value=56616>, <Achievement name='Friend in Need' stars=3 value=59501>, <Achievement name='Friend in Need' stars=3 value=109562>, <Achievement name='Friend in Need' stars=3 value=306290>, <Achievement name='Friend in Need' stars=2 value=11854>, <Achievement name='Friend in Need' stars=1 value=3433>, <Achievement name='Friend in Need' stars=3 value=55551>, <Achievement name='Friend in Need' stars=3 value=40241>, <Achievement name='Friend in Need' stars=3 value=50313>, <Achievement name='Friend in Need' stars=2 value=20733>, <Achievement name='Friend in Need' stars=3 value=75596>, <Achievement name='Friend in Need' stars=2 value=19958>, <Achievement name='Friend in Need' stars=3 value=43132>, <Achievement name='Friend in Need' stars=1 value=3558>, <Achievement name='Friend in Need' stars=3 value=36972>, <Achievement name='Friend in Need' stars=3 value=38991>, <Achievement name='Friend in Need' stars=2 value=12793>, <Achievement name='Friend in Need' stars=3 value=306360>, <Achievement name='Friend in Need' stars=2 value=8320>, <Achievement name='Friend in Need' stars=3 value=54925>, <Achievement name='Friend in Need' stars=2 value=7756>, <Achievement name='Friend in Need' stars=2 value=18249>, <Achievement name='Friend in Need' stars=2 value=6375>, <Achievement name='Friend in Need' stars=3 value=125554>, <Achievement name='Friend in Need' stars=3 value=30524>, <Achievement name='Friend in Need' stars=2 value=5077>, <Achievement name='Friend in Need' stars=2 value=24678>, <Achievement name='Friend in Need' stars=1 value=3179>, <Achievement name='Friend in Need' stars=1 value=1714>, <Achievement name='Friend in Need' stars=2 value=7797>, <Achievement name='Friend in Need' stars=3 value=27459>]
Update 1.
this is what my array looks after using print command
答案1
得分: 1
[ 56616, 59501, 109562, 306290, 11854, 3433, 55551, 40241, 50313, 20733, 75596, 19958, 43132, 3558, 36972, 38991, 12793, 306360, 8320, 54925, 7756, 18249, 6375, 125554, 30524, 5077, 24678, 3179, 1714, 7797, 27459 ]
英文:
Your input is not a valid python. You need to add quotes.
lst = ["<Achievement name='Friend in Need' stars=3 value=56616>", "<Achievement name='Friend in Need' stars=3 value=59501>", "<Achievement name='Friend in Need' stars=3 value=109562>", "<Achievement name='Friend in Need' stars=3 value=306290>", "<Achievement name='Friend in Need' stars=2 value=11854>", "<Achievement name='Friend in Need' stars=1 value=3433>", "<Achievement name='Friend in Need' stars=3 value=55551>", "<Achievement name='Friend in Need' stars=3 value=40241>", "<Achievement name='Friend in Need' stars=3 value=50313>", "<Achievement name='Friend in Need' stars=2 value=20733>", "<Achievement name='Friend in Need' stars=3 value=75596>", "<Achievement name='Friend in Need' stars=2 value=19958>", "<Achievement name='Friend in Need' stars=3 value=43132>", "<Achievement name='Friend in Need' stars=1 value=3558>", "<Achievement name='Friend in Need' stars=3 value=36972>", "<Achievement name='Friend in Need' stars=3 value=38991>", "<Achievement name='Friend in Need' stars=2 value=12793>", "<Achievement name='Friend in Need' stars=3 value=306360>", "<Achievement name='Friend in Need' stars=2 value=8320>", "<Achievement name='Friend in Need' stars=3 value=54925>", "<Achievement name='Friend in Need' stars=2 value=7756>", "<Achievement name='Friend in Need' stars=2 value=18249>", "<Achievement name='Friend in Need' stars=2 value=6375>", "<Achievement name='Friend in Need' stars=3 value=125554>", "<Achievement name='Friend in Need' stars=3 value=30524>", "<Achievement name='Friend in Need' stars=2 value=5077>", "<Achievement name='Friend in Need' stars=2 value=24678>", "<Achievement name='Friend in Need' stars=1 value=3179>", "<Achievement name='Friend in Need' stars=1 value=1714>", "<Achievement name='Friend in Need' stars=2 value=7797>", "<Achievement name='Friend in Need' stars=3 value=27459>"]
print(lst)
["<Achievement name='Friend in Need' stars=3 value=56616>",
"<Achievement name='Friend in Need' stars=3 value=59501>",
"<Achievement name='Friend in Need' stars=3 value=109562>",
"<Achievement name='Friend in Need' stars=3 value=306290>",
"<Achievement name='Friend in Need' stars=2 value=11854>",
"<Achievement name='Friend in Need' stars=1 value=3433>",
"<Achievement name='Friend in Need' stars=3 value=55551>",
"<Achievement name='Friend in Need' stars=3 value=40241>",
"<Achievement name='Friend in Need' stars=3 value=50313>",
"<Achievement name='Friend in Need' stars=2 value=20733>",
"<Achievement name='Friend in Need' stars=3 value=75596>",
"<Achievement name='Friend in Need' stars=2 value=19958>",
"<Achievement name='Friend in Need' stars=3 value=43132>",
"<Achievement name='Friend in Need' stars=1 value=3558>",
"<Achievement name='Friend in Need' stars=3 value=36972>",
"<Achievement name='Friend in Need' stars=3 value=38991>",
"<Achievement name='Friend in Need' stars=2 value=12793>",
"<Achievement name='Friend in Need' stars=3 value=306360>",
"<Achievement name='Friend in Need' stars=2 value=8320>",
"<Achievement name='Friend in Need' stars=3 value=54925>",
"<Achievement name='Friend in Need' stars=2 value=7756>",
"<Achievement name='Friend in Need' stars=2 value=18249>",
"<Achievement name='Friend in Need' stars=2 value=6375>",
"<Achievement name='Friend in Need' stars=3 value=125554>",
"<Achievement name='Friend in Need' stars=3 value=30524>",
"<Achievement name='Friend in Need' stars=2 value=5077>",
"<Achievement name='Friend in Need' stars=2 value=24678>",
"<Achievement name='Friend in Need' stars=1 value=3179>",
"<Achievement name='Friend in Need' stars=1 value=1714>",
"<Achievement name='Friend in Need' stars=2 value=7797>",
"<Achievement name='Friend in Need' stars=3 value=27459>"]
Split each item of your lst
using split()
in a list comprehension and convert to int
[int(x.split('value=')[-1][:-1]) for x in lst]
Output
[ 56616, 59501, 109562, 306290, 11854,
3433, 55551, 40241, 50313, 20733,
75596, 19958, 43132, 3558, 36972,
38991, 12793, 306360, 8320, 54925,
7756, 18249, 6375, 125554, 30524,
5077, 24678, 3179, 1714, 7797,
27459 ]
答案2
得分: 0
"Presuming your strings look like:
```python
"<Achievement name='Friend in Need' stars=3 value=56616>"
You need only use a regular expression and a list comprehension to extract the desired value.
import re
r = re.compile(r"(?<=\bvalue=)\d+\b", re.X)
lst = ["<Achievement name='Friend in Need' stars=3 value=56616>"]
[
int(m[0])
for x in lst
for m in (r.search(x),)
if m
]
[56616]
If you need these as ints, then the conversion can either happen in the list comprehension, or later."
<details>
<summary>英文:</summary>
Presuming your strings look like:
"<Achievement name='Friend in Need' stars=3 value=56616>"
You need only use a regular expression and a list comprehension to extract the desired value.
>>> import re
>>> r = re.compile(r"(?<= \b value= ) \d+ \b", re.X)
>>> lst = ["<Achievement name='Friend in Need' stars=3 value=56616>"]
>>> [
... m[0]
... for x in lst
... for m in (r.search(x),)
... if m
... ]
['56616']
If you need these _as_ ints, then the conversion can either happen in the list comprehension, or later.
</details>
# 答案3
**得分**: 0
你可以使用HTMLParser,将`list`的每个项都视为HTML起始标记来处理。这将允许您读取任何属性。即使我们只是将您的整个原始打印输出包裹在引号中,这种方法仍然有效。
```python3
from html.parser import HTMLParser
L = "[<Achievement name='Friend in Need' stars=3 value=56616>, <Achievement name='Friend in Need' stars=3 value=59501>, <Achievement name='Friend in Need' stars=3 value=109562>, <Achievement name='Friend in Need' stars=3 value=306290>, <Achievement name='Friend in Need' stars=2 value=11854>, <Achievement name='Friend in Need' stars=1 value=3433>, <Achievement name='Friend in Need' stars=3 value=55551>, <Achievement name='Friend in Need' stars=3 value=40241>, <Achievement name='Friend in Need' stars=3 value=50313>, <Achievement name='Friend in Need' stars=2 value=20733>, <Achievement name='Friend in Need' stars=3 value=75596>, <Achievement name='Friend in Need' stars=2 value=19958>, <Achievement name='Friend in Need' stars=3 value=43132>, <Achievement name='Friend in Need' stars=1 value=3558>, <Achievement name='Friend in Need' stars=3 value=36972>, <Achievement name='Friend in Need' stars=3 value=38991>, <Achievement name='Friend in Need' stars=2 value=12793>, <Achievement name='Friend in Need' stars=3 value=306360>, <Achievement name='Friend in Need' stars=2 value=8320>, <Achievement name='Friend in Need' stars=3 value=54925>, <Achievement name='Friend in Need' stars=2 value=7756>, <Achievement name='Friend in Need' stars=2 value=18249>, <Achievement name='Friend in Need' stars=2 value=6375>, <Achievement name='Friend in Need' stars=3 value=125554>, <Achievement name='Friend in Need' stars=3 value=30524>, <Achievement name='Friend in Need' stars=2 value=5077>, <Achievement name='Friend in Need' stars=2 value=24678>, <Achievement name='Friend in Need' stars=1 value=3179>, <Achievement name='Friend in Need' stars=1 value=1714>, <Achievement name='Friend in Need' stars=2 value=7797>, <Achievement name='Friend in Need' stars=3 value=27459>]"
class Achievements(HTMLParser):
def handle_starttag(self, tag, attrs):
self.__acv.append(dict(attrs))
def feed(self, data):
self.__acv = []
super().feed(''.join(data))
return self
# 找到每个实例中的“value”
def __getattr__(self, key):
found = []
for a in self.__acv:
if (v := a.get(key, None)) is None:
continue
v = (str, int)[v.isdigit()](v)
found.append(v)
return found
a = Achievements().feed(L)
print(a.value)
输出:
[56616, 59501, 109562, 306290, 11854, 3433, 55551, 40241, 50313, 20733, 75596, 19958, 43132, 3558, 36972, 38991, 12793, 306360,
8320, 54925, 7756, 18249, 6375, 125554, 30524, 5077, 24678, 3179, 1714, 7797, 27459]
由于您的更新,看起来更像是您有一个充满具有自定义__repr__
的类的列表。如果是这样的话,您可以使用以下方法之一获取您的变量:
values = [cls.value for cls in YourList]
values = [cls['value'] for cls in YourList]
英文:
You could use HTMLParser and treat every item of the list
like an html start tag. This would allow you to read any attribute. Even if we do nothing but wrap your entire original printout in quotes, this way still works.
from html.parser import HTMLParser
L = "[<Achievement name='Friend in Need' stars=3 value=56616>, <Achievement name='Friend in Need' stars=3 value=59501>, <Achievement name='Friend in Need' stars=3 value=109562>, <Achievement name='Friend in Need' stars=3 value=306290>, <Achievement name='Friend in Need' stars=2 value=11854>, <Achievement name='Friend in Need' stars=1 value=3433>, <Achievement name='Friend in Need' stars=3 value=55551>, <Achievement name='Friend in Need' stars=3 value=40241>, <Achievement name='Friend in Need' stars=3 value=50313>, <Achievement name='Friend in Need' stars=2 value=20733>, <Achievement name='Friend in Need' stars=3 value=75596>, <Achievement name='Friend in Need' stars=2 value=19958>, <Achievement name='Friend in Need' stars=3 value=43132>, <Achievement name='Friend in Need' stars=1 value=3558>, <Achievement name='Friend in Need' stars=3 value=36972>, <Achievement name='Friend in Need' stars=3 value=38991>, <Achievement name='Friend in Need' stars=2 value=12793>, <Achievement name='Friend in Need' stars=3 value=306360>, <Achievement name='Friend in Need' stars=2 value=8320>, <Achievement name='Friend in Need' stars=3 value=54925>, <Achievement name='Friend in Need' stars=2 value=7756>, <Achievement name='Friend in Need' stars=2 value=18249>, <Achievement name='Friend in Need' stars=2 value=6375>, <Achievement name='Friend in Need' stars=3 value=125554>, <Achievement name='Friend in Need' stars=3 value=30524>, <Achievement name='Friend in Need' stars=2 value=5077>, <Achievement name='Friend in Need' stars=2 value=24678>, <Achievement name='Friend in Need' stars=1 value=3179>, <Achievement name='Friend in Need' stars=1 value=1714>, <Achievement name='Friend in Need' stars=2 value=7797>, <Achievement name='Friend in Need' stars=3 value=27459>]"
class Achievements(HTMLParser):
def handle_starttag(self, tag, attrs):
self.__acv.append(dict(attrs))
def feed(self, data):
self.__acv = []
super().feed(''.join(data))
return self
#find every instance of "key"
def __getattr__(self, key):
found = []
for a in self.__acv:
if (v := a.get(key, None)) is None:
continue
v = (str,int)[v.isdigit()](v)
found.append(v)
return found
a = Achievements().feed(L)
print(a.value)
output:
[56616, 59501, 109562, 306290, 11854, 3433, 55551, 40241, 50313, 20733, 75596, 19958, 43132, 3558, 36972, 38991, 12793, 306360,
8320, 54925, 7756, 18249, 6375, 125554, 30524, 5077, 24678, 3179, 1714, 7797, 27459]
Due to your update, it looks more like you have a list full of classes that have a custom __repr__
. If so, you could probably get your variables one of these ways:
values = [cls.value for cls in YourList]
values = [cls['value'] for cls in YourList]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论