替换和移动列表列表中的值

时间:2018-04-11 00:30:32

标签: python

我有以下列表清单

[
    ['90 min', 'Drama', 'Horror', 'Thriller', 'User Rating: 8.2/10 (28,588 user ratings)', '82', 'Metascore', 'Rank:', '2', 'Showtimes:', 'Studio Movie Grill - Downey', '11:15 am', '12:05 pm', '1:40', '2:30', '4:15', '6:40', '7:30', '9:05', '10:15'],
    ['90 min', 'Drama', 'Horror', 'Thriller', 'User Rating: 8.2/10 (28,588 user ratings)', '82', 'Metascore', 'Rank:', '2', 'Showtimes:', 'AMC Norwalk 20', '11:45 am', '12:45 pm', '2:15', '3:15', '4:45', '5:45', '7:15', '8:15', '9:45', '10:45'],
    ['90 min', 'Drama', 'Horror', 'Thriller', 'User Rating: 8.2/10 (28,588 user ratings)', '82', 'Metascore', 'Rank:', '2', 'Showtimes:', 'Cinemark Downey and XD', '10:00 am', '11:15', '12:30 pm', '1:45', '3:00', '4:15', '5:30', '6:30', '6:45', '8:00', '8:45', '9:15', '10:00', '10:30', '11:15', '11:45'],
    ['90 min', 'Drama', 'Horror', 'Thriller', 'User Rating: 8.2/10 (28,588 user ratings)', '82', 'Metascore', 'Rank:', '2', 'Showtimes:', 'AMC Stonebriar 24', '11:40 am', '12:30 pm', '2:00', '3:00', '4:40', '5:30', '7:20', '8:00', '10:00', '10:30'],
    ['90 min', 'Drama', 'Horror', 'Thriller', 'User Rating: 8.2/10 (28,588 user ratings)', '82', 'Metascore', 'Rank:', '2', 'Showtimes:', 'AMC Orange 30 (AMC Block 30)', '11:00 am', '12:15 pm', '1:30', '2:45', '4:00', '5:15', '6:30', '7:45', '9:00', '10:15']
]

我想做两件事

1)如果用户评分('用户评分:8.2 / 10(28,588用户评分)')> = 6.5,我想通过添加 标记列表中的现有值。 2)其次,我希望在"放映时间之后移动价值:"即列表清单中的第10个元素,并将其置于" 0"

我不知道如何做到这一点,我是python的新手。如果有人能给我起点,我会很感激。

解决方案

借助Ajax1234提供的解决方案;我能够写出我想要的东西。这可能不是最佳解决方案,但可行。我从Ajax1234解决方案中添加的唯一改进是" User Rating"和"放映时间"它们不是硬编码的,它们可以在列表中的任何位置。

import re
s = [['86 min', 'Animation', 'Adventure', 'Comedy', 'Family', 'Fantasy', 'Mystery', 'Romance', 'User Rating: 4.7/10 (1,287 user ratings)', '36', 'Metascore', 'Rank:', '75', 'Showtimes:', 'Studio Movie Grill - Downey', '11:00 am', '1:15 pm', '3:30', '5:45', '7:55'], ['86 min', 'Animation', 'Adventure', 'Comedy', 'Family', 'Fantasy', 'Mystery', 'Romance', 'User Rating: 4.7/10 (1,287 user ratings)', '36', 'Metascore', 'Rank:', '75', 'Showtimes:', 'AMC Norwalk 20', '11:35 am', '2:00 pm', '4:30', '7:00', '9:25']]


x1=''
a1=''
def getvalues(x,a):
    re_rating = re.compile('^User Rating: (\d(\.\d+)?)')
    for i, j in enumerate(s):
        for x,y in enumerate(j):
            if y == "Showtimes:":
                x1=x
        for a,b in enumerate(j):
            if re.match(re_rating, b):
                a1=a
    return x1,a1


x,a = getvalues(x1,a1)
print(x,a)
def fix_data(data,x,a):
    tags = ['<b>', '</b>']
    return [[i[x+1]]+i[:a]+[re.sub('(?<=User Rating: )[\d\.]+', '{}', i[a]).format((lambda x:tags[0]+x+tags[-1] if float(x) >= 6.5 else x)(re.findall('(?<=User Rating: )[\d\.]+', i[a])[0]))]+i[a+1:x+1]+i[x+2:] for i in data]

print(fix_data(s,x,a))

1 个答案:

答案 0 :(得分:1)

