我在 sqlite3 表中有一個包含所有化學元素的列。有一個名為“符號”的列,其中包含化學物質的符號。例如,H、Be、Mg。
我想從 AZ 中提取出列中未出現的所有字符(不區分大小寫)。以下是我目前所擁有的
conn = sqlite3.connect('chemicals.sqlite')
cur = conn.cursor()
cur.execute('select Symbol from chemicalElements')
rows = cur.fetchall()
for row in rows:
print(row)
下表示例
| 代碼 | 象征 | 姓名 |
|---|---|---|
| 1 | H | 氫 |
| 2 | 他 | 氦 |
| 3 | N | 氮 |
| 4 | 鎂 | 鎂 |
在這種情況下,最終輸出應該是 H、E、N、M、G 以外的字母
uj5u.com熱心網友回復:
您可以在查詢中使用 group concat 來獲取一個字串中的所有符號,然后使用一組只保存唯一的字母。
創建一組字母表并對它們進行對稱差分運算。
import string
conn = sqlite3.connect('chemicals.sqlite')
cur = conn.cursor()
cur.execute("select upper(group_concat(symbol,'')) from chemicalElements")
row = cur.fetchone()
symbols = set(row[0])
alphabet = set(string.ascii_uppercase)
other_symbols = alphabet.symmetric_difference(symbols)
print(other_symbols)
uj5u.com熱心網友回復:
建立一組你找到的所有字符,從所有字母的集合中減去它。
conn = sqlite3.connect('chemicals.sqlite')
cur = conn.cursor()
cur.execute('select Symbol from chemicalElements')
rows = cur.fetchall()
found = set()
for row in rows:
found = found.union(list(row[0].upper()))
missing = set(string.ascii_uppercase) - found
print(missing)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/360099.html
上一篇:SQL聚合函式的執行順序
