我有一个字符串值数组,需要遍历它们,以随机替换每个字符串中5%的元素,如果它们为1,则将其翻转为0,如果它们为0,则将其翻转为1。
我有一个字符串数组,看起来像这样:
['10011000000100000000011101100010001000110111101100100101100000111010000110011111001101001110100111110110000110001001010001010001110000000000000111110000111100010011101001011001111111011010001001100100110110000001000001010100111111110010011001100001001100011001111010010011000101000101111001100000110011101100000010110111011000010001111011010000111010100001101011000110111000000010000000111100010100100110101101111011001010000001110010110100011110000010001101110001101000100011001110101000100011111010',
'11010000000110110011011110111010011011111010000101101101111010101000100000001010011100011011101111001000100000011110000011001100101011100111111001001101111110001101001100010111000100100010010111001110010110010101010110100000110011011110100110010011110000101001111111001001001101011000111001101101011000111101010010000011001001011110011010101111110001010100001011000001011110001011100100010011001101111100001111101000000010001010001100001010000010000000000001010101001110110111000010010001001001010101',
'10011000000100000000011101100010001000110111101100100101100000111010000110011111001101001110100111110110000110001001010001010001110000000000000111110000111100010011101001011001111111011010001001000001011010100011000101000001100101000101010000001100111100101000011010000001011000000000000000011010100111100111010001111010000101100101010000110011111011111110100011111000001110111111001011011111101011110100000011101101101110010101001010100110111010000111000000111000110010110110001101111010011110000111',
'11010000000110110011011110111010011011111010000101101101111010101000100000001010011100011011101111001000100000011110000011001100101011100111111001001101111110001101001100010111000100100010010111001110010110010101010110100000110011011110100110010011110000101001111111001001001101011000111001101101011000111101010010011100101111001010010000010010101101001000001111010110000111110100100001101101111011110101001000001101101100110110001110011000010000110110011100100001001101011010101100010011110111000000']
有效地,字符串中5%的值将从0更改为1,反之亦然。
答案 0 :(得分:1)
尝试以下循环:
for idx,i in enumerate(l):
y=list(i)
for x in random.sample(range(len(i)),(len(i)*5)//100):
y[x]=str(abs(int(y[x])-1))
l[idx]=''.join(y)
从一翻转到零,反之亦然,只有5%。
答案 1 :(得分:0)
使用random.choices获取5%的索引
import random
[[i for i in random.choices(range(len(arr[j])), k=int(len(arr[j]) * 0.05))] for j in range(len(arr))]
答案 2 :(得分:0)
为了有效地生成新的字符串数组,必须避免在每次修改的更改时使字符串发生变异。因此,我提供了我的解决方案( message:{},
recipient: {},
sender: {},
timestamp: 1231234123
,如果需要,可以对其进行优化。
<?php
include('connect.php');
// first check URL
if (isset($_GET['amount'])) {
$perpage = $_GET['amount'];
}
// Now check form
if (isset($_POST['amount'])) {
$perpage = $_POST['amount'];
}
// if nothing set, default:
if(!isset($perpage)) {
$perpage = 50;
}
if(isset($_GET['page']) && !empty($_GET['page'])){ // code to get the amount of records for HTML paginated pages
$currentPage = $_GET['page'];
} else {
$currentPage = 1;
}
// Pagination code and query
$startFrom = ($currentPage * $perpage) - $perpage;
$totalEmpSQL = "SELECT * FROM `tblusers` WHERE Id NOT IN (1,2,3)";
$allEmpResult = mysqli_query($conn, $totalEmpSQL);
$totalEmployee = mysqli_num_rows($allEmpResult);
$lastPage = ceil($totalEmployee/$perpage);
$firstPage = 1;
$nextPage = $currentPage + 1;
$previousPage = $currentPage - 1;
$empSQL = "SELECT Id, FirstName, LastName, Cell, Email, Address, Region
FROM `tblusers`
WHERE Id NOT IN (1,2,3)
ORDER BY LastName
LIMIT $startFrom, $perpage";
$empResult = mysqli_query($conn, $empSQL);
?>
<!-- HTML Form -->
<section class="panel">
<header class="panel-heading">
<div class="input-group">
<!-- JavaScript Filter Input -->
<input type="text" class="form-control" id="search" onkeyup="myFunction()"
placeholder="Soek lede..">
<div class="input-group-btn">
<button class="btn btn-default" type="submit">
<i class="glyphicon glyphicon-search"></i>
</button>
</div>
</div>
</header>
<div class="panel-body">
<div class="row datatables-header form-inline">
<div class="col-sm-4 col-md-4">
<!-- THE FORM THAT IS SUPPOSED TO UPDATE THE
VARIABLE FOR AMOUNT QUERY -->
<form method="post" action="<?php echo
htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<select id="amount" class="form-control"
onchange="this.form.submit()">
<option value="10">10</option>
<option value="25">25</option>
<option value="50">50</option>
<option value="100">100</option>
<option value="500">500</option>
</select>
</form>
</div>
<div class="col-sm-4 col-md-4">
<nav aria-label="Page navigation">
<ul class="pagination">
<?php if($currentPage != $firstPage) { ?>
<li class="page-item">
<a class="page-link" href="?page=<?php echo
$firstPage ?>" tabindex="-1" aria-label="Vorige">
<span aria-hidden="true">Eerste</span>
</a>
</li>
<?php } ?>
<?php if($currentPage >= 2) { ?>
<li class="page-item"><a class="page-link" href="?
page=<?php echo $previousPage ?>"><?php echo $previousPage ?></a></li>
<?php } ?>
<li class="page-item active"><a class="page-link"
href="?page=<?php echo $currentPage ?>"><?php echo $currentPage ?></a>
</li>
<?php if($currentPage != $lastPage) { ?>
<li class="page-item"><a class="page-link" href="?page=<?= $nextPage ?>&amount=<?= $perpage ?>"><?= $nextPage ?></a></li>
<li class="page-item">
<a class="page-link" href="?page=<?php echo
$lastPage ?>" aria-label="Volgende">
<span aria-hidden="true">Laaste</span>
</a>
</li>
<?php } ?>
</ul>
</nav>
</div>
<div class="col-sm-4 col-md-4">
<a class="btn btn-primary pull-right" style="margin:20px;"
href="index.php?page=new">Laai nuwe lid</a>
</div>
</div>
<!-- THE DYNAMIC TABLE -->
<div class="table-responsive">
<table id="myTable" class="table table-hover table-striped
table-condensed mb-none">
<?php
echo "<thead><th>Van:</th><th>Naam:</th><th>Selfoon:
</th><th>E-pos:</th><th>Adres:</th><th>Streek:</th><th>Aksie:</th>
</thead>
<tbody>";
while($emp = mysqli_fetch_assoc($empResult)){
?>
<tr>
<?php echo "<tr><td>". $emp["LastName"] . "
</td><td>". $emp["FirstName"] . "</td><td>". $emp["Cell"] . "</td><td>".
$emp["Email"] . "</td><td>". $emp["Address"] . "</td><td>". $emp["Region"]
.
"</td><td class='actions'><a href='index.php?page=edit&id=" . $emp["Id"] .
"'><i class='fa fa-pencil'> Wysig | </i></a><a href='index.php?
page=delete&id=" . $emp["Id"] . "'<i class='delete-row'><i class='fa fa-
trash-o'> Verwyder | </i></a><a href='index.php?page=single&id=" .
$emp["Id"]
. "'<i class='delete-row'><i class='fa fa-comment-o'> SMS</i></a></td>
</tr>";?>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
</section>
<!-- dash panel end -->
<script>
function myFunction() {
// Declare variables
var input, filter, table, tr, td, i;
input = document.getElementById("search");
filter = input.value.toUpperCase();
table = document.getElementById("myTable");
tr = table.getElementsByTagName("tr");
// Loop through all table rows, and hide those who don't match the search query
for (i = 0; i < tr.length; i++) {
td = tr[i].getElementsByTagName("td")[0];
if (td) {
if (td.innerHTML.toUpperCase().indexOf(filter) > -1) {
tr[i].style.display = "";
} else {
tr[i].style.display = "none";
}
}
}
}
</script>
和method2
接近,不同之处在于使用了理解列表而不是for循环。
method 1
较慢,因为生成长度为500的修改字符串所需的时间比产生线程的时间短。但是对于更长的字符串,此方法可能是最快的。
方法4来自U9-Forward
2
结果:
method 3