从字符串列表中获取数字列表

huangapple go评论66阅读模式
英文:

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 = &quot;[&lt;Achievement name=&#39;Friend in Need&#39; stars=3 value=56616&gt;, &lt;Achievement name=&#39;Friend in Need&#39; stars=3 value=59501&gt;, &lt;Achievement name=&#39;Friend in Need&#39; stars=3 value=109562&gt;, &lt;Achievement name=&#39;Friend in Need&#39; stars=3 value=306290&gt;, &lt;Achievement name=&#39;Friend in Need&#39; stars=2 value=11854&gt;, &lt;Achievement name=&#39;Friend in Need&#39; stars=1 value=3433&gt;, &lt;Achievement name=&#39;Friend in Need&#39; stars=3 value=55551&gt;, &lt;Achievement name=&#39;Friend in Need&#39; stars=3 value=40241&gt;, &lt;Achievement name=&#39;Friend in Need&#39; stars=3 value=50313&gt;, &lt;Achievement name=&#39;Friend in Need&#39; stars=2 value=20733&gt;, &lt;Achievement name=&#39;Friend in Need&#39; stars=3 value=75596&gt;, &lt;Achievement name=&#39;Friend in Need&#39; stars=2 value=19958&gt;, &lt;Achievement name=&#39;Friend in Need&#39; stars=3 value=43132&gt;, &lt;Achievement name=&#39;Friend in Need&#39; stars=1 value=3558&gt;, &lt;Achievement name=&#39;Friend in Need&#39; stars=3 value=36972&gt;, &lt;Achievement name=&#39;Friend in Need&#39; stars=3 value=38991&gt;, &lt;Achievement name=&#39;Friend in Need&#39; stars=2 value=12793&gt;, &lt;Achievement name=&#39;Friend in Need&#39; stars=3 value=306360&gt;, &lt;Achievement name=&#39;Friend in Need&#39; stars=2 value=8320&gt;, &lt;Achievement name=&#39;Friend in Need&#39; stars=3 value=54925&gt;, &lt;Achievement name=&#39;Friend in Need&#39; stars=2 value=7756&gt;, &lt;Achievement name=&#39;Friend in Need&#39; stars=2 value=18249&gt;, &lt;Achievement name=&#39;Friend in Need&#39; stars=2 value=6375&gt;, &lt;Achievement name=&#39;Friend in Need&#39; stars=3 value=125554&gt;, &lt;Achievement name=&#39;Friend in Need&#39; stars=3 value=30524&gt;, &lt;Achievement name=&#39;Friend in Need&#39; stars=2 value=5077&gt;, &lt;Achievement name=&#39;Friend in Need&#39; stars=2 value=24678&gt;, &lt;Achievement name=&#39;Friend in Need&#39; stars=1 value=3179&gt;, &lt;Achievement name=&#39;Friend in Need&#39; stars=1 value=1714&gt;, &lt;Achievement name=&#39;Friend in Need&#39; stars=2 value=7797&gt;, &lt;Achievement name=&#39;Friend in Need&#39; stars=3 value=27459&gt;]&quot;


class Achievements(HTMLParser):
    def handle_starttag(self, tag, attrs):
        self.__acv.append(dict(attrs))
            
    def feed(self, data):
        self.__acv = []
        super().feed(&#39;&#39;.join(data))
        return self
        
    #find every instance of &quot;key&quot;
    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[&#39;value&#39;] for cls in YourList]

huangapple
  • 本文由 发表于 2023年7月20日 08:52:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76726027.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定