當前位置:首頁 » 編程語言 » pythonforlist

pythonforlist

發布時間: 2022-12-20 08:33:54

python中列表的遍歷

沒有優雅的方法解決,只有不要使用print語句,如樓上所說,或者使用python3.X中的print函數(通過

from __future__ import print_function使能print函數形式)

其實,在python2.X手冊中對print語句描述說:(python2.7.2官方幫助文檔)
一個空格會被自動列印在每個對象前,
除非:(1)還沒有輸出寫到標准輸出中
(2)當最後一個寫到標准輸出的是一個除了空格『 』的空白字元
(3)當最後寫到標准輸出的不是一個print語句。

所以在apple、banana等每個字元前都有一個空格。(apple的a前也有空格呢!)

一個好的解決辦法是使用python3.X中的print函數。
通過在文件前面加上:
from __future__ import print_function
就可以使用print的函數形式了。
print函數的語法:
print([object, ...][, sep=' '][, end='\n'][, file=sys.stdout])
默認下,若沒有指定sep,則使用空格。沒指定end,則使用換行符。沒指定輸出文件則輸出到標准輸出。
例如:print('hello','world',sep='-',end='#')輸出:
hello-world#
所以,你的程序可改為:
from __future__ import print_function
list = ["apple", "banana", "grape", "orange"]
for x in range(len(list)):
print('list[%d]:'%x,end='')
for y in range(len(list[x])):
print(list[x][y],sep='',end='')
print('')

至於: 'list[%d]:'%x 這里的百分號,是一個對字元串的操作符。百分號使得百分號前面的字元串中

的%d被百分號後的x的值替換掉。

⑵ python list遍歷問題,跪求大神指導

list=[('1abc','11112sfgsd'),('g5sd','11112rtrt'),
('y234','eare4543'),('wetw','eareewtwe'),('ryhe','eare'),
('8989','5633tewtt'),('a34f','ertyey')]
list2=[]
#找鍵值插入
foreinlist:
head=e[1].split('')[0]#提取鍵值
found=False
idx=0
forrinlist2:#看鍵值是否存在
ifr[0]==head:
found=True;
break
idx=idx+1
iffound:
list2[idx][1].append(e)#存在直接追加
else:
list2.append([head,[e]])#不存在創建新list
#Tuple化
list3=[]
foreinlist2:
list3.append((e[0],tuple(e[1])))
list3=tuple(list3)

print(list3)

運行結果:

⑶ python語法問題:關於在list中使用for循環

Python 的強大特性之一是其對 list 的解析,它提供一種緊湊的方法,可以通過對 list 中的每個元素應用一個函數,從而將一個 list 映射為另一個 list。

例 3.24. List 解析介紹

>>> li = [1, 9, 8, 4]
>>> [elem*2 for elem in li]
[2, 18, 16, 8]
>>> li
[1, 9, 8, 4]
>>> li = [elem*2 for elem in li]
>>> li
[2, 18, 16, 8]

為了便於理解它,讓我們從右向左看。li 是一個將要映射的 list。Python 循環遍歷 li 中的每個元素。對每個元素均執行如下操作:首先臨時將其值賦給變數 elem,然後 Python 應用函數 elem*2 進行計算,最後將計算結果追加到要返回的 list 中。

需要注意是,對 list 的解析並不改變原始的 list。

將一個 list 的解析結果賦值給對其映射的變數是安全的。不用擔心存在競爭情況或任何古怪事情的發生。Python 會在內存中創建新的 list,當對 list 的解析完成時,Python 將結果賦給變數。

From <dive into python>

⑷ python中,如何在每一次for循環時都建一個新的列表

這樣:

list=[]

>>>foriinrange(5):

...list.append([i])

...

>>>list

[[0],[1],[2],[3],[4]]

>>>list[0]

[0]

>>>list[1]

[1]

(4)pythonforlist擴展閱讀:

注意事項

Python for循環可以遍歷任何序列的項目,如一個列表或者一個字元串。

