顯示具有 list 標籤的文章。 顯示所有文章
顯示具有 list 標籤的文章。 顯示所有文章

2018年3月14日 星期三

Python - list的元素的刪除,以另一個list來指定要刪除的元素

版本相關資訊:

System version : Windows 10 64-bit
Python version : Python 3.6.0 :: Anaconda 4.3.1 (64-bit)

Codes:

list1=[1,2,3,3,2]
list2=[2,3]

list3 = [x for x in list1 if x not in list2]
print(list3)

結果:


[1]

2018年2月12日 星期一

Python- 去除list中半形與全形標點符號 -Removing Punctuation From Python List Items

版本相關資訊:

System version : Windows 10 64-bit
Python version : Python 3.6.0 :: Anaconda 4.3.1 (64-bit)

Codes:

tokens = ['abc',',.aaa','=','』','abc']
full_punctuation = '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~' + '→↓△▿⋄•!??〞#$%&』()*+,-╱︰;<=>@〔╲〕 _ˋ{∣}∼、〃》「」『』【】﹝﹞【】〝〞–—『』「」…﹏'

punctuation_tokens_1 = [ele for ele in tokens if not ele in full_punctuation] 
print('punctuation_tokens_1 :',punctuation_tokens_1)

translator = str.maketrans('', '', full_punctuation)
punctuation_tokens_2 = [s.translate(translator) for s in tokens]
print('punctuation_tokens_2 :',punctuation_tokens_2)

punctuation_tokens_3 = [ele for ele in punctuation_tokens_2 if not ele=='']
print('punctuation_tokens_3 :',punctuation_tokens_3)

結果:

punctuation_tokens_1 : ['abc', ',.aaa', 'abc']
punctuation_tokens_2 : ['abc', 'aaa', '', '', 'abc']
punctuation_tokens_3 : ['abc', 'aaa', 'abc']

2018年2月5日 星期一

Python-如何用迴圈取得清單的index-How to Loop With Indexes in Python

版本相關資訊:

System version : Windows 10 64-bit
Python version : Python 3.6.0 :: Anaconda 4.3.1 (64-bit)

Codes:

list1 =['a','b','c']
for idx, val in enumerate(list1):
    print('index :',idx,' , value :', val)

結果:

index : 0 , value : a
index : 1 , value : b
index : 2 , value : c

2017年12月29日 星期五

Python - 清單刪除重複元素,保留原清單排序的作法 - How to remove all duplicate items from a list

版本:

System version : Windows 10  64-bit
Python version : Python 3.6.0 :: Anaconda 4.3.1 (64-bit)
list1=[1,2,4,3,1,2]
list1_remove_duplicate = sorted(set(list1),key=list1.index)
print(list1_remove_duplicate)

執行結果

[1, 2, 4, 3]

2017年9月7日 星期四

Python - Building a list using for loop and other work about list - list用迴圈產生、list串接(相加)、使用迴圈切割list

Python version :Python 3.6.0 :: Anaconda 4.3.1 (64-bit)

System version :Windows 10
list1 = ['a'+str(x) for x in range(7)]
print(list1)
print('='*40)

list2=[]
for a in range(2):
    list2=list2+list1
print(list2)
print('='*40)

num = 3
for a in range(0,len(list2),num):
    print(list2[a:a+num])
執行結果:
['a0', 'a1', 'a2', 'a3', 'a4', 'a5', 'a6']
========================================
['a0', 'a1', 'a2', 'a3', 'a4', 'a5', 'a6', 'a0', 'a1', 'a2', 'a3', 'a4', 'a5', 'a6']
========================================
['a0', 'a1', 'a2']
['a3', 'a4', 'a5']
['a6', 'a0', 'a1']
['a2', 'a3', 'a4']
['a5', 'a6']

2017年9月5日 星期二

Python - Convert String to list and Convert list to dictionary in Python - 字串藉由指定符號轉為list與list轉為字典

Python version :Python 3.6.0 :: Anaconda 4.3.1 (64-bit)
System version :Windows 10
str1 = '背景,0,特性,1,元件,3'
list1 = str1.split(",")
dic1 = {list1[i]: list1[i+1] for i in range(0, len(list1), 2)}
print(list1)
print(dic1)
執行結果:
['背景', '0', '特性', '1', '元件', '3']
{'背景': '0', '特性': '1', '元件': '3'}

2017年7月27日 星期四

Python - Convert list to dictionary with indexes - 將list轉為字典且將index設為字典的key

Version

Python version :Python 3.6.0 :: Anaconda 4.3.1 (64-bit)
System version :Windows 10

Code:

list1 = ['A','B','C']
dic1 = {k:v for k,v in enumerate(list1)}
dic1

Result:

{0: 'A', 1: 'B', 2: 'C'}

2017年7月11日 星期二

Python - How to write a list to a file and read it as a list type using json - list與file的讀寫IO

Python version :Python 3.6.0 :: Anaconda 4.3.1 (64-bit)
Pandas version :0.19.2
System version :Windows 10
# write to file
import json
file_path='./dic1.json'
dic1 = {'list1':[1,2,3,4]}
dic1
with open(file_path, 'w') as f:  
    json.dump(dic1, f)

# read from file
import json
file_path='./dic1.json'
with open(file_path) as f:
    dic2 = json.load(f)
list2=dic2['list1']
list2
執行結果:
``` [1, 2, 3, 4] ```

2017年7月10日 星期一

Python - Using a for loop to add values in a new list - 運用迴圈快速建立list

Python version :Python 3.6.0 :: Anaconda 4.3.1 (64-bit)
System version :Windows 10
new_list = ['item' + str(a)  for a in range(5)]
new_list
執行結果:
['item0', 'item1', 'item2', 'item3', 'item4']