您可以使用正则表达式并使用函数指定应使用哪种类型的“标记”来包围特定值:

import re
s = [['90 min', 'Drama', 'Horror', 'Thriller', 'User Rating: 8.2/10 (28,588 user ratings)', '82', 'Metascore', 'Rank:', '2', 'Showtimes:', 'Studio Movie Grill - Downey', '11:15 am', '12:05 pm', '1:40', '2:30', '4:15', '6:40', '7:30', '9:05', '10:15'], ['90 min', 'Drama', 'Horror', 'Thriller', 'User Rating: 8.2/10 (28,588 user ratings)', '82', 'Metascore', 'Rank:', '2', 'Showtimes:', 'AMC Norwalk 20', '11:45 am', '12:45 pm', '2:15', '3:15', '4:45', '5:45', '7:15', '8:15', '9:45', '10:45'],['90 min', 'Drama', 'Horror', 'Thriller', 'User Rating: 8.2/10 (28,588 user ratings)', '82', 'Metascore', 'Rank:', '2', 'Showtimes:', 'Cinemark Downey and XD', '10:00 am', '11:15', '12:30 pm', '1:45', '3:00', '4:15', '5:30', '6:30', '6:45', '8:00', '8:45', '9:15', '10:00', '10:30', '11:15', '11:45'],['90 min', 'Drama', 'Horror', 'Thriller', 'User Rating: 8.2/10 (28,588 user ratings)', '82', 'Metascore', 'Rank:', '2', 'Showtimes:', 'AMC Stonebriar 24', '11:40 am', '12:30 pm', '2:00', '3:00', '4:40', '5:30', '7:20', '8:00', '10:00', '10:30'], ['90 min', 'Drama', 'Horror', 'Thriller', 'User Rating: 8.2/10 (28,588 user ratings)', '82', 'Metascore', 'Rank:', '2', 'Showtimes:', 'AMC Orange 30 (AMC Block 30)', '11:00 am', '12:15 pm', '1:30', '2:45', '4:00', '5:15', '6:30', '7:45', '9:00', '10:15']]
def fix_data(data, tags = ['[', ']']):
  return [[i[10]]+i[:4]+[re.sub('(?<=User Rating: )[\d\.]+', '{}', i[4]).format((lambda x:tags[0]+x+tags[-1] if float(x) >= 6.5 else x)(re.findall('(?<=User Rating: )[\d\.]+', i[4])[0]))]+i[5:10]+i[11:] for i in data]

print(fix_data(s, tags = ['<b>', '</b>']))

输出:

[['Studio Movie Grill - Downey', '90 min', 'Drama', 'Horror', 'Thriller', 'User Rating: <b>8.2</b>/10 (28,588 user ratings)', '82', 'Metascore', 'Rank:', '2', 'Showtimes:', '11:15 am', '12:05 pm', '1:40', '2:30', '4:15', '6:40', '7:30', '9:05', '10:15'], ['AMC Norwalk 20', '90 min', 'Drama', 'Horror', 'Thriller', 'User Rating: <b>8.2</b>/10 (28,588 user ratings)', '82', 'Metascore', 'Rank:', '2', 'Showtimes:', '11:45 am', '12:45 pm', '2:15', '3:15', '4:45', '5:45', '7:15', '8:15', '9:45', '10:45'], ['Cinemark Downey and XD', '90 min', 'Drama', 'Horror', 'Thriller', 'User Rating: <b>8.2</b>/10 (28,588 user ratings)', '82', 'Metascore', 'Rank:', '2', 'Showtimes:', '10:00 am', '11:15', '12:30 pm', '1:45', '3:00', '4:15', '5:30', '6:30', '6:45', '8:00', '8:45', '9:15', '10:00', '10:30', '11:15', '11:45'], ['AMC Stonebriar 24', '90 min', 'Drama', 'Horror', 'Thriller', 'User Rating: <b>8.2</b>/10 (28,588 user ratings)', '82', 'Metascore', 'Rank:', '2', 'Showtimes:', '11:40 am', '12:30 pm', '2:00', '3:00', '4:40', '5:30', '7:20', '8:00', '10:00', '10:30'], ['AMC Orange 30 (AMC Block 30)', '90 min', 'Drama', 'Horror', 'Thriller', 'User Rating: <b>8.2</b>/10 (28,588 user ratings)', '82', 'Metascore', 'Rank:', '2', 'Showtimes:', '11:00 am', '12:15 pm', '1:30', '2:45', '4:00', '5:15', '6:30', '7:45', '9:00', '10:15']]