語法:

for循環的語法格式如下:

for iterating_var in sequence:

statements(s)

例如:

#!/usr/bin/python

# -*- coding: UTF-8 -*-

for letter in 'Python': # 第一個實例

print '當前字母 :', letter

fruits = ['banana', 'apple', 'mango']

for fruit in fruits: # 第二個實例

print '當前水果 :', fruit
print "Good bye!"

⑸ python for循環的問題

else沒執行到是因為for b in list1使得b不再是150,而是list1中的每個值,自然不會執行else了。
你的Python程序我幫你改完了,你看看吧.(改動的地方見注釋)

⑹ python 怎麼判斷list里元素類型

可以通過tpye()方法來判斷list里的元素類型。代碼舉例如下:

testList = [1, 2, 'a', [1, 2]]

for listElement in testList:

print '%s 的類型是:%s' % (listElement, type(listElement))

其中,for in語句用來遍歷testList這個list里的元素,然後分別列印出元素對應的類型,運行程序,輸出結果為:

1 的類型是:<type 'int'>

2 的類型是:<type 'int'>

a 的類型是:<type 'str'>

[1, 2] 的類型是:<type 'list'>

(6)pythonforlist擴展閱讀

python語言中type()函數介紹:

1、type()函數的作用

在python中type()是即簡單又實用的一種對象數據類型查詢方法。它是一個內建的函數,調用它就能夠得到一個反回值,從而知道想要查詢的對像類型信息。

2、type()函數使用方法:type(對象)

type()是接收一個對象當做參考,之後反回對象的相應類型。例如:

type(1)

<type 'int'> #整型

type("iplaypython")

<type 'str'> #字元串


⑺ python中關於for循環取list的數值。

for i in test:
if i=='ABC':
print 'The result is right'

⑻ python中的for循環取list中的第i 個值值

如果一定要用循環寫,是這么寫的
lis=['a','b','c']
for i in lis:
if i=='b':
print(i)

⑼ Python for循環生成列表的實例

Python for循環生成列表的實例
一般Python for語句前不加語句,但我在機器學習實戰中看到了這兩條語句:
featList = [example[i] for example in dataSet]
classList = [example[-1] for example in dataSet]

多方研究和詢問,得到如下解釋:

語句featList = [example[i] for example in dataSet]作用為:

將dataSet中的數據按行依次放入example中,然後取得example中的example[i]元素,放入列表featList中

語句classList = [example[-1] for example in dataSet]作用為:

將dataSet中的數據按行依次放入example中,然後取得example中的example[-1]元素,放入列表classList中

總而言之,類似上述兩種for循環形式可以很方便地用來創建列表,如下例:
list_0 = [x*x for x in range(5)]
print(list_0)
#輸出:
#[0, 1, 4, 9, 16]
以上這篇Python for循環生成列表的實例就是小編分享給大家的全部內容了

⑽ python list操作

就是用類型轉換啊

for i in list:
list2[int(float(i[0])*10)]=i
print list2

熱點內容
android文件夾重命名 發布:2025-05-15 01:13:50 瀏覽:481
cns腳本 發布:2025-05-15 01:13:38 瀏覽:722
數據結構與演算法筆試題 發布:2025-05-15 01:04:20 瀏覽:417
搜狗輸入法如何直接編輯配置文件 發布:2025-05-15 00:51:47 瀏覽:668
電箱都有哪些配置 發布:2025-05-15 00:30:21 瀏覽:74
安卓qq邀請碼在哪裡尋找 發布:2025-05-15 00:02:04 瀏覽:35
三菱fx編程口 發布:2025-05-15 00:01:23 瀏覽:810
醫院招商引資宣傳片腳本 發布:2025-05-15 00:01:21 瀏覽:368
linuxcftp伺服器 發布:2025-05-14 23:58:18 瀏覽:718
探岳什麼配置才有駕駛模式選擇 發布:2025-05-14 23:53:17 瀏覽:146