我正在努力寻找一种方法来检查列表中的所有其他数字是否具有交替的奇偶校验(即,偶数,奇数,偶数,奇数等)
[1,2,3,4] # odd, even, odd, even (True, because they alternate)
[1,3,2,4] # odd, odd, even, even (False, because they don't alternate)
任何人都知道我该如何检查吗?
答案 0 :(得分:2)
您可以在输入列表中的相邻值上使用Python模运算符<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/cardView"
android:layout_width="match_parent"
android:layout_height="@dimen/giveawayCardHeight"
android:layout_gravity="center"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="3dp"
card_view:cardUseCompatPadding="true"
>
<android.support.v7.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="match_parent"
android:elevation="5dp">
<RelativeLayout
android:layout_width="150dp"
android:layout_height="match_parent"
android:background="#FFFFFF">
<android.support.v7.widget.AppCompatImageView
android:id="@+id/raffleThumbnail"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
/>
<com.tomatedigital.giveawaymaster.ui.FontFitTextView
android:id="@+id/raffleOwner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:alpha="0.7"
android:background="@color/colorPrimaryDark"
android:gravity="center_horizontal|bottom"
android:text="viajandocomgabi"
android:textColor="@color/cardview_light_background"
android:textSize="@dimen/normalFont" />
</RelativeLayout>
<android.support.v7.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="@dimen/smallMargin"
android:orientation="vertical"
android:background="#FFFFFF">
<android.support.v7.widget.AppCompatTextView
android:id="@+id/raffleDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/raf_date_label"
android:textSize="@dimen/normalFont" />
<android.support.v7.widget.LinearLayoutCompat
android:id="@+id/giveawayButtonsLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">
<Space
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<android.support.v7.widget.AppCompatImageButton
android:id="@+id/startButton"
android:layout_width="@dimen/cardButtonSize"
android:layout_height="@dimen/cardButtonSize"
android:background="?android:attr/selectableItemBackground"
android:scaleType="fitXY"
android:src="@drawable/ic_play" />
<Space
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2" />
<android.support.v7.widget.AppCompatImageButton
android:id="@+id/deleteButton"
android:layout_width="@dimen/cardButtonSize"
android:layout_height="@dimen/cardButtonSize"
android:background="?android:attr/selectableItemBackground"
android:scaleType="fitXY"
android:src="@drawable/ic_trash" />
<Space
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
</android.support.v7.widget.LinearLayoutCompat>
<android.support.v7.widget.AppCompatTextView
android:id="@+id/ticketAmountLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:drawableRight="@drawable/ic_tickets"
android:text="@string/you_have"
android:textSize="@dimen/normalFont" />
</android.support.v7.widget.LinearLayoutCompat>
</android.support.v7.widget.LinearLayoutCompat>
</android.support.v7.widget.CardView>
,并在迭代时检查是否相等。
如果您选择这种方法并且效率是一个问题,我建议您使用第三方库(例如NumPy / numba),这样迭代就不会在Python级别进行:
%
答案 1 :(得分:1)
请尝试使用此功能,<div class = "container">
<table class = "table">
<tr>
<td> MODEL: </td>
<td>
<select id="model" name="model" onchange="populate(this.id,
'destination')">
<option value="select">--Select Model--</option>
<option value="Model-A">Model-A</option>
<option value = "Model-B"> Model-B </option>
</select>
</td>
</tr>
</table>
<input type="button" id="addbtn" value="Add Destination"/>
<hr>
<table>
<tr>
<th><center> DESTINATION: </th></center>
<th><center> CRITERIA: </th></center>
<th><center> QTY: </th></center>
<th><center> CELL: </th></center>
</tr>
<tr>
<td width = "140">
<center>
<div id="destination" style = "width:230px;
word-wrap: break-word">
</center>
</div>
</td>
<td width = "140">
<center>
<div id="criteria" style = "width:350px;
word-wrap: break-word">
</center>
</div>
</td>
<td width = "140">
<center>
<div id = "qty" required>
</td>
</center>
<td width = "140">
<center>
<div id = "cell" required>
</td>
</center>
</tr>
进行列表理解,而l
进行奇数或偶数检查,然后检查是否全部为真(第二个偶数索引元素全部为True或全部为False,并且与每个奇数索引元素:
bool
输出:
def oddeven_alter(l):
l=[i%2 for i in l]
return all([any([all(l[::2]),all(not i for i in l[::2])]),any([all(l[1::2]),all(not i for i in l[1::2])])])
)
print(oddeven_alter([1,2,3,4]))
print(oddeven_alter([1,3,2,4]))
种类复杂的思想
答案 2 :(得分:1)
这里
def alter(ls):
for i in range(len(ls)-1):
if ls[i]%2 == ls[i+1]%2:
return False
return True
答案 3 :(得分:1)
您可以遍历序列的所有索引,然后将其与下一个索引进行比较:
def is_alternating_parity(seq):
for i in range(len(seq) - 1):
if seq[i] % 2 == seq[i + 1] % 2:
return False
return True
print(is_alternating_parity([1, 2, 3, 4])) # True
print(is_alternating_parity([1, 3, 2, 4])) # False