導航:首頁 > 編程語言 > python怎麼把list中的元素組合

python怎麼把list中的元素組合

發布時間:2023-03-07 09:00:38

python中如何實現列表元素內容合並

list1=['a','b','c']
list2=['1','2','3']
n=len(list1)
list3=[]
foriinrange(0,n):
list3.append('')
foriinrange(0,n):
list3[i]=list1[i]+list2[i]
print(list3)

⑵ python怎麼簡單的生成多個list的元素組合

生成排列可以用proct:

1 from itertools import proct
2 l = [1, 2, 3]
3 print list(proct(l, l))
4 print list(proct(l, repeat=4))

組合的話可以用combinations:

1 from itertools import combinations
2 print list(combinations([1,2,3,4,5], 3))

想更好的學習python請關注微信公眾號「Python基礎教程」!

⑶ 如何排列組合合並Python里兩個list的元素

排列組合合並Python里兩個list的元素
import itertools
a,b=[1,2,3],[4,5,6]
print(list(itertools.proct(a,b)))

⑷ 在python中如何將兩個list合並成一個list,不用for語句

1、運算符:

list1 = [1, 2, 3]

list2 = [4, 5, 6]

list3 = list1 + list2

print(list3)

2、extend()方法:

list1 = [1, 2, 3]

list2 = [4, 5, 6]

list3 = list1.extend(list2)

print(list3)

3、切片方式:

list1 = [1, 2, 3]

list2 = [4, 5, 6]

list1[len(list1):len(list1)] = list2

print(list1)

(4)python怎麼把list中的元素組合擴展閱讀:

list的方法

L.append(var) #追加元素

L.insert(index,var)

L.pop(var) #返回最後一個元素,並從list中刪除之

L.remove(var) #刪除第一次出現的該元素

L.count(var) #該元素在列表中出現的個數

L.index(var) #該元素的位置,無則拋異常

L.extend(list) #追加list,即合並list到L上

L.sort() #排序

L.reverse() #倒序

list 操作符:,+,*,關鍵字del

a[1:] #片段操作符,用於子list的提取

[1,2]+[3,4] #為[1,2,3,4]。同extend()

[2]*4 #為[2,2,2,2]

del L[1] #刪除指定下標的元素

del L[1:3] #刪除指定下標范圍的元素

⑸ 在python中如何實現列表中元素的所有排列組合如輸入為['1','2','3']和2輸出為['

#!/usr/bin/python
#Two method for generate a list whose item is all possible permutation and combination come from every item of many list.

A = ['1', '2']
B = ['a', 'b', 'c']
C = ['A', 'B', 'C', 'D']

retList = []
for a in A:
for b in B:
for c in C:
retList.append((a,b,c))
print retList

print '*' * 40

def myfunc(*lists):
#list all possible composition from many list, each item is a tuple.
#Here lists is [list1, list2, list3], return a list of [(item1,item2,item3),...]

#len of result list and result list.
total = rece(lambda x, y: x * y, map(len, lists))
retList = []

#every item of result list.
for i in range(0, total):
step = total
tempItem = []
for l in lists:
step /= len(l)
tempItem.append(l[i/step % len(l)])
retList.append(tuple(tempItem))

return retList

print myfunc(A,B,C)

⑹ 在Python的IDLE中如何讓兩個list中特定元素組合在一起

這不很簡單嗎直接用索引列印不就可以了,print(list2[0]+list1[1]+','+list2[2]+list1[2])

閱讀全文

與python怎麼把list中的元素組合相關的資料

熱點內容
php和類名相同的方法 瀏覽:360
香港台灣dns伺服器地址列表 瀏覽:537
大同app怎麼樣 瀏覽:438
php去掉特殊字元 瀏覽:387
androidapi中文合集 瀏覽:658
win7下安裝linux虛擬機 瀏覽:838
雲主機用別的伺服器嗎 瀏覽:922
黑馬買入指標源碼副圖 瀏覽:962
微軟為什麼會把伺服器放在水底 瀏覽:257
php截取字元串中文 瀏覽:21
虛擬機和編譯軟體哪個好 瀏覽:750
存儲伺服器為什麼比通用伺服器難 瀏覽:373
用php列印出前一天的時間 瀏覽:369
2010編譯方法 瀏覽:239
華為哪裡查看隱藏app 瀏覽:889
linux網卡重置 瀏覽:830
框架柱低於四米箍筋全高加密 瀏覽:694
米二如何安卓版本升級到高安卓版 瀏覽:783
安卓手機數據慢怎麼辦 瀏覽:727
雙底買賣指標公式源碼無未來函數 瀏覽:685