Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python排序正则表达式_Python_Regex_Sorting - Fatal编程技术网

Python排序正则表达式

Python排序正则表达式,python,regex,sorting,Python,Regex,Sorting,嗨,制作一个文件,通过一个txt文件进行排序,选择名称:和前3个统计数据,并将它们存储在一个dict中,然后对下一个名称+3个统计数据执行相同的操作。如果dict不聪明,我想将它们存储在一个列表中也会起作用 txt文件如下所示: 我尝试了player=re.findall(r“[-+]?\d*\。\d+\d+”,player\u字符串) 但它只给了我球员的评分,我想我需要使用某种dict来存储所有不同的球员 如果这很复杂,你不必为我做所有的事情,只要给我指出正确的方向。 非常感谢。 我正在使用p

嗨,制作一个文件,通过一个txt文件进行排序,选择名称:和前3个统计数据,并将它们存储在一个dict中,然后对下一个名称+3个统计数据执行相同的操作。如果dict不聪明,我想将它们存储在一个列表中也会起作用

txt文件如下所示: 我尝试了
player=re.findall(r“[-+]?\d*\。\d+\d+”,player\u字符串)
但它只给了我球员的评分,我想我需要使用某种dict来存储所有不同的球员

如果这很复杂,你不必为我做所有的事情,只要给我指出正确的方向。 非常感谢。
我正在使用py2.6,我想您需要的是:

import re

player_string = "player a 34 45 56 player b 38 93 75 playerc 39 29 18 playerd 38 98"

pattern = re.compile(r"player\s+(\w+)\s+(\d+)\s+(\d+)\s+(\d+)")
matches = pattern.findall(player_string)
d = {}
for m in matches :
    print m
    d[m[0]] = m[1:]

print d
import re

player_string = "player a 34 45 56 player b 38 93 75 playerc 39 29 18 playerd 38 98"

pattern = re.compile(r"([\w ]*?)\s+(\d+)\s+(\d+)\s+(\d+)")
matches = pattern.findall(player_string)
d = {}
for m in matches :
    print m
    d[m[0].strip()] = m[1:]

print d

请注意,你写的“playerc”和“playerd”没有空格,这两个将找不到

我想你需要的是:

import re

player_string = "player a 34 45 56 player b 38 93 75 playerc 39 29 18 playerd 38 98"

pattern = re.compile(r"player\s+(\w+)\s+(\d+)\s+(\d+)\s+(\d+)")
matches = pattern.findall(player_string)
d = {}
for m in matches :
    print m
    d[m[0]] = m[1:]

print d
import re

player_string = "player a 34 45 56 player b 38 93 75 playerc 39 29 18 playerd 38 98"

pattern = re.compile(r"([\w ]*?)\s+(\d+)\s+(\d+)\s+(\d+)")
matches = pattern.findall(player_string)
d = {}
for m in matches :
    print m
    d[m[0].strip()] = m[1:]

print d

请注意,你写的“playerc”和“playerd”没有空格,这两个将找不到

我认为这可能会给你一些你想要的东西,尽管不使用正则表达式:

my_list = # list of players and stats from above

# build a list by splitting the original string at the word "player"
# and stripping extra white space
my_list_split = [item.strip() for item in my_list.split("player")]
这给出了一个类似于
['','a344556',…]
的列表,其中每个元素都应该包含不同玩家的信息。接下来,我们将元素拆分为一个字典,其中玩家名称是键,统计数据是值:

my_dict = {}  # initialize the dictionary
for entry in my_list_split:
  if entry is not "":  # possible you will have a blank string at the beginning
    entry_list = entry.split(" ")  # split entry at spaces
    my_dict[entry_list[0]] = entry_list[1:]  # first element is the name, remaining elements are the stats
你可以修改一些,只获得前两个或三个统计数据,或者改变统计数据的存储方式等等。您给出的列表上的结果
my_dict.items()

[('a', ['34', '45', '56']),
 ('c', ['39', '29', '18']),
 ('b', ['38', '93', '75']),
 ('d', ['38', '98'])]

我认为这可能会给你一些你正在寻找的东西,尽管不使用正则表达式:

my_list = # list of players and stats from above

# build a list by splitting the original string at the word "player"
# and stripping extra white space
my_list_split = [item.strip() for item in my_list.split("player")]
这给出了一个类似于
['','a344556',…]
的列表,其中每个元素都应该包含不同玩家的信息。接下来,我们将元素拆分为一个字典,其中玩家名称是键,统计数据是值:

