![](/img/trans.png)
[英]Removing rows that contain the same dates from another dataframe - python - pandas
[英]Removing the rows that contain the same value in python
我想組合list_a和list_b中的元素。 list_a和list_b都具有相同的元素。每個列表中有5個元素,因此輸出應為5x5 = 25。 但是我希望我的程序不要打印出具有相同元素的那5行,請看一下輸出。
list_a=["apple","banana","melon","grape","orange"] list_b=["apple","banana","melon","grape","orange"]
for x in list_a: for z in list_b: print(x,"-",z)
蘋果蘋果蘋果香蕉香蕉甜瓜葡萄葡萄橙橙色謝謝
我認為您正在尋找沒有重復行的笛卡爾乘積 。
如果是這樣的話:
>>> ['{} - {}'.format(a,b) for a in list_a for b in list_b if a!=b]
['apple - banana', 'apple - melon', 'apple - grape', 'apple - orange', 'banana - apple', 'banana - melon', 'banana - grape', 'banana - orange', 'melon - apple', 'melon - banana', 'melon - grape', 'melon - orange', 'grape - apple', 'grape - banana', 'grape - melon', 'grape - orange', 'orange - apple', 'orange - banana', 'orange - melon', 'orange - grape']
或者,在Python 3.7上:
>>> [f'{a} - {b}' for a in list_a for b in list_b if a!=b]
要使您的版本正常工作,只需在循環中添加一個continue
:
for x in list_a:
for z in list_b:
if x==z: continue
print(x,"-",z)
要么,
for x in list_a:
for z in list_b:
if x!=z: print(x,"-",z)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.