我一直在编写一些代码,包括将随机字符插入列表,然后删除这些字符。想象一下,你有一个字符串:
['x', 'x', 'x', 'x', 'x', 'x', 'x', 'x']
以此为输入,您可以将其作为输出:
['x', 'x', 'a', 'r', 'x', 'x', 't', 'q', 'x', 'x' 'j', 'z', 'x', 'x']
我已编写此代码。我试过这个删除那些随机字符:
iterations = 0
removal = 0
for s in my_list:
if iterations % 3 == 0:
removal = 0
for letters in range(2):
del my_list[removal + iterations]
removal += 1
iterations += 1
这只是删除了其中一些,我无法弄清楚这种模式。这个想法是,这可以通过列表每八次迭代取出五个字符。或者每任何数量的迭代都有任意数量的字符。
答案 0 :(得分:1)
如果您可以在订单模式中插入字符,那么您应该能够以相同的顺序删除这些字符。
好吧,假设插入字符2次并且插入的位置是3。 然后你需要先删除每个第4个元素
>>>for i in range(3, len(my_list), 3):
my_list.pop(i) # I have a problem with my python, so I could only use pop() to remove
['x', 'x', 'a', 'x', 'x', 't', 'x', 'x' 'j', 'x', 'x']
然后你需要删除每个第3个元素
>>>for i in range(2, len(my_list), 2):
my_list.pop(i)
['x', 'x', 'x', 'x', 'x', 'x', 'x', 'x']
答案 1 :(得分:0)
正如其中一位评论者指出的那样,根据索引随机开始从列表中删除项目并不容易。我要解决这个问题的方法是在你要删除的索引中存储一些随机值,而不是通过检查是否为该值将数组过滤到一个新数组中。
iterations = 0
removal = 0
placeholder = "TO_DELETE"
for s in my_list:
if iterations % 3 == 0:
removal = 0
for letters in range(2):
my_list[removal + iterations] = placeholder
removal += 1
iterations += 1
my_list = [i for i in my_list if i != placeholder]
答案 2 :(得分:0)
您在代码中遇到的问题是在迭代时修改列表。
这是使用itertools
中的from itertools import cycle
def stride(iterable, take, leave):
pattern = cycle([True] * take + [False] * leave)
for value, take in zip(iterable, pattern):
if take:
yield value
的替代方法:
x = ['x', 'x', 'a', 'r', 'x', 'x', 't', 'q', 'x', 'x', 'j', 'z', 'x', 'x']
如果>>> list(stride(x, 2, 2))
['x', 'x', 'x', 'x', 'x', 'x', 'x', 'x']
,那么:
take
如果leave
或pattern
很大,则会通过制作take + leave
大小为def take_or_leave(take, leave):
while True:
for _ in range(take):
yield True
for _ in range(leave):
yield False
def stride(iterable, take, leave):
pattern = take_or_leave(take, leave)
for value, take in zip(iterable, pattern):
if take:
yield value
的列表来浪费内存。所以我们可以做一个生成器辅助函数:
$db_hostname = "localhost";
$db_username = "username";
$db_password = "password";
$db_database = "database";
$db_server = mysql_connect($db_hostname, $db_username, $db_password);
if (!$db_server) die("Unable to connect to MySQL: " . mysql_error());
mysql_select_db($db_database)
or die("Unable to select database: " . mysql_error());
$query = "SELECT * FROM company WHERE company = '1 800 FLOWERS COM INC' AND date = '2015-08-06'";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
$file = "ftp://ftp.sec.gov/" . $row[4];
$text = file_get_contents($file);
if($text === false){
echo "error downloading file $row[4]\n";
continue;
}
$tarray = explode('<SEQUENCE>', $text);
for($i = 1; $i < count($tarray); $i++){
$a = strstr($tarray[$i], '<HTML>');
if($a == false)continue; //means that there is no html document in this sequence
$html = strstr($a, '</HTML>', true);
$html.="</HTML>";
$running = $running . $html;
}
$temp = "cache.htm";
file_put_contents($temp, $running);
$name = $row[0] . "-" . $row[3] . ".pdf";
$name = str_replace(' ', '_', $name);
//$content = file_get_contents($row[2] . "-" . $row[1] . ".htm");
exec("D://wkhtmltopdf/bin/wkhtmltopdf.exe $temp $name");
unlink($temp);
//echo($row[0] . " created");
答案 3 :(得分:-2)
我喜欢用不稳定的单行功能来接近这些类型的东西。这就是我所拥有的:
removeStuff = lambda myArray, delEvery: [myArray[x] for x in range(len(myArray)) if x%delEvery!=0]
用法:
>>> removeStuff([1,2,3,4,5,6,7], 3)
[2, 3, 5, 6]