my_dict = {}  # initialize the dictionary
for entry in my_list_split:
  if entry is not "":  # possible you will have a blank string at the beginning
    entry_list = entry.split(" ")  # split entry at spaces
    my_dict[entry_list[0]] = entry_list[1:]  # first element is the name, remaining elements are the stats
你可以修改一些,只获得前两个或三个统计数据,或者改变统计数据的存储方式等等。您给出的列表上的结果
my_dict.items()

[('a', ['34', '45', '56']),
 ('c', ['39', '29', '18']),
 ('b', ['38', '93', '75']),
 ('d', ['38', '98'])]

我想你需要的是:

import re

player_string = "player a 34 45 56 player b 38 93 75 playerc 39 29 18 playerd 38 98"

pattern = re.compile(r"player\s+(\w+)\s+(\d+)\s+(\d+)\s+(\d+)")
matches = pattern.findall(player_string)
d = {}
for m in matches :
    print m
    d[m[0]] = m[1:]

print d
import re

player_string = "player a 34 45 56 player b 38 93 75 playerc 39 29 18 playerd 38 98"

pattern = re.compile(r"([\w ]*?)\s+(\d+)\s+(\d+)\s+(\d+)")
matches = pattern.findall(player_string)
d = {}
for m in matches :
    print m
    d[m[0].strip()] = m[1:]

print d
在最后一个玩家“playerd”之后,你只有2个数字,而不是regex期望的3个

输出:

{'playerc': ('39', '29', '18'), 'player b': ('38', '93', '75'), 'player a': ('34', '45', '56')}

我想你需要的是:

import re

player_string = "player a 34 45 56 player b 38 93 75 playerc 39 29 18 playerd 38 98"

pattern = re.compile(r"player\s+(\w+)\s+(\d+)\s+(\d+)\s+(\d+)")
matches = pattern.findall(player_string)
d = {}
for m in matches :
    print m
    d[m[0]] = m[1:]

print d
import re

player_string = "player a 34 45 56 player b 38 93 75 playerc 39 29 18 playerd 38 98"

pattern = re.compile(r"([\w ]*?)\s+(\d+)\s+(\d+)\s+(\d+)")
matches = pattern.findall(player_string)
d = {}
for m in matches :
    print m
    d[m[0].strip()] = m[1:]

print d
在最后一个玩家“playerd”之后,你只有2个数字,而不是regex期望的3个

输出:

{'playerc': ('39', '29', '18'), 'player b': ('38', '93', '75'), 'player a': ('34', '45', '56')}


这个文件真的是由一个长行组成的吗?你期望的输出是什么?是的,它只是一个长行的文本,带有不合适的空格。期望的输出只是一个列表或命令,这样我就可以给玩家打电话,并显示与名字相关的3个统计数据。那么玩家a和玩家C呢?那是打字错误吗?也许你应该发布一个实际输入的简短示例。文件真的由一行长的文字组成吗?你期望的输出是什么?是的,只是一行长的文字,需要的输出只是一个列表或命令,这样我就可以给玩家打电话,并调出与名字相关的3个统计数据。那么玩家a和玩家C呢?那是打字错误吗?也许你应该发布一个简短的实际输入样本。是的,你是故意这样做的,因为我不能确定某些名字可能像迈克·琼斯、克拉克·肯特或马克斯鲍尔。这种变化正是我遇到如此困难的原因。@user3496483那么如果一个球员被命名为33 77 91怎么办?您正在生成此数据吗?如果你引用它,解析起来可能会更容易。所以你的意思是,在你的示例中,“player a”是玩家的全名,“playerc”也是玩家的全名。也就是说,“player”不仅仅是一个分隔符,实际名称是什么,对吗?在这种情况下,正则表达式必须被修复。@Mihai是的,很抱歉没有说得更清楚Mihaiye是故意这么做的,因为我不能确定某些名字可能像Mike Jones或Clark Kent或Maxpower。我对这种变化感到很不快。@user3496483那么如果一个球员的名字是33 77 91呢?您正在生成此数据吗?如果你引用它,解析起来可能会更容易。所以你的意思是,在你的示例中,“player a”是玩家的全名,“playerc”也是玩家的全名。也就是说,“player”不仅仅是一个分隔符,实际名称是什么,对吗?在这种情况下,正则表达式必须被修正。@Mihai是的,是的,很抱歉没有更清楚Mihai@Mihai是的,如果值不存在,我只希望它是零@Mihai是的,如果值不存在,我只希望